【发布时间】:2016-03-10 17:07:25
【问题描述】:
我一直在学习 csstricks 的 clearfix hack。在The Easy Clearing Methos。我发现它的行为出乎意料。他们建议了代码:
.clearfix:after {
content: "";
visibility: hidden;
display: block;
height: 0;
clear: both;
}
首先我按原样尝试过;它没有清除任何东西。然后我用了一个空格作为内容(content: " ");什么都没有被清除。然后我使用了一个字符作为内容,content: "H"。 H 出现在浮动 div 的上方和下方,但没有清除任何内容。以下是我使用以下代码的代码:
Content: ""
.rowx div:nth-of-type(even) {
background-color: darkgray;
}
.rowx div:nth-of-type(odd) {
background-color: lightgray;
}
.hack:after,
.hack:before {
content: "";
clear: both;
display: block;
}
.fitty {
width: 20%;
float: left;
}
<div class="rowx">
<div class="fitty">.col-xs-4 </div>
<div class="fitty hack">.col-xs-4</div>
<div class="fitty">.col-xs-4</div>
<div class="fitty">.col-xs-4 </div>
</div>
Content: " "
.rowx div:nth-of-type(even) {
background-color: darkgray;
}
.rowx div:nth-of-type(odd) {
background-color: lightgray;
}
.hack:after, .hack:before {
content: " "; clear: both; display: block;
}
.fitty {
width: 20%; float: left;
}
<div class="rowx">
<div class="fitty">.col-xs-4 </div>
<div class="fitty hack">.col-xs-4</div>
<div class="fitty">.col-xs-4</div>
<div class="fitty">.col-xs-4 </div>
</div>
Content: "H"
.rowx div:nth-of-type(even) {
background-color: darkgray;
}
.rowx div:nth-of-type(odd) {
background-color: lightgray;
}
.hack:after,
.hack:before {
content: "H";
clear: both;
display: block;
}
.fitty {
width: 20%;
float: left;
}
<div class="rowx">
<div class="fitty">.col-xs-4 </div>
<div class="fitty hack">.col-xs-4</div>
<div class="fitty">.col-xs-4</div>
<div class="fitty">.col-xs-4 </div>
</div>
问题:
- 为什么 clearfix 不能清除浮动元素?
-
content: " "中的空白是否被 Web 浏览器完全忽略?
【问题讨论】: