Blog / CSS

Mastering CSS Clearfix: A Simple Fix for Height Problems in IE6 Compatible Code

Have you ever found yourself in a situation where the height of your parent div seems to collapse when you have floating elements inside it, such as float:left? If so, fear not! There's an auxiliary method called Clearfix that can help solve this issue.

So, how do you use Clearfix? The most common example of using this pseudo-class since the days of IE6 looks something like this:

.clearfix {
  zoom: 1;
}
.clearfix:before,
.clearfix:after {
  content: "";
  display: table;
}
.clearfix:after {
  clear: both;
}

By using clearfix, you can make sure your floating elements are properly contained within their parent div and avoid any collapsing height issues. Pretty neat, huh?

If you prefer a more modern and shorter version of clearfix, you can use the one supported by Bootstrap, which works on IE8+ and looks like this:

.clearfix:after {
  content: "";
  display: table;
  clear: both;
}

Also an alternative solution could be overflow:auto or overflow:hiden

Hope this helps! Let me know if you have any other questions or if there's anything else I can do to assist you.