I use clearfix to clear the float. But the problem is ,there is a different height in <li>
and <div>
. li.clearfix
height is 32px, but div.clearfix
height is 18px. when I delete .clearfix:before
, they are all the same.
But, when tried in bootstrap, it's failed.(I delete the .clearfix:before
in bootstrap, but there is still a difference in height.)
<style>
.pull-left{
float:left;
}
.clearfix {
*zoom: 1;
}
.clearfix:before,
.clearfix:after {
display: table;
line-height: 0;
content: "";
}
.clearfix:after {
clear: both;
}
</style>
<div class="clearfix">
<div class="pull-left">Hello</div>
</div>
<ul>
<li class="clearfix"><div class="pull-left">hello</div></li>
</ul>
Demo:http://jsfiddle.net/nevimop/p4HMS/
browsers (chrome safari ie10 , no problem in ff)
add this ul{list-style: none;}
the heights same.
The clearfix, for those unaware, is a CSS hack that solves a persistent bug that occurs when two floated elements are stacked next to each other. When elements are aligned this way, the parent container ends up with a height of 0, and it can easily wreak havoc on a layout.
A clearfix is a way for an element to automatically clear or fix its elements so that it does not need to add additional markup. It is generally used in float layouts where elements are floated to be stacked horizontally.
The clearfix property is generally used in float layouts where elements are floated to be stacked horizontally. The CSS float property states how an element should float; i.e., it specifies the position where an element should be placed. The clearfix property allows a container to wrap its floated children.
Clearfix property clear all the floated content of the element that it is applied to. It is also used to clear floated content within a container. Example 2: With clearfix property. Without using the clearfix class, the parent div may not wrap around the children button elements properly and can cause a broken layout.
Clearfix is used to force the parent of floated element(s) to receive a height, it does this by using display: table
on :before
and :after
pseudo elements to contain the margins of it's children and then uses clear: both
on the :after
element to clear it's own float. Together this allows us to apply a clearfix without needing additional markup.
If we force the :before
pseudo element not to use display: table
and instead use display: inline
we can solve your issue of the unnecessary white-space.
li.clearfix:before {
display: inline;
}
http://jsfiddle.net/72Nmy/
Is there any reason why do you set :after
and :before
as display:table;
instead of display:block;
Something like this: http://jsfiddle.net/N6sG7/3/
.clearfix:before,
.clearfix:after {
display: table;
line-height: 0;
content: "";
display:block;
height:0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With