Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clearfix issue with <ul> <li> tag

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)

enter image description here

add this ul{list-style: none;} the heights same.

like image 622
user3114590 Avatar asked Dec 18 '13 09:12

user3114590


People also ask

What is Clearfix and what problem does it fix?

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.

What does Clearfix mean?

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.

What is container Clearfix?

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.

What is div class Clearfix?

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.


2 Answers

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/

like image 187
Adam Sheridan Avatar answered Oct 04 '22 12:10

Adam Sheridan


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;
}
like image 25
Slavenko Miljic Avatar answered Oct 04 '22 13:10

Slavenko Miljic