Question
While reviewing a website's source code, I noticed that many <div> elements had a clearfix class. I found references to it being used for older versions of Internet Explorer, but what does a clearfix actually do?
Could you show examples of a layout with a clearfix and the same layout without one, explaining the difference?
Short Answer
You will learn why CSS floats can cause a parent container to appear to have no height, how a clearfix fixes that problem, and which modern layout tools usually make clearfix unnecessary.
Concept
A clearfix is a CSS technique that makes a container include the height of its floated children.
Historically, CSS developers used float: left or float: right to place elements side by side, such as columns, images, and navigation links. A floated element is removed from the normal document flow. This means its parent may act as though the floated child is not there.
For example, if a parent contains only floated children, the parent can collapse to a height of 0. Its background, border, and spacing may disappear, and the next section can move upward as if the floated layout did not exist.
A clearfix adds an invisible element after the floated content. That element uses clear: both, forcing it to sit below left- and right-floated elements. Because the invisible element is in normal flow, the parent expands to contain it.
Clearfix was especially common in older float-based layouts. It is not specifically an IE6 feature, although old Internet Explorer compatibility hacks were sometimes included in clearfix implementations. Today, use Flexbox or CSS Grid for most layouts instead of floats. Clearfix is still useful when maintaining older code or when text needs to wrap around a floated image.
Mental Model
Think of a parent container as a box holding objects.
Normally, every object takes up space in the box, so the box grows tall enough to hold them. A floated element is like an object that is visually inside the box but is not counted when measuring the box's height.
A clearfix is like placing a small, invisible marker at the bottom of the floated objects. The marker is counted by the box, so the box stretches down to the correct height.
Syntax and Examples
The common modern clearfix uses a pseudo-element:
.clearfix::after {
content: "";
display: block;
clear: both;
}
Apply the class to the parent of floated elements:
<div class="profile clearfix">
<img class="avatar" src="avatar.jpg" alt="A user avatar">
<p>
This paragraph wraps beside the image because the image is floated.
</p>
</div>
<section class="next-section">
This section should begin below the entire profile box.
</section>
.profile {
border: 2px solid #4b5563;
padding: 16px;
background: #f3f4f6;
}
.avatar {
float: left;
width: 96px;
height: 96px;
margin-right: 16px;
}
.clearfix::after {
content: "";
display: block;
clear: both;
}
.next-section {
margin-top: 16px;
padding: 16px;
background: #dbeafe;
}
The ::after pseudo-element is generated as the last child of .profile. clear: both prevents it from sitting beside either a left or right float, so it moves below the floated image and gives .profile the required height.
An older alternative is to add a real clearing element:
<div class="container">
<div class="column">First column</div>
<div class="column">Second column</div>
<div class="clear"></div>
</div>
.clear {
clear: both;
}
This works, but it adds markup solely for presentation. The pseudo-element clearfix keeps that presentational detail in CSS.
Step by Step Execution
Consider two floated cards inside a parent:
<div class="cards clearfix">
<article class="card">Card one</article>
<article class="card">Card two</article>
</div>
<p class="notice">Content after the cards</p>
.cards {
border: 3px solid tomato;
padding: 12px;
}
.card {
float: left;
width: 45%;
padding: 20px;
margin-right: 2%;
background: #fde68a;
}
.clearfix::after {
content: "";
display: block;
clear: both;
}
Execution sequence:
- The browser creates the
.cardscontainer and applies its border and padding. - Each
.cardis floated left, so the cards appear side by side when enough horizontal space exists. - Because floated cards are outside normal flow,
.cardswould not normally calculate its height from them. - The browser generates the
.clearfix::afterpseudo-element after both cards. clear: bothrequires that pseudo-element to be positioned below all left and right floats.- The pseudo-element is in normal flow, so
.cardsgrows tall enough to include it. - The
.noticeparagraph is laid out after the now correctly sized.cardscontainer.
Without the .clearfix class, the tomato border can appear to wrap only the container's padding instead of the cards. The following paragraph may visually overlap the area where the cards appear.
Real World Use Cases
Clearfix is mainly encountered in existing float-based CSS. Typical uses include:
- Article images: An image floats left or right so text wraps around it. A clearfix can make an enclosing article section include the image's height.
- Legacy navigation bars: Navigation links floated left inside a header need their header to contain them.
- Older multi-column layouts: Two or three floated column elements need a wrapper with a visible background or border.
- CMS themes and older templates: WordPress themes and other long-lived templates often contain
.clearfix,.cf, or.grouputility classes. - Maintaining old browser-compatible code: A project may retain float layouts where replacing them would be risky or outside the scope of a small fix.
For a new row of cards, navigation items, or page columns, prefer Flexbox or Grid rather than floats.
Real Codebase Usage
In real projects, clearfix is usually defined once as a reusable utility class:
.clearfix::after {
content: "";
display: table;
clear: both;
}
display: table is a traditional variant. For clearfix purposes, display: block also works in most cases. The essential declaration is clear: both.
A legacy component might look like this:
<div class="product-summary clearfix">
<img class="product-summary__image" src="shoe.jpg" alt="Blue running shoe">
<div class="product-summary__details">
<h2>Running shoe</h2>
<p>Lightweight daily trainer.</p>
</div>
</div>
.product-summary__image {
float: left;
width: 140px;
margin-right: 20px;
}
Developers add the clearfix to .product-summary, the element that must visually surround the floated image.
In new code, a component would more often use Flexbox:
.product-summary {
display: flex;
gap: 20px;
align-items: flex-start;
}
.product-summary__image {
width: 140px;
}
Flexbox keeps its children in normal layout flow, so the parent naturally has the correct height and no clearfix is needed.
Common Mistakes
Applying clearfix to the floated child
The clearfix belongs on the parent that contains floats, not on each floated item.
/* Usually incorrect for containing the cards */
.card.clearfix::after {
content: "";
clear: both;
}
/* Correct: the parent must contain its floated children */
.cards.clearfix::after {
content: "";
display: block;
clear: both;
}
Forgetting content
A pseudo-element is not generated without content.
/* Broken: no generated pseudo-element */
.clearfix::after {
clear: both;
}
Use content: "".
Using floats for every modern layout
Floats were designed primarily for text wrapping around content. They can create layout side effects that require clearing. Prefer Flexbox for one-dimensional rows or columns and Grid for two-dimensional page layouts.
Clearing only one direction unnecessarily
If a container can have both float: left and float: right children, use:
clear: both;
clear: left only clears left floats, while clear: right only clears right floats.
Confusing clear with float
floatmoves an element to a side and lets surrounding inline content wrap around it.cleartells an element not to sit beside preceding floats on a specified side.
Assuming overflow: hidden is always a clearfix replacement
This can contain floats:
.container {
overflow: hidden;
}
But it may clip dropdowns, shadows, tooltips, or other content that extends outside the container. Do not use it as a default clearfix technique.
Comparisons
| Technique | Main purpose | Does it contain floated children? | Best use today |
|---|---|---|---|
float | Wrap text around an element or create legacy side-by-side content | No | Text wrapping and legacy maintenance |
| Clearfix | Make a parent expand around floated children | Yes | Existing float-based layouts |
clear: both on a real element | Put a specific following element below floats | Yes, if placed inside the parent | Simple legacy markup, but adds a presentational element |
display: flex | Arrange items in a row or column | Naturally | Most new one-dimensional layouts |
display: grid | Arrange items in rows and columns | Naturally | Most new two-dimensional layouts |
overflow: auto | Create a scroll/clipping context; can also establish float containment | Often | Only when its overflow behavior is desired |
A key distinction is that clear: both can be used directly on a specific next element, while a clearfix is a reusable pattern that creates a clearing element automatically after a container's contents.
Cheat Sheet
/* Standard clearfix utility */
.clearfix::after {
content: "";
display: block;
clear: both;
}
<!-- Add it to the parent of floated elements -->
<div class="clearfix">
<img class="float-left" src="photo.jpg" alt="Example">
</div>
.float-left {
float: left;
}
- Floated children do not contribute to their parent's normal height.
- A clearfix creates an invisible final child that clears floats.
- Use
clear: bothwhen either left or right floats may be present. - Add clearfix to the container, not normally to each float.
- For new rows, columns, and card layouts, use Flexbox or Grid.
- Floats remain appropriate when text should wrap around an image or similar content.
FAQ
What is a clearfix in CSS?
A clearfix is a CSS pattern that makes a parent container expand to include floated child elements.
Why does a parent collapse when its children use float?
Floated elements are removed from normal document flow. Because they are not counted in normal flow, a parent containing only floats may calculate no content height.
What does clear: both mean?
It means an element must be placed below any preceding left-floated or right-floated elements.
Is clearfix still needed in modern CSS?
It is needed when maintaining float-based layouts. For new component layouts, Flexbox and Grid normally eliminate the need for it.
Should I use overflow: hidden instead of clearfix?
Usually no. While it can contain floats, it can also clip visible content such as shadows, menus, and tooltips.
Why do some clearfix snippets use display: table?
It is a historical clearfix variant with reliable behavior. display: block is also commonly used; the important part is generating the pseudo-element and giving it clear: both.
Does clearfix fix a layout where text overlaps a float?
It fixes the parent-height issue caused by floats. If a particular following element must start below a float, applying clear: both directly to that following element may be the more direct solution.
Mini Project
Description
Build a small legacy-style product list using floated product cards. The exercise demonstrates the visible problem caused by uncontained floats and then uses a clearfix utility so the product list background and footer are positioned correctly.
Goal
Create two floated product cards whose parent container correctly surrounds them, with a footer displayed below the card list.
Requirements
:[
Keep learning
Related questions
Angular ngClass Conditional Class Binding Explained
Learn how to use Angular ngClass for conditional classes, fix common binding mistakes, and understand why this template error happens.
Apply CSS to an iframe: Same-Origin Styling in HTML
Learn why parent-page CSS does not style iframe content, how same-origin rules work, and practical ways to style embedded pages safely.
Apply Multiple CSS Transforms: rotate() and translate()
Learn how to combine multiple CSS transform functions such as rotate() and translate(), understand transform order, and avoid overwritten rules.