Question
In CSS, block elements such as div normally expand to contain their child elements. However, when child elements are floated, their non-floated parent may appear to collapse and behave as if it has height: 0.
For example:
<div>
<div style="float: left;">Div 1</div>
<div style="float: left;">Div 2</div>
</div>
In this case, the parent div does not expand to contain the floated children.
What are the correct ways to prevent this parent collapse? Consider common approaches such as:
- floating the parent
- giving the parent an explicit height
- inserting a clearing element
- using
overflow: autoor similar
What are the pros, cons, and browser-related tradeoffs of these solutions?
Short Answer
By the end of this page, you will understand why floated elements are removed from normal document flow, why that causes parent containers to collapse, and how to fix it safely. You will also learn when to use classic clearfix techniques, when overflow works, and why modern layouts like Flexbox or Grid are often better choices today.
Concept
In CSS, a floated element is taken out of the normal document flow. That means nearby content can wrap around it, but its parent does not automatically measure its height the same way it would for a regular block child.
When all children inside a container are floated, the parent may end up with no normal-flow content to size itself from. The result is a parent that visually looks empty or collapsed.
Example:
<div class="parent">
<div class="child">Div 1</div>
<div class="child">Div 2</div>
</div>
.child {
float: left;
}
Even though the children are visible, the .parent may behave as though it has zero height.
This matters because collapsed parents can cause:
- backgrounds and borders to disappear
- following content to overlap the floated section
- broken spacing and layout bugs
- confusing behavior for beginners
Historically, developers used floats for page layout, so this issue was extremely common. Today, floats are still useful for text wrapping around images, but they are no longer the best tool for most full layout tasks.
Mental Model
Imagine a parent element as a box trying to measure how tall its children are.
Normal children stand on the floor inside the box, so the box can easily grow around them.
Floated children are different. They are more like balloons tied near the side of the box. You can see them, but they are not standing on the floor in the usual way. So the box says, “I do not detect anything taking up height,” and collapses.
To fix this, you can:
- tell the box to notice the balloons using a clearfix
- make the box establish a special layout context using
overflow - place something after the balloons that forces the box to stretch using a clear element
- stop using balloons for layout altogether by using Flexbox or Grid
Syntax and Examples
The most common traditional fix is a clearfix.
<div class="parent clearfix">
<div class="child">Div 1</div>
<div class="child">Div 2</div>
</div>
.child {
float: left;
margin-right: 12px;
}
.clearfix::after {
content: "";
display: table;
clear: both;
}
This works by inserting a generated pseudo-element after the floated children. That pseudo-element clears the floats, causing the parent to expand.
Another common fix is overflow:
<div class="parent">
<div class=>Div 1
Div 2
Step by Step Execution
Consider this example:
<div class="parent clearfix">
<div class="child">A</div>
<div class="child">B</div>
</div>
.parent {
border: 2px solid black;
}
.child {
float: left;
width: 80px;
height: 40px;
}
.clearfix::after {
content: "";
display: table;
clear: both;
}
Here is what happens step by step:
- The browser creates the
.parentelement. - Inside it, both
.childelements are floated left. - Because floated elements are removed from normal flow,
.parentdoes not use them to calculate height in the usual way.
Real World Use Cases
This issue shows up in several practical situations:
- Image with wrapped text: a floated image inside an article can cause surrounding containers to size strangely.
- Legacy navigation bars: older layouts often used floated list items for horizontal menus.
- Card rows in older codebases: before Flexbox, developers floated boxes left to create columns.
- CMS themes and templates: many WordPress and older site themes still contain float-based layouts.
- Email-like or legacy frontend systems: some environments continue using older CSS patterns for compatibility reasons.
Example: legacy floated card layout
<div class="cards clearfix">
<div class="card">Card 1</div>
<div class="card">Card 2</div>
<div class="card">Card 3</div>
</div>
.card {
float: left;
width: 30%;
margin-right: 3%;
}
Real Codebase Usage
In real projects, developers usually choose one of these patterns:
1. Reusable clearfix utility
A utility class is added where needed:
.clearfix::after {
content: "";
display: block;
clear: both;
}
<section class="product-list clearfix">
...
</section>
This is common in legacy codebases because it is reusable and easy to apply.
2. overflow: hidden or overflow: auto
.container {
overflow: hidden;
}
This is short and practical, but developers must be careful because clipped overflow may hide dropdowns, shadows, or absolutely positioned content.
3. Migration to Flexbox or Grid
In modern codebases, teams often replace float-based layout code with:
.container {
display: flex;
}
or:
Common Mistakes
1. Assuming the parent will always grow automatically
Beginners often expect this to work:
<div class="parent">
<div class="child">One</div>
<div class="child">Two</div>
</div>
.child {
float: left;
}
But the parent may collapse because floats are not in normal flow.
2. Using a fixed height as a quick fix
Broken approach:
.parent {
height: 100px;
}
This may seem to work at first, but it breaks when:
- content becomes taller
- text wraps differently
- responsive layouts change size
Prefer clearfix or modern layout methods.
3. Using overflow: hidden without understanding side effects
Comparisons
| Approach | How it works | Pros | Cons | Best use |
|---|---|---|---|---|
| Float the parent | Parent also becomes floated | Can contain floated children | Changes parent layout behavior | Rarely the best choice |
| Fixed height | Parent gets explicit height | Simple to understand | Not flexible, breaks with dynamic content | Only when height is truly known |
| Clear element | Add an element with clear: both | Reliable and easy | Extra non-semantic markup | Legacy HTML where CSS-only fix is hard |
| Clearfix pseudo-element | Generated content clears floats | Clean, reusable, no extra HTML | Slightly more CSS to learn | Best traditional float fix |
Cheat Sheet
/* Classic clearfix */
.clearfix::after {
content: "";
display: table;
clear: both;
}
/* Overflow containment */
.container {
overflow: auto;
}
/* Legacy clear element */
.clear {
clear: both;
}
Rules to remember
- Floated elements are removed from normal flow.
- A parent with only floated children may collapse.
clear: bothforces an element below floats.- A clearfix uses a pseudo-element to clear floats without extra HTML.
overflow: autoorhiddencan contain floats by creating a new formatting context.- Flexbox and Grid are usually better than floats for layout.
Good defaults
- For legacy float layouts: use a clearfix.
- For simple containment: try
overflow: autocarefully. - For new layouts: use
display: flexordisplay: grid.
Avoid when possible
- fixed heights for unknown content
- empty spacer elements in HTML
FAQ
Why do floated elements make the parent collapse?
Floated elements are taken out of normal document flow, so the parent may not count them when calculating its height.
What is the best way to clear floats in CSS?
For traditional float-based layouts, a clearfix pseudo-element is usually the cleanest solution.
Is overflow: auto a valid fix for float collapse?
Yes. It often works by creating a new block formatting context, but it can introduce scrollbars or clipping-related side effects.
Should I still use floats for layout?
Usually no. Use Flexbox or Grid for most modern layout tasks. Floats are still useful for wrapping text around images.
Why is adding an empty clearing div discouraged?
Because it adds markup that exists only for presentation, which makes HTML less semantic and harder to maintain.
Is setting a fixed height on the parent a good solution?
Only if the height is truly known and will not change. For dynamic content, it is fragile.
What does clear: both do?
It forces an element to move below any left-floated or right-floated elements that come before it.
Can Flexbox replace float-based columns?
Yes. Flexbox is a much better fit for rows, columns, spacing, and alignment in modern interfaces.
Mini Project
Description
Build a small legacy-style layout with floated cards inside a container, then fix the collapsing parent using a reusable clearfix class. This project demonstrates the exact float containment problem and a practical way to solve it in real CSS.
Goal
Create a floated card row whose parent correctly wraps around its children without using a fixed height.
Requirements
- Create a parent container with a visible border or background.
- Add at least three child cards floated to the left.
- Show that the parent would collapse without a fix.
- Apply a reusable clearfix class to the parent.
- Keep the layout responsive enough for normal text content.
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.
CSS :not() Selector for Excluding a Class or Attribute
Learn how to use the CSS :not() selector to target elements that do not have a specific class or attribute, with examples and common mistakes.
Can HTML Checkboxes Be Readonly? Understanding readonly vs disabled in HTML Forms
Learn why HTML checkboxes do not support readonly, how disabled differs, and practical ways to prevent changes while still submitting values.