What Is a Clearfix in CSS, and Do You Still Need One?

Quick Summary
- A clearfix adds a cleared ::after element to a container, so the container stretches to wrap its floated children instead of collapsing.
- The container collapses because floats leave the normal flow, and a parent's height only counts the content that is still in the flow.
- In current CSS, display: flow-root on the container does the same job without a clearfix, and flexbox and grid layouts need no clearing at all.
A clearfix is a small CSS rule that makes a parent element stretch to wrap its floated children. Without it, a box whose content is floated collapses, so its border and background stop short and the floats hang out of the bottom. In current CSS you rarely need the hack: display: flow-root on the parent does the same job in one line, and flexbox and grid layouts don’t use floats at all.
Why a container collapses around floats
Take a card with a floated image and a short caption:
<div class="card">
<img class="card-image" src="photo.jpg" alt="">
<p>A short caption.</p>
</div>.card {
border: 1px solid #ccc;
padding: 10px;
}
.card-image {
float: left;
width: 150px;
margin-right: 15px;
}If the image is taller than the caption, it pokes out of the bottom of the card. The border wraps the caption and stops there, and text that comes after the card wraps around the image.
The reason is that a float leaves the normal flow. The browser works out the card’s height from the content that is still in the flow, here the caption, and leaves the float out. If every child is floated, nothing is left in the flow and the card’s height drops to zero, apart from its padding.
That isn’t a bug. Floats were made for wrapping text around images, and a picture floated in one paragraph has to be able to hang down beside the next one. Eric Meyer explained this in Containing Floats back in 2003. It only turns into a problem when you use floats to build layouts, as most sites did before flexbox.
How the clearfix works
The fix is to put something at the end of the container that sits below the floats. The old way was an empty <div style="clear: both"> in the markup. A clearfix creates that element with CSS instead, so the HTML stays clean:
.clearfix::after {
content: "";
display: table;
clear: both;
}::aftergenerates a box as the last child of the element.content: ""is required. Without acontentvalue, the browser doesn’t render the pseudo-element at all.clear: bothmoves that box below any earlier left or right float. The parent now has an in-flow child under the floats, so it grows to reach it.display: tableis there because::afteris inline by default andclearonly applies to block-level boxes.display: blockworks as well; Bootstrap’s clearfix mixin uses it.
Add the class to the element that holds the floats:
<div class="card clearfix">
<img class="card-image" src="photo.jpg" alt="">
<p>A short caption.</p>
</div>Older stylesheets carry longer versions, with content: " ", visibility: hidden, height: 0 or old Internet Explorer hacks such as *zoom: 1. Those lines were for browsers nobody supports anymore, and the three-line rule above is enough.
The modern fix: display: flow-root
You can get the same result without an extra class or pseudo-element:
.card {
display: flow-root;
border: 1px solid #ccc;
padding: 10px;
}flow-root makes the element an ordinary block box that also starts a new block formatting context (BFC). An element that starts a BFC contains the floats inside it, so its height includes them. It also keeps outside floats from overlapping it and stops its children’s margins from collapsing through its edges, which is usually what you want for a card anyway.
Every current browser supports flow-root: Chrome since version 58, Firefox since 53 and Safari since 13.
Before flow-root existed, the common shortcut was overflow: hidden or overflow: auto on the container, because those also start a BFC. They work, but overflow is meant for clipping and scrolling: hidden cuts off box shadows and anything that sticks out of the box, and auto can add scrollbars you didn’t ask for. flow-root has no such side effects.
Flexbox and grid don’t need it
If you float elements to put them side by side, that’s the job flexbox and grid were made for. float and clear have no effect on flex items or grid items, so inside a flex or grid container there is nothing to clear, and the container’s height includes its children:
.columns {
display: flex;
gap: 20px;
}
.columns > * {
flex: 1;
}Which one should you use?
- Columns, cards in a row, navigation bars: flexbox or grid. No floats, nothing to clear.
- Text wrapping around an image: keep the float and add
display: flow-rootto the container. - An older theme or framework that already uses a
.clearfixclass: leave it in place. It’s harmless, and removing it means retesting every layout that relies on it. If you keep a clearfix helper in a Sass project, a utilities partial, as in the 7-1 Sass architecture, is a natural home for it.
Further reading
- Containing Floats by Eric Meyer: why floats hang out of their containers
- All About Floats on CSS-Tricks
- Block formatting context on MDN: the full list of properties that create one
- What is a clearfix? on Stack Overflow
