Html 使用 :after 清除浮动元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10699343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using :after to clear floating elements
提问by Torben
I have a list and the li's have a float:left;
. The contents after the <ul>
should be aligned correctly. Therefore i can build the following:
我有一个列表,而 li 有一个float:left;
. 之后的内容<ul>
应该正确对齐。因此,我可以构建以下内容:
I thought, that I can remove the <div class="clear">
by using pseudo selectors like :after.
我想,我可以<div class="clear">
通过使用像 :after 这样的伪选择器来删除。
But the example is not working:
但是这个例子不起作用:
Do I always have to create a separate div to clear floating elements?
我是否总是需要创建一个单独的 div 来清除浮动元素?
回答by sandeep
Write like this:
像这样写:
.wrapper:after {
content: '';
display: block;
clear: both;
}
Check this http://jsfiddle.net/EyNnk/1/
回答by kernelpanic
No, you don't it's enough to do something like this:
不,你做这样的事情还不够:
<ul class="clearfix">
<li>one</li>
<li>two></li>
</ul>
And the following CSS:
以及以下 CSS:
ul li {float: left;}
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.clearfix {
display: inline-block;
}
html[xmlns] .clearfix {
display: block;
}
* html .clearfix {
height: 1%;
}
回答by Mahdi
回答by sachin kumar
Use
用
.wrapper:after {
content : '\n';
}
Much like solution provided by Roko. It allows to insert/change content using : after and :before psuedo. For details check http://www.quirksmode.org/css/content.html
很像 Roko 提供的解决方案。它允许使用 :after 和 :before psuedo 插入/更改内容。详情请查看 http://www.quirksmode.org/css/content.html
回答by Alex
The text 'dasda' will never not be within a tag, right? Semantically and to be valid HTML it as to be, just add the clear class to that:
文本“dasda”永远不会不在标签内,对吗?从语义上讲,要成为有效的 HTML,只需向其添加 clear 类: