CSS Float & Clear
float was the backbone of web layouts for over a
decade before Flexbox and Grid arrived. Today it is rarely used for full page layouts, but it still has an
important and legitimate role — wrapping text around images. Understanding it also helps you read and fix
legacy CSS code confidently.
float does and how it
removes elements from flowfloat: left, right, none, inheritclear — left, right, both, none
What Does float Do?
float takes an element out of the normal document
flow and pushes it to the left or right edge of its container. Surrounding inline and text content
wraps around it — block-level siblings still stack normally but their content flows around the
floated element.
div.float: left & float: right — Live Demo
The image below is floated left. Text content wraps naturally around its right side — just like a newspaper article. The bottom demo shows float right:
This text flows around the floated element on the right-hand side. The floated element is pushed to the left edge of its container and the text wraps naturally along the right. This is exactly how newspapers and magazine layouts work — text flowing around images. The float continues until the text runs out or a clear property is applied. As you can see, the text occupies the remaining horizontal space beside the floated element, and once the text height exceeds the float's height, it continues below normally.
/* Float an image left — text wraps right */
.article img {
float: left;
width: 200px;
margin: 0 16px 8px 0; /* right + bottom gap */
}
/* Float image right — text wraps left */
.pull-quote {
float: right;
width: 240px;
margin: 0 0 8px 16px; /* left + bottom gap */
}
/* Reset float */
.no-float { float: none; }
/* Float makes block elements shrink-wrap to content width
unless an explicit width is set */
div { width: 100%; } /* before float */
div.floated { float: left; } /* now shrinks to content */
The Parent Collapse Problem
When all children of a container are floated, the parent collapses to zero height — because floated elements are removed from flow and the parent has no in-flow children to measure. This is one of the most common float bugs.
clear — Stopping Wrap
The clear property tells an element to move
below any floated elements — stopping the wrap effect for that element and everything after it.
.clear-left { clear: left; } /* drops below left floats */
.clear-right { clear: right; } /* drops below right floats */
.clear-both { clear: both; } /* drops below all floats */
.no-clear { clear: none; } /* default — no clearing */
/* Clearing with an empty element (old method — avoid) */
<div style="clear:both"></div> /* adds empty HTML just for layout */
/* ↑ Bad practice — markup should not contain presentational empty divs */
Clearfix — Fixing the Collapse
The clearfix is a pattern applied to the parent container to force it to expand around its floated children. There have been many variants over the years — here are the main ones from oldest to most modern:
::after pseudo-element. Clean — no extra
HTML. Works everywhere.<div style="clear:both"></div>
in HTML. Pollutes markup. Don't use./* ── Method 1: Modern clearfix (most portable) ── */
.clearfix::after {
content: '';
display: table; /* or display: block */
clear: both;
}
/* Usage */
<div class="clearfix">
<div style="float:left">...</div>
<div style="float:right">...</div>
</div>
/* ── Method 2: display: flow-root (cleanest modern way) ── */
.container { display: flow-root; }
/* Creates a new block formatting context — automatically
contains floated children. No extra pseudo-elements needed. */
/* ── Method 3: overflow (works but clips content) ── */
.container { overflow: hidden; } /* or auto */
/* ── Old method: DO NOT USE ── */
<div style="clear:both"></div> /* empty div in HTML */
When to Use float Today
Float's legitimate use cases in modern CSS are narrow but important. For anything else, use Flexbox or Grid:
Article text that wraps naturally around the floated image element, just like a newspaper or magazine article. This is float's original and still-valid purpose in modern web design.
Body text flowing beside the figure and its caption as a grouped unit.
<figure> element to keep image
and caption together with wrapping text.main
sidebar
Float vs Flexbox vs Grid
Choosing the right tool for the job — float has a specific niche, but Flexbox and Grid have superseded it for almost everything else:
| Task | float | Flexbox | Grid |
|---|---|---|---|
| Text wrapping around image | ✓ Best tool | ✗ Not possible | ✗ Not possible |
| Horizontal nav bar | ✗ Fragile | ✓ Best tool | OK |
| Card grid layout | ✗ Fragile | OK (with wrap) | ✓ Best tool |
| Centring content | ✗ Hard | ✓ Trivial | ✓ Trivial |
| Two-column page layout | ✗ Requires clearfix | ✓ Easy | ✓ Best tool |
| Drop cap / pull quote | ✓ Best tool | ✗ Not applicable | ✗ Not applicable |
| Equal height columns | ✗ Impossible | ✓ Default | ✓ Default |
| Reordering items | ✗ Not possible | ✓ order property | ✓ grid-area |
float only for text-wrapping around images and drop
caps. For everything else — layouts, navigation, grids, centring — reach for Flexbox or Grid. When
maintaining old CSS code that uses floats for layout, understand how clearfix works so you can debug
confidently.
Try It Yourself
The editor shows all float and clear scenarios. Try removing the clearfix class,
changing float direction, or switching the image to float: right:
Quick Check — Chapter 22
float takes an element out of normal flow and pushes it to the left
or right. The key difference from absolute: floated elements still affect the layout of inline
and text content — surrounding text wraps around them. Block-level siblings ignore the float
but their text content still flows around it.position: absolute, by contrast, completely removes the element from
flow — surrounding content ignores it entirely and no wrapping occurs.Another difference: float shrinks the element to its content width unless given an explicit width. Absolute doesn't affect sibling sizing at all.
Use float when you want text to flow around an element (images in articles). Use absolute when you want precise placement without affecting any surrounding content.
The visual result is the parent's background and border appear as a zero-height line (or disappear entirely), and the next sibling element rises up to where the floated children visually are.
This is the most common and frustrating float bug. Solutions include the clearfix (adding a clearing pseudo-element after the parent), using
display: flow-root on the
parent, or using overflow: hidden/auto on the parent to create a new
block formatting context.
::after pseudo-element on the parent
container:.clearfix::after {content: '';display: table;clear: both;}This inserts an invisible empty element at the end of the parent. Because it has
clear: both, it drops below all floated children. Because it's an
in-flow element, the parent must expand to contain it — forcing the parent to grow tall enough to
enclose all the floated children above.The advantage over the old empty-div approach: no extra HTML elements — it's entirely CSS. Just add the
clearfix class to the parent and the floated children are
contained.The cleanest modern alternative is
display: flow-root on the parent
— one property, no pseudo-element needed.
clear property has four values:none — default. No clearing. The element stays
beside any floats normally.left — the element drops below any left-floated
elements. Right-floated elements are ignored.right — the element drops below any right-floated
elements. Left-floated elements are ignored.both — the element drops below all floated
elements, both left and right. The most commonly used value — in practice you almost always want to
clear both directions.clear is applied to the element that should not sit beside
the float — typically the next sibling or a clearfix pseudo-element.
For text wrapping: Yes, still the only option. The one thing float does that neither Flexbox nor Grid can replicate is making text flow around an element — the classic newspaper/magazine image-beside-text pattern. CSS shapes (
shape-outside) extends this
to non-rectangular shapes.Other valid uses: drop caps (floating a large first letter), pull quotes, figures with captions.
When you encounter float-based layouts in legacy codebases, understanding clearfix helps you maintain and debug them without rewriting everything.
float: left?float still the best CSS
tool in 2025?floattakes an element out of flow and pushes it left or right. Text and inline content wraps around it. Block siblings ignore it but their content still wraps around it.- Values:
float: left,right,none(default),inherit. Floated elements shrink-wrap to content width. - Parent collapse — when all children float, the parent height collapses to zero. The fix is clearfix.
clear— prevents an element from sitting beside floats. Values:left,right,both(most used),none.- Modern clearfix:
.clearfix::after { content:''; display:table; clear:both; }on the parent. No extra HTML needed. - Cleanest modern fix:
display: flow-rooton the parent — creates a new block formatting context in one property. - Use float for: text wrapping around images, drop caps, pull quotes — its original purpose.
- Do not use float for: multi-column layouts, navigation, card grids, centring. Use Flexbox or CSS Grid instead.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.