CSS Float & Clear
Master the float and clear properties for text wrapping and legacy layouts. Understand when to use floats and modern alternatives.
What do float and clear do in CSS?
float pushes an element to the left or right and lets text and inline content wrap around it, like an image in a magazine article. clear stops an element from sitting beside floats and moves it below them. For page layouts, use Flexbox or Grid instead; floats are now mainly for wrapping text around images.
img.article-photo {
float: left;
margin: 0 1rem 1rem 0;
}
.footer { clear: both; }
/* Contain floats inside a parent */
.container { display: flow-root; }Float & Clear Demo
Float Left/Right
float: left;
float: right;
float: none; /* Default */Elements float to left or right side of container
Clear Property
clear: left;
clear: right;
clear: both;
clear: none; /* Default */Prevents elements from wrapping around floated elements
Clearfix Technique
.clearfix::after {
content: "";
display: table;
clear: both;
}Force parent containers to contain floated children
Modern Alternatives
display: flex;
display: grid;
/* Use instead of float */Flexbox and Grid are preferred over float for layout
Code Editor
<article>
<img src="https://picsum.photos/id/237/140/140" alt="A black puppy" width="140" height="140">
<p>Floated images let text wrap around them, like in a magazine. This paragraph flows around the picture on the left. Keep typing to see the wrap continue below the image.</p>
<p class="after">clear: both starts below the float.</p>
</article>Preview
Understanding Float
Float was originally designed to wrap text around images, similar to print media. It removes elements from normal document flow while still affecting surrounding content.
Float was heavily used for layouts before Flexbox and Grid. While modern methods are preferred for layouts, float is still the best choice for text wrapping.
Float Values
float: left
Element floats to the left side, content flows around its right side
float: right
Element floats to the right side, content flows around its left side
float: none
Default value - element stays in normal document flow
The Clear Property
Clear controls how elements behave next to floated elements, forcing them to appear below rather than beside floated content.
Clear Values
- clear: left - Clear left floats only
- clear: right - Clear right floats only
- clear: both - Clear all floats
- clear: none - Don't clear (default)
Common Use Cases
- • Force content below floated images
- • Separate sections in layouts
- • Prevent unwanted text wrapping
- • Create visual breaks in content
The Clearfix Problem
When all children of a container are floated, the container collapses to zero height because floated elements are removed from normal document flow.
Classic Clearfix Solution
.clearfix::after {
content: "";
display: table;
clear: both;
}The modern approach uses display: flow-root, which creates a new block formatting context.
.modern-clearfix {
display: flow-root;
}When to Use Float
Good Use Cases
- • Text wrapping around images
- • Drop caps in articles
- • Pull quotes in content
- • Simple sidebar layouts (legacy)
- • Maintaining older codebases
Modern Alternatives
- • Flexbox: For component layouts
- • Grid: For page layouts
- • Position: For overlays
- • Multi-column: For text columns
- • Shape-outside: For complex text wrapping
CSS Float and Clear — step by step
float: left and float: right
float removes an element from normal flow and shifts it to the left or right edge, letting text and inline content wrap around it.
🎯 Quick Check
What causes a parent container to collapse to zero height?
CSS Float and Clear: summary
Key points
- A floated element is taken out of normal flow, but text and inline content still wrap around it.
- A parent with only floated children collapses to zero height. Fix it with
display: flow-rooton the parent. clear: left | right | bothmoves the element below any earlier floats on that side.- Floats don't work on flex or grid items; the
floatproperty is ignored there.
Common mistakes
- Building whole page layouts with floats and clearfix hacks when Flexbox or Grid is simpler and more robust.
- Forgetting to contain floats, so the parent's background or border collapses.
Frequently asked questions
›What is a clearfix?
A clearfix is a technique to make a parent contain its floated children. The modern way is display: flow-root on the parent. The older hack used ::after { content: ""; display: table; clear: both; }.
›Should I still use float in CSS?
Only for wrapping text around an image or a pull quote. For columns and layouts, use Flexbox or Grid.
›What does clear: both do?
It moves the element down until it's below every earlier left and right float, so nothing floats beside it.