CSS Float
float pushes an element to the left or right side of its container and lets text and inline content wrap around it — just like a picture in a magazine article. Floats were once used for entire page layouts; today Flexbox and Grid do that job, but floats remain the right tool for wrapping text around images.
float values
| Value | Effect |
|---|---|
none (default) |
No floating |
left |
Element moves to the left; content flows on its right |
right |
Element moves to the right; content flows on its left |
inline-start / inline-end |
Left/right depending on writing direction |
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Georgia, serif; line-height: 1.5; max-width: 420px; }
.pic { width: 90px; height: 90px; border-radius: 12px; background: linear-gradient(135deg, #ff8a65, #ba68c8); }
.left { float: left; margin: 4px 14px 6px 0; }
.right { float: right; margin: 4px 0 6px 14px; background: linear-gradient(135deg, #4dd0e1, #81c784); }
</style>
</head>
<body>
<p><span class="pic left"></span>This square is floated left. The paragraph's text wraps around its right side and continues underneath once it is past the bottom of the square. Floats are perfect for this kind of magazine-style layout.</p>
<p><span class="pic right"></span>This one floats right, so the text hugs its left side instead. Notice the margins we add on the side facing the text so the words don't touch the shape.</p>
</body>
</html>Tip: A floated element automatically becomes block-level, so you can give a floated
<span>a width and height.
Floating several elements
Multiple elements floated in the same direction line up next to each other until they run out of space, then wrap to the next line.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.tile { float: left; width: 80px; height: 60px; margin: 5px; color: white; display: grid; place-items: center; border-radius: 6px; }
.tile:nth-child(3n+1) { background: #ef5350; }
.tile:nth-child(3n+2) { background: #42a5f5; }
.tile:nth-child(3n) { background: #66bb6a; }
</style>
</head>
<body>
<div class="tile">1</div><div class="tile">2</div><div class="tile">3</div><div class="tile">4</div>
<div class="tile">5</div><div class="tile">6</div><div class="tile">7</div><div class="tile">8</div>
</body>
</html>clear
clear tells an element not to sit next to floats; it moves down below them.
| Value | Moves below |
|---|---|
left |
Left floats |
right |
Right floats |
both |
All floats |
none |
Nothing (default) |
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.f { float: left; width: 100px; height: 80px; background: #ffca28; margin-right: 10px; }
.note { background: #e1f5fe; padding: 6px; }
.cleared { clear: both; background: #f8bbd0; }
</style>
</head>
<body>
<div class="f"></div>
<p class="note">I wrap beside the float.</p>
<p class="note cleared">I have clear: both, so I start below the float.</p>
</body>
</html>The collapsing-container problem
Floats are taken out of normal flow, so a parent containing only floats has zero height — its background and border collapse. There are two fixes:
display: flow-rooton the parent (modern, clean).- The older clearfix hack: a
::afterpseudo-element withclear: both.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.box { border: 3px solid #3949ab; background: #e8eaf6; margin-bottom: 70px; padding: 4px; }
.float { float: left; width: 70px; height: 50px; background: #3949ab; margin: 4px; }
.fixed { display: flow-root; margin-bottom: 10px; }
</style>
</head>
<body>
<div class="box"><div class="float"></div><div class="float"></div>broken: parent collapses</div>
<div class="box fixed"><div class="float"></div><div class="float"></div>fixed with flow-root</div>
</body>
</html>The clearfix version looks like this:
.clearfix::after {
content: "";
display: table;
clear: both;
}When not to use float
For rows of cards, sidebars and columns, use Flexbox or Grid: they handle equal heights, alignment and spacing much more easily. Reserve float for wrapping text around images and pull quotes, and shape-outside if you want text to follow a curve:
.round { float: left; width: 120px; height: 120px; border-radius: 50%; shape-outside: circle(); }Test yourself
What happens to text next to an element with float: right?
- It goes below the element
- It wraps around the element's left side
- It is hidden
- It floats too
Which property makes an element move below preceding floats?
- float: none
- clear: both
- display: block
- overflow: visible
What is the modern way to make a parent contain its floated children?
- height: auto
- display: flow-root
- position: relative
- float: none