Question
What Is a Clearfix in CSS? Understanding Float Clearing and Layout Behavior
Question
I was reviewing some website code and noticed that many <div> elements had a class named clearfix.
After a quick search, I learned that it was sometimes used for older browsers like IE6, but I still do not understand what a clearfix actually is.
What is a clearfix in CSS, and why is it needed? Could you also show examples of a layout with a clearfix and without a clearfix so the difference is clear?
Short Answer
By the end of this page, you will understand what a clearfix is, why floated elements can cause their parent container to collapse, and how a clearfix fixes that problem. You will also see practical CSS examples, common mistakes, and how this concept appears in real codebases.
Concept
In CSS, a clearfix is a technique used to make a parent element properly contain its floated children.
To understand why this is needed, you first need to know how floats work.
When an element is floated with CSS like this:
float: left;
that element is taken out of the normal document flow. It still appears on the page, and surrounding content wraps around it, but its parent may no longer calculate its height based on that floated child.
That can lead to a common layout bug:
- the child elements are visible
- but the parent container appears to have
height: 0 - backgrounds, borders, and spacing on the parent may disappear or behave incorrectly
- content below may slide upward and overlap the floated layout
A clearfix solves this by forcing the parent to acknowledge the floated children again.
A common modern clearfix looks like this:
.clearfix::after {
content: "";
display: table;
clear: both;
}
This works by inserting an invisible pseudo-element after the floated children. The clear: both; part tells the browser that this generated element must sit below left and right floats. As a result, the parent expands to include the floated children.
Why it matters:
- older layouts often relied heavily on floats
- many existing codebases still contain float-based components
- understanding clearfix helps you debug collapsed containers
- even though modern layouts usually use Flexbox or Grid, you will still encounter clearfix in legacy CSS
Today, clearfix is mostly a legacy float-fix technique, but it remains important for understanding how CSS layout evolved.
Mental Model
Imagine a parent container as a box that should wrap around everything inside it.
Normally, if you place items in the box, the box grows to fit them.
But floated elements act a bit like balloons tied near the box instead of resting inside it. You can still see them, but the box does not fully count them when deciding its height.
So the box may look empty even though visible items are there.
A clearfix is like placing a small invisible marker at the bottom that says:
"This box must extend below all floating items."
That invisible marker makes the parent box stretch to the correct height again.
Syntax and Examples
The classic idea of clearfix is: after the floated children, add something that clears the floats.
Basic float layout without clearfix
<div class="container">
<div class="box">Left</div>
<div class="box">Right</div>
</div>
<p>This paragraph may move up underneath the floated boxes.</p>
.container {
border: 2px solid steelblue;
background: #eef6ff;
}
.box {
float: left;
width: 100px;
height: 60px;
margin: 10px;
background: coral;
}
What happens?
- Both
.boxelements float left. - The
.containermay collapse because it does not count the floated children for height. - The paragraph below can rise up and appear too close or overlap visually.
The same layout with clearfix
<div class="container clearfix">
<div class="box">Left</div>
<div class="box">Right</div>
</div>
<p>This paragraph now sits below the container correctly.</p>
.container {
border: 2px solid steelblue;
background: #eef6ff;
}
.box {
float: left;
width: 100px;
height: 60px;
margin: 10px;
background: coral;
}
.clearfix::after {
content: "";
display: table;
clear: both;
}
Why this works
The ::after pseudo-element is created after the floated boxes. Because it has clear: both, it moves below the floats. That forces the parent .container to expand enough to include the floats.
Older clearfix variations
You may also see older versions such as:
.clearfix::after {
content: "";
display: block;
clear: both;
}
or old browser hacks designed for IE6/IE7. In modern CSS, the pseudo-element approach is usually enough when working with old float-based layouts.
Step by Step Execution
Consider this HTML and CSS:
<div class="card clearfix">
<img src="avatar.png" alt="Avatar" class="avatar">
<p>Hello there</p>
</div>
.card {
border: 1px solid #999;
padding: 10px;
}
.avatar {
float: left;
width: 60px;
height: 60px;
margin-right: 10px;
}
.clearfix::after {
content: "";
display: table;
clear: both;
}
Step by step
- The browser creates the
.cardelement. - Inside it, the image
.avataris floated left. - Because the image is floated, it is removed from normal flow.
- The paragraph text wraps beside the image.
- Without clearfix, the parent
.cardmight not fully expand to include the floated image. - The
::afterpseudo-element is inserted after the content. clear: bothtells that pseudo-element to sit below any floats.- Since that pseudo-element is inside
.card, the card must extend downward far enough to include it. - The result is that
.cardnow has the correct height.
Visual result
Without clearfix:
- the border may not wrap around the floated image correctly
- the next element below may shift upward
With clearfix:
- the border surrounds the whole content
- spacing below the card behaves correctly
Real World Use Cases
Clearfix was commonly used in real websites when float-based layouts were standard.
Common situations
- Image and text blocks: a floated image thumbnail next to article text
- Navigation menus: horizontal menu items floated left inside a nav container
- Grid systems: columns floated left inside a row container
- Media objects: avatars or icons floated next to text content
- Card layouts: multiple small floated boxes inside a wrapping section
Example: floated columns
<div class="row clearfix">
<div class="column">Sidebar</div>
<div class="column">Main content</div>
</div>
.column {
float: left;
width: 50%;
}
Without clearfix, the .row container may collapse. With clearfix, the .row correctly wraps around both columns.
Why this still matters
Even though modern projects often use Flexbox and Grid, you may still need to:
- maintain older websites
- understand CSS framework internals
- debug layout issues in legacy code
- read tutorials or snippets written before Flexbox became standard
Real Codebase Usage
In real codebases, clearfix is usually used as a utility class.
Common pattern
.clearfix::after {
content: "";
display: table;
clear: both;
}
Then developers apply it wherever a container has floated children:
<div class="profile clearfix">
<img class="avatar" src="user.jpg" alt="User photo">
<div class="bio">...</div>
</div>
Typical usage patterns
- Utility classes: one reusable
.clearfixclass shared across many components - Legacy grid rows: each row clears floated columns
- Guarding old components: if a component contains floats, a clearfix is added to prevent collapse
- Framework support: older CSS frameworks included clearfix helpers because floats were used for columns and alignment
Related patterns developers use instead
In many modern projects, developers avoid clearfix by using:
display: flow-root;on the parent- Flexbox for horizontal alignment
- CSS Grid for multi-column layouts
overflow: auto;oroverflow: hidden;in some cases, though that can have side effects
Practical takeaway
If you see .clearfix in a codebase, it usually means:
- the layout uses floats
- the parent needs help containing floated children
- the code may be older or designed for compatibility with older layout patterns
Common Mistakes
1. Thinking clearfix is only for Internet Explorer
It became famous partly because of older browser issues, but the core problem is not just an IE problem. It comes from how floats interact with normal flow.
2. Applying clearfix to the floated child instead of the parent
Broken idea:
.box.clearfix::after {
content: "";
clear: both;
}
Usually, the clearfix should go on the parent container that needs to wrap around floated children.
Correct idea:
<div class="container clearfix">
<div class="box"></div>
<div class="box"></div>
</div>
3. Forgetting content: "" on the pseudo-element
Broken code:
.clearfix::after {
display: block;
clear: both;
}
Without content, the pseudo-element may not be generated.
Correct code:
.clearfix::after {
content: "";
display: block;
clear: both;
}
4. Using clearfix when floats are not the right tool
If you are building a new layout, Flexbox or Grid is often a better choice.
5. Using overflow: hidden as a fix without understanding the side effects
This can contain floats, but it may also:
- clip shadows
- hide dropdowns
- cut off absolutely positioned content
6. Confusing clear with clearfix
clearis a CSS property used on an elementclearfixis a pattern or utility class that often usesclear: bothinternally
Comparisons
| Approach | What it does | Good for | Downsides |
|---|---|---|---|
clearfix | Makes parent contain floated children | Legacy float layouts | Extra CSS pattern to remember |
clear: both on a real element | Pushes that specific element below floats | Simple cases with a natural next element | Adds layout responsibility to content |
overflow: hidden on parent | Creates a new block formatting context and contains floats | Quick fix in some layouts | Can clip content |
display: flow-root | Creates a new block formatting context cleanly | Modern replacement for clearfix | Not used in very old browsers |
| Flexbox | Lays out items in rows or columns without floats | Modern UI layout | Different model than floats |
| CSS Grid | Handles rows and columns directly | Complex two-dimensional layouts | More than needed for simple float fixes |
Clearfix vs flow-root
If you are writing modern CSS, this is often simpler:
.container {
display: flow-root;
}
This tells the browser to create a new formatting context, which causes the parent to contain floats without needing a pseudo-element.
Clearfix vs Flexbox
Instead of floating columns:
.column {
float: left;
width: 50%;
}
You would often write:
.row {
display: flex;
}
.column {
flex: 1;
}
Flexbox is usually easier for modern horizontal layouts, but clearfix is still useful when reading or maintaining old CSS.
Cheat Sheet
Quick definition
A clearfix is a CSS technique that makes a parent element contain its floated children.
Why it is needed
- Floated elements are removed from normal flow.
- Their parent may collapse in height.
- Borders, backgrounds, and spacing may break.
Common clearfix
.clearfix::after {
content: "";
display: table;
clear: both;
}
Typical usage
<div class="container clearfix">
<div class="item"></div>
<div class="item"></div>
</div>
Key property
clear: both;
This forces an element below left and right floats.
Modern alternative
.container {
display: flow-root;
}
When to use
- mainly in legacy float-based layouts
- when maintaining older CSS
- when a parent does not wrap floated children
When not to use
- for new layouts where Flexbox or Grid is better
Common warning signs
- parent background disappears
- parent border has zero height
- next element overlaps or moves upward
FAQ
What is a clearfix in simple terms?
A clearfix is a CSS trick that makes a container expand to include floated elements inside it.
Why do floated elements break the parent height?
Because floats are removed from normal document flow, the parent often does not count them when calculating height.
Is clearfix still used today?
Mostly in older or legacy CSS. Modern layouts usually use Flexbox, Grid, or display: flow-root instead.
What does clear: both mean?
It means the element must appear below any left-floated or right-floated elements before it.
Can I use overflow: hidden instead of clearfix?
Sometimes, yes, but it can clip content like shadows or dropdowns. It is not always the best choice.
What is the modern replacement for clearfix?
A common modern replacement is:
display: flow-root;
Should I learn clearfix if floats are old?
Yes. You will still encounter floats and clearfix in older codebases, interview questions, and CSS debugging.
Mini Project
Description
Build a simple two-column news card layout using floated elements, then fix the collapsed parent issue with a clearfix. This project demonstrates exactly why clearfix exists and helps you see the visual difference in a realistic component.
Goal
Create a float-based layout where the parent container correctly wraps around its floated columns by using a clearfix utility class.
Requirements
- Create a parent container with a visible border and background.
- Add two child columns floated to the left.
- Show that the parent collapses without a clearfix.
- Add a
.clearfixclass to fix the layout. - Include a paragraph below the container to verify spacing is correct.
Keep learning
Related questions
Allow Only Numeric Input (0-9) in HTML Input Using jQuery
Learn how to allow only digits 0-9 in an HTML input using jQuery, with examples, validation tips, common mistakes, and best practices.
Apply CSS !important with jQuery .css()
Learn why jQuery .css() cannot set !important and how to use CSSStyleDeclaration.setProperty() to apply inline important styles safely.
CSS ::before and ::after on Input Elements
Learn why CSS ::before and ::after usually do not work on input elements, what replaced elements are, and practical alternatives.