Floats and Clearing: Wrapping Text Around Content
What float really does: shifting a box to one side so text wraps around it, the placement rules, shrink-to-fit width and blockification, containing floats with flow-root, clear and clearance, why floats are no longer for page layout, and the jobs they still do best.
kvi5hnuCode Sprout team member8 min readOn this page
float was designed for one job: putting an image to one side of a column and letting the text wrap around it, as in a magazine. For about a decade it was also the main tool for building page layouts, columns and grids, which it was never meant for. Flexbox and grid have taken over layout, but floats are still the only way to make text flow around a box, so they're worth understanding properly.
What a float does
.figure {
float: inline-start; /* or left */
inline-size: 12rem;
margin-inline-end: 1rem;
margin-block-end: 0.5rem;
}
A floated element is moved to the left or right side of its containing block, and taken out of normal flow (article 44). Two things then happen:
- Line boxes next to the float are shortened, so the text wraps around it.
- Block boxes ignore the float: their backgrounds and borders run underneath it, as if it weren't there. Only the text inside them moves aside.
That second point surprises people: a paragraph's background extends behind a floated image, while its words stop at the image's edge. A block that establishes a new formatting context, such as one with display: flow-root, behaves differently: it narrows to sit beside the float (article 44).

The outline shows the difference: on the left the paragraph's box is full width and only its lines are shortened; on the right the box itself is narrowed.
| Value | Effect |
|---|---|
none (initial) | not floated |
left, right | floats to that side |
inline-start, inline-end | floats to where lines start or end, following the writing direction (article 47) |
Floats become blocks
A floated element is always laid out as a block: a floated <span> computes to display: block, and its width, height and vertical margins work. This is called blockification. If you don't set a width, a float shrinks to fit its content, up to the width of its containing block, rather than filling the line like a normal block.
Margins on floats never collapse with anything, so a float's margins always keep their full size.
Placement rules
The browser places each float as high as it can, and as far to its side as it can, with these constraints:
- A float's top can't be higher than the top of its containing block, or than the line where it appears in the source. A float in the middle of a paragraph starts at that line.
- A float can't be higher than an earlier float.
- Floats on the same side stack next to each other. If there isn't room, a float moves down below the earlier ones.
- Left and right floats can't overlap.
So several left floats line up in a row, wrapping to a new row when the line is full, which is why floats were once used for grids. When their heights differ, a float can catch on a taller one above it, making ragged rows: one reason grid layouts with floats were fragile.

The first three panels have the same two floats; only flow-root on the parent or clear on what follows changes the result. The last panel is the ragged-row problem of float grids.
Containing floats
Floats are out of flow, so a parent's height doesn't include them. A <div> containing only a floated image has a height of zero, and the image hangs out of its bottom, overlapping whatever comes next.
The modern fix is to make the parent a new block formatting context:
.media { display: flow-root; } /* contains its floats, no side effects */
Older code uses a clearfix, a pseudo-element that clears the floats at the end of the parent:
.clearfix::after { content: ""; display: block; clear: both; }
or overflow: hidden on the parent, which also clips content and creates a scroll container. Prefer flow-root, which has worked everywhere since 2019. Flex and grid items are block formatting context roots, so a float inside a flex or grid item stays inside it. float on the items themselves is ignored.
clear
clear makes an element start below earlier floats instead of beside them:
| Value | Moves below |
|---|---|
none (initial) | nothing |
left, right | left or right floats |
inline-start, inline-end | floats on that logical side |
both | all floats |
A cleared element gets clearance: extra space above its top margin that puts its border edge just below the float. Its own top margin is counted from where it would have been, not from the float, so it often seems to disappear. In Chrome, a heading with margin-top: 20px that clears a 100px float starts exactly at the float's bottom edge, not 20px lower. To add space below a float, give the float a bottom margin.
clear only applies to block-level elements, and only clears floats in the same block formatting context: it doesn't reach floats outside a flow-root container.
body { font: 0.85rem/1.45 system-ui, sans-serif; margin: 1rem; }
.post p { background: #f4c95d; margin: 0 0 0.75rem; padding: 0.25rem; }
.cover {
float: inline-start;
inline-size: 6rem; block-size: 8rem;
margin: 0 0.75rem 0.5rem 0;
background: #2f7d4f; color: white; display: grid; place-items: center; font-weight: 700;
opacity: 0.6; /* see-through, to show the paragraph's background underneath */
}
.clear { clear: both; margin: 0.25rem 0; color: #8a3b12; }
.box { border: 2px solid #8a3b12; padding: 0.4rem; margin-block: 1rem 0.5rem; }
.cover.small { inline-size: 4rem; block-size: 4.5rem; }
.flow-root { display: flow-root; }
.box + .box { clear: both; } /* start below the float that escaped the first box */
The cover is see-through, so you can see the yellow paragraph background continue underneath it. The first box's border ends after its text, and its float hangs out below; the second box clears it. The flow-root box grows to contain its own float.
Floats today
Don't use floats to build layouts: columns, card grids, navigation bars and sidebars are simpler and more robust with flexbox (article 61) and grid (article 66), which align items, handle equal heights, and don't need clearing. Floats still do what no other layout method does:
- Wrapping text around images, figures, pull quotes and asides in long-form content.
- Non-rectangular wrapping with
shape-outside(article 55), which only works on floats. - Drop caps:
initial-letter(article 93) sizes a drop cap exactly in Chrome and, prefixed, Safari, but Firefox doesn't support it, so a floated::first-letteris still the fallback.
On narrow screens, a float that takes half the column leaves a strip of text too narrow to read. Remove the float, or limit it, below a breakpoint:
.figure { inline-size: min(40%, 14rem); }
@media (width < 30rem) { .figure { float: none; inline-size: auto; } }
Floats also don't change the reading order: screen readers and keyboard users follow the HTML source, so put a floated figure near the text it relates to.
What's current
float: inline-startandinline-end, and the matchingclearvalues, are Baseline since Chrome 118 (2023).display: flow-roothas replaced clearfix hacks for containing floats.- Floats have no effect on flex and grid items.
Common questions
Why does my paragraph's background go under the floated image?
Floats only shorten line boxes; block boxes run under them. Give the paragraph display: flow-root if the whole box should sit beside the float.
Why is my container's height zero?
Its children are floated, and floats are out of flow. Add display: flow-root to the container.
Why doesn't the margin on my cleared element work? Clearance absorbs it: the element's top margin is counted from where it would have been, not from the float. Put a bottom margin on the float instead.
Can I float elements inside a flex or grid container?
float has no effect on flex and grid items. It works on elements inside them, such as an image inside a grid item's text.
Should I use floats for a two-column layout? No. Use grid or flexbox.
Summary
floatmoves a box to one side; line boxes beside it get shorter, so text wraps, while block boxes run underneath.- Floated elements are blockified, shrink to fit without a width, and their margins never collapse.
- Floats are placed as high and as far to their side as the rules allow, and stack beside earlier floats.
- Parents don't contain floats;
display: flow-rootfixes that. clearmoves an element below earlier floats, adding clearance that absorbs its top margin.- Use floats for wrapping text, not for layout.
Practice questions
Write CSS that floats .pull-quote to the end of the line in any writing direction, at most 40% wide, with space between it and the text.
Answer
.pull-quote {
float: inline-end;
max-inline-size: 40%;
margin-inline-start: 1.5rem;
margin-block-end: 0.75rem;
}
What's the computed display of a floated <a>, and why?
Answer
block. Floats are blockified: an inline element that floats is laid out as a block, so width, height and vertical margins apply to it.
A heading has clear: both; margin-top: 2rem;, but it sits right against the floated image above it. Why, and how do you add the space?
Answer
Clearance puts the heading's border edge just below the float, and the heading's own margin is counted from where it would have been, so it's absorbed. Add a bottom margin to the float, such as margin-block-end: 2rem.
A card with a floated thumbnail ends before the thumbnail does, and the next card overlaps it. Fix the card with one declaration.
Answer
.card { display: flow-root; }
Previous: Responsive Images: srcset, sizes, picture and image-set()
Next: CSS Shapes: shape-outside, shape-margin and Non-Rectangular Text Wrap

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.