简体   繁体   English

内联元素和标记空白渲染问题

[英]inline elements, and markup white space rendering issues

Way back in the day, we had to deal with browsers adding white space between elements if in the source markup we also had white space. 回到过去,我们不得不处理浏览器在元素之间添加空格,如果在源标记中我们也有空格。

I thought that issue had all but disappeared but I rant into a situation today where the problem still exists. 我认为这个问题几乎消失了,但我今天陷入困境仍然存在的情况。 What's odd is that the problem isn't confined to a particular browser. 奇怪的是,问题不仅限于特定的浏览器。 It shows up the same problematic way in Firefox, Safari, Chrome and Opera and only slightly differently in IE. 它在Firefox,Safari,Chrome和Opera中显示出同样有问题的方式,在IE中只是略有不同。

Sample CSS: CSS示例:

<style type="text/css">
li {
    display: inline;
    background: pink;
    margin: 0px;
    padding: 10px 0px;
}

li a {
    background: green;
    margin: 0px;
    padding: 0px;
}

</style>    

Sample markup: 样本标记:

<ul>
<li>
    <a href="#">hello</a>
</li>
<li>
    <a href="#">world</a>
</li>
</ul>

<ul>
<li><a href="#">hello</a></li>
<li><a href="#">world</a></li>
</ul>

<ul>
<li><a href="#">hello</a></li><li><a href="#">world</a></li>
</ul>

Only that last UL appears the way I'd like it to appear...with the A tags spanning the full width of the container LI tag. 只有最后一个UL出现的方式我希望它出现... A标签跨越容器LI标签的整个宽度。

Given the consistency across browsers, this maybe is actually rendering as it should? 鉴于浏览器的一致性,这可能实际上是应该渲染的吗? Short of reverting to old comment hacks (starting a comment on the end of one line and expanding to the beginning of the next) anyone know of a workaround for this or why this is doing what it does? 如果没有恢复旧的评论黑客(开始评论一行的结尾并扩展到下一行的开头),任何人都知道这个的解决方法或者为什么这样做呢?

Ideally, I'd float my LI's instead, but due to some other issues keeping then inline would be preferred. 理想情况下,我会漂浮我的LI,但由于一些其他问题保持,然后内联将是首选。

Yup, whitespace is whitespace when it comes to inline elements. 是的,当涉及到内联元素时,空白是空白。 That's almost always exactly what you want. 这几乎总是你想要的。 Take, for example, the following: 举个例子,如下:

<p>I <em>really</em> <strong>really</strong> want that whitespace.</p>

Would suck pretty hard if that ended up rendered as: 如果最终呈现为:

I *really***really** want that whitespace. 我真的非常想要那个空白。

If all the browsers render it a certain way, it's pretty likely that they're right and you're wrong. 如果所有浏览器都以某种方式呈现它,那很可能是因为它们是对的并且你错了。 That goes double if all browsers except for IE render it a certain way... 如果除了IE之外的所有浏览器都以某种方式呈现它,那么这将是双倍

I believe this is by design. 我相信这是设计的。
You have included white space (even if it is carriage returns) in your source so the browser is rendering this faithfully. 您已在源中包含空格(即使它是回车符),因此浏览器会忠实地呈现此内容。 I doubt there is a way around this apart from ensuring there is no white space between your elements. 我怀疑除了确保你的元素之间没有空白之外,还有办法解决这个问题。

您可以将容器的字体大小设置为0,例如将其放入div中,然后在LI项中设置propper font-size

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM