为什么 :after 元素会受到换行符的影响?
我有一个像这样编码的简单菜单,
<ul id="main-menu" class="container">
<li><a href="#">About Us</a></li>
<li><a href="#">Home</a></li>
<li><a href="#">Villas & Yachts</a></li>
<li><a href="#">Islands</a></li>
<li><a href="#">Gallery</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Get In Touch</a></li>
</ul>
看起来像这样
每个菜单项之间的小点是使用 :after 伪元素创建的。一切工作正常,但我还需要子菜单,它将是嵌套列表。
问题是,当我像这样向菜单添加换行符时,
<ul id="main-menu" class="container">
<li><a href="#">About Us</a></li>
<li><a href="#">Home</a></li>
<li><a href="#">Villas & Yachts</a>
<!-- LINE BREAK -->
</li>
<li><a href="#">Islands</a>
<!-- LINE BREAK -->
</li>
<li><a href="#">Gallery</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Get In Touch</a></li>
</ul>
我在 Safari 和 Safari 中得到了这个结果。 Chrome(但不是 Firefox)...
在我看来,webkit 将空格视为“前”。 :after 元素的 CSS 看起来像这样
ul#main-menu li:after
{
content: "\00b7";
width: 61px;
float: right;
text-align: center;
border: rgba(225,225,225,0.25) 1px solid;
}
我还尝试在 ul、li 和 :after 元素上设置 white-space: normal/nowrap
,这不会影响任何内容。
任何人都可以看到我哪里出错了,或者这是 Webkit/Firefox 的问题吗?
更新
我在 http://jsfiddle.net/zmVbH/ 创建了一个演示
I have a simple menu coded up like this
<ul id="main-menu" class="container">
<li><a href="#">About Us</a></li>
<li><a href="#">Home</a></li>
<li><a href="#">Villas & Yachts</a></li>
<li><a href="#">Islands</a></li>
<li><a href="#">Gallery</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Get In Touch</a></li>
</ul>
which looks like this
The little dots in-between each menu item are created using the :after pseudo element. Eveything is working fine, but I also need sub menus, which will be nested lists.
The problem is, when i add a line break to the menu like this
<ul id="main-menu" class="container">
<li><a href="#">About Us</a></li>
<li><a href="#">Home</a></li>
<li><a href="#">Villas & Yachts</a>
<!-- LINE BREAK -->
</li>
<li><a href="#">Islands</a>
<!-- LINE BREAK -->
</li>
<li><a href="#">Gallery</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Get In Touch</a></li>
</ul>
I get this result in Safari & Chrome (But not Firefox)...
It seems to me as though webkit is treating the whitespace as 'pre'. The CSS for the :after element looks like this
ul#main-menu li:after
{
content: "\00b7";
width: 61px;
float: right;
text-align: center;
border: rgba(225,225,225,0.25) 1px solid;
}
I've also tried setting white-space: normal/nowrap
on the ul, li and :after elements which doesn't affect anything.
Can anyone see where I'm going wrong, or is this a problem with Webkit/Firefox?
UPDATE
I've created a demo at http://jsfiddle.net/zmVbH/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是换行符是空白,这使得浮动内容掉一行。可以通过在
和
之间添加一个空格来重现该问题。尝试使插入的内容
display:inline-block
而不是浮动。更新了 JSFiddle。
OP更新是的
,inline-block修复了这个问题,但事情并不是那么简单,因为inline-block有一些不完整的浏览器支持。
The issue is that the line break is white space which makes the floated content drop a line. The issue can be reproduced by adding a single space between the
</a>
and</li>
. Try making the inserted contentdisplay:inline-block
instead of floated.Updated JSFiddle.
UPDATE BY OP
Yup, inline-block fixes this, but it's not quite that simple since inline-block has some patchy browser support.