What is a clearfix?

A clearfix is a CSS technique used to clear floats in web layouts. Floating elements are positioned horizontally, allowing other elements to wrap around them. However, floating elements can cause issues when it comes to the height and positioning of their parent container. A clearfix solves this problem by ensuring that the parent container expands to accommodate its floating children.

Why do we need clearfix?

When you float an element, it is taken out of the normal flow of the document, and other elements may not be aware of its presence. This can lead to layout issues, as the parent container may not expand to encompass the floated elements. Here's an example:

<div class="parent">
    <div class="float-left">Float Left</div>
    <div class="float-right">Float Right</div>
</div>

In this example, the parent container has two child divs, one floating to the left and the other floating to the right. Without using a clearfix, the parent div does not expand to include its floated children:

.parent {
    background-color: lightgray;
}

.float-left {
    float: left;
    width: 50%;
}

.float-right {
    float: right;
    width: 50%;
}

How does clearfix work?

A clearfix works by adding an additional element to the parent container that clears the float. There are multiple ways to implement a clearfix, but two popular methods are:

1. Using the ::after pseudo-element:

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

2. Using a clearfix class:

.clearfix {
    clear: both;
}

Both of these methods effectively add an element after the floated children that clears the float, allowing the parent container to expand accordingly.

Example of a layout with and without clearfix:

Layout without clearfix:

<div class="parent">
    <div class="float-left">Float Left</div>
    <div class="float-right">Float Right</div>
</div>

Layout with clearfix:

<div class="parent clearfix">
    <div class="float-left">Float Left</div>
    <div class="float-right">Float Right</div>
</div>

In both examples, the parent div has two child divs, one floating to the left and the other floating to the right. However, the layout without clearfix results in a broken layout, while the layout with clearfix correctly expands the parent div to include the floated children.

Using clearfix in a real-world example:

Let's say we have a blog post with a main content area and a sidebar. We want the main content area to float to the left and the sidebar to float to the right. Without using a clearfix, the parent container (e.g. a

) would not expand to include the floated elements, resulting in a broken layout. Here's an example of how clearfix can be used to solve this:
<div class="blog-post">
    <div class="main-content float-left">Main Content</div>
    <div class="sidebar float-right">Sidebar</div>
</div>

In this example, we apply the clearfix class to the parent div:

<div class="blog-post clearfix">
    <div class="main-content float-left">Main Content</div>
    <div class="sidebar float-right">Sidebar</div>
</div>

With the clearfix class applied, the parent div now expands to include its floated children, resulting in a correctly laid out blog post.

Cross-browser compatibility:

One of the reasons for using clearfix is to ensure cross-browser compatibility. In the past, older versions of Internet Explorer, specifically IE6, did not handle floats correctly. By using a clearfix, you can ensure that your layout looks consistent across different browsers and versions.

Conclusion:

A clearfix is a CSS technique used to clear floats in web layouts. It solves the issue of floated elements not expanding their parent container, resulting in broken layouts. By adding an element that clears the float, the clearfix ensures that the parent container expands to accommodate its floated children. This clears the way for consistent and reliable cross-browser layouts.