Today is 19:50:31 ()
What is ‘Float’ in CSS?
In CSS, the float property is used to position an element to the left or right of its container, allowing text and other inline elements to wrap around it. This is commonly used for creating layouts where images are placed alongside text, or for more complex multi-column designs. However, a common issue arises when elements after a floated element are affected by the float, leading to layout problems. This is where ‘float clearing’ comes into play.
The Problem: Floated Elements and Layout Collapses
When an element is floated, it’s essentially taken out of the normal document flow. This means the container element might not recognize the height of the floated element, causing the container to collapse. Furthermore, subsequent elements can “flow” up and around the floated element, which is often not the desired behavior. This can lead to overlapping content or unexpected layout breaks.
Imagine a scenario with two div elements. The first is floated left, and the second follows immediately after. Without clearing the float, the second div might appear to sit beside the floated element, rather than below it, or its container might not properly enclose the floated element.
Methods for Clearing Floats
Several techniques can be used to clear floats and restore the normal document flow. Here are the most common:
The ‘clear’ Property
The clear property is the most straightforward way to clear floats. It specifies which side of an element should not allow floating elements. Possible values are:
clear: left;: Prevents the element from floating to the left of it.clear: right;: Prevents the element from floating to the right of it.clear: both;: Prevents the element from floating on either side of it. This is the most commonly used value.
Example:
.container {
width: 500px;
border: 1px solid black;
}
.floated-image {
float: left;
width: 150px;
height: 100px;
margin-right: 10px;
}
.clear {
clear: both;
}
.content {
background-color: #f0f0f0;
padding: 10px;
}
In this example, the .clear class, when applied to an element, will ensure that it appears below the floated image.
The Overflow Technique
Applying overflow: auto; or overflow: hidden; to a container element can often clear floats. This works because the overflow property creates a new block formatting context, which contains the floats.
Example:
.container {
width: 500px;
border: 1px solid black;
overflow: auto; /* or overflow: hidden; */
}
.floated-image {
float: left;
width: 150px;
height: 100px;
margin-right: 10px;
}
.content {
background-color: #f0f0f0;
padding: 10px;
}
This method is often preferred when you want to avoid adding extra markup (like the .clear div).
The ‘clearfix’ Hack
The ‘clearfix’ hack is a widely used technique that involves adding a pseudo-element (::after) to the container element. This pseudo-element is given content: ""; and display: table;, which effectively clears the floats.
Example:
.container {
width: 500px;
border: 1px solid black;
}
.container::after {
content: "";
display: table;
clear: both;
}
.floated-image {
float: left;
width: 150px;
height: 100px;
margin-right: 10px;
}
.content {
background-color: #f0f0f0;
padding: 10px;
}
This method is considered a robust and widely compatible solution.
Choosing the Right Method
The best method for clearing floats depends on your specific needs:
clearproperty: Simple and direct, but requires adding extra markup.overflowtechnique: Avoids extra markup, but can sometimes have unintended side effects if you’re relying on the container’s overflow behavior for other purposes.- ‘clearfix’ hack: Robust and widely compatible, but involves using a pseudo-element.
Beyond the Basics: Modern Layout Techniques
While understanding float clearing is important for maintaining older codebases, modern CSS layout techniques like Flexbox and Grid often provide more flexible and powerful solutions for creating complex layouts without the need to worry about floats and clearing.
Consider using Flexbox or Grid for new projects whenever possible.

