Look at a newspaper or magazine and you'll see photos with text flowing neatly around them. On the web, that effect comes from the float property. A floated element is pushed to the left or right side of its container, and the text and inline content that follow wrap around it.
Floats have an interesting history. Before flexbox and grid existed, developers used floats to build entire page layouts: columns, sidebars and navigation bars. That was always a workaround, and it came with odd problems such as parent boxes collapsing to zero height. Today you should use flexbox and grid for layouts, and keep float for what it was designed for: wrapping text around images and pull quotes.
You'll still meet floats in older code, in WordPress themes and in email templates, so it pays to understand them. In this lesson you'll learn float, clear, why floated children collapse their parent, the classic clearfix, and the modern fix, display: flow-root.
How float works
img.left { float: left; margin: 0 16px 8px 0; }float: leftmoves the element to the left edge;float: rightto the right edge.float: noneis the default.- The text after it wraps around the floated element's side.
- Add a margin on the side facing the text so words don't touch the image.
- A floated element becomes block-level, so you can give it a width even if it's an inline element like a
<span>. - A floated element without a width shrinks to fit its content.
clear: stopping the wrap
Sometimes you want an element to start below a float instead of beside it, such as the next heading in an article. That's what clear does:
| Value | The element moves below… |
|---|---|
clear: left | any left floats before it |
clear: right | any right floats before it |
clear: both | all floats before it |
clear: both is the one you'll use most often.
The collapsing parent problem
Floated elements are taken out of the normal flow for their parent's height. If a box contains only floats, the browser thinks it's empty and its height collapses to zero. Backgrounds and borders on the parent then disappear, and following content creeps up beside the floats.
There are two fixes:
/* modern fix: one line, no side effects */
.container { display: flow-root; }
/* the classic clearfix, still common in older code */
.clearfix::after { content: ""; display: table; clear: both; }display: flow-root creates a new block formatting context, which simply means the box contains its floats. overflow: auto also works, but it can add unwanted scrollbars or clip shadows.
Shaping the wrap with shape-outside
Text usually wraps around a float's rectangular box. With shape-outside, text can follow a circle or other shape:
.avatar { float: left; width: 120px; height: 120px; border-radius: 50%; shape-outside: circle(50%); }It's a nice touch for portraits, round logos and editorial designs, and it only affects floated elements.
CSS Float Syntax
Float and clear
.photo { float: right; width: 40%; margin: 0 0 1em 1em; }
.next-section { clear: both; }
.container { display: flow-root; }
.portrait { float: left; shape-outside: circle(50%); }CSS Float Summary
float: leftorfloat: rightpushes an element to a side, and text wraps around it.- Add margin on the text side so words don't touch the floated element.
clear: bothmoves an element below any floats before it.- A parent with only floated children collapses; fix it with
display: flow-root. - Use flexbox and grid for page layouts, and float for wrapping text.
CSS Float Examples
Each example runs in the page. Edit the code and press Run to see what changes.
Wrapping text around an image
Change left to right on line 2 and run again.
<article>
<img src="https://picsum.photos/seed/garden/200/160" alt="Raised vegetable beds" width="200" height="160">
<p>Raised beds warm up faster in spring, drain well after heavy rain and keep paths free of mud. Fill them with a mix of topsoil and compost, and plant close together so the leaves shade out weeds. In summer, water in the early morning so the soil stays cool through the day. A single bed one metre by two can grow salad leaves, herbs, beans and courgettes for a small family.</p>
</article>article img {
float: left;
margin: 0 16px 8px 0;
border-radius: 8px;
max-width: 45%;
height: auto;
}
article { font-family: sans-serif; line-height: 1.6; }Line-by-line explanation
- Line 2The image moves to the left and the paragraph flows around it.
- Line 3Right and bottom margins keep text away from the photo.
- Line 5On narrow screens the image never takes more than 45% of the width.
Fixing a collapsed parent with flow-root
The first box has only floated children, so its border and background collapse. The second uses display: flow-root.
<div class="box broken">
<div class="tag">float</div><div class="tag">float</div>
</div>
<div class="box fixed">
<div class="tag">float</div><div class="tag">float</div>
</div>.box {
border: 3px solid #16a34a;
background: #dcfce7;
margin-bottom: 40px;
padding: 4px;
}
.tag {
float: left;
padding: 10px 16px;
margin: 4px;
background: white;
font: 600 14px sans-serif;
}
.fixed { display: flow-root; }Line-by-line explanation
- Lines 1 to 6The green box should surround the tags.
- Line 9Floated tags don't count towards the first box's height, so it shrinks to just its padding.
- Line 16
flow-rootmakes the second box wrap its floats properly.
Clearing before a new section
<img class="pic" src="https://picsum.photos/seed/bike/120/120" alt="Bicycle" width="120" height="120">
<p>A short intro paragraph.</p>
<h3>Next section</h3>
<p>This heading should start below the photo.</p>.pic {
float: right;
margin: 0 0 8px 12px;
border-radius: 50%;
shape-outside: circle(50%);
}
h3 {
clear: both;
}
body { font-family: sans-serif; }Line-by-line explanation
- Line 2The round photo floats to the right.
- Line 5Text follows the circle instead of the square box.
- Line 9The heading moves below the float instead of squeezing beside it.
Common CSS Float Mistakes
Using floats for a page layout
.sidebar { float: left; width: 30%; }
.main { float: left; width: 70%; }.page { display: grid; grid-template-columns: 30% 1fr; gap: 24px; }Grid gives you gaps, equal heights and no clearing problems.
Forgetting margin on the text side
img { float: left; }img { float: left; margin-right: 16px; margin-bottom: 8px; }Without a margin, text touches the image edge and is hard to read.
Adding empty clearing divs
/* <div style="clear: both"></div> added after every float group */.container { display: flow-root; }Empty markup just for layout is old practice. flow-root fixes the parent without extra HTML.
CSS Float in Real Projects
Where floats still appear
News sites and blogs float images and pull quotes inside articles. WordPress's alignleft and alignright classes are floats. HTML emails still lean on floats and tables because many email apps have limited CSS support. When you work on older codebases, such as Bootstrap 3 sites, you'll see .clearfix and .pull-right classes everywhere.
CSS Float Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Float the photo to the right.
.photo {
: right;
}
float takes left, right or none.What happens to text after a floated image?
Which property makes a heading start below earlier floats?
A container holds only floated boxes and its background has vanished. What's the modern fix?
The gallery's grey background has collapsed because its images are floated, and the caption sits beside the images instead of below. Fix both.
<div class="gallery">
<div class="thumb"></div><div class="thumb"></div>
</div>
<p class="caption">Two photos from the trip.</p>.gallery { background: #e5e7eb; padding: 6px; }
.thumb { float: left; width: 80px; height: 80px; margin: 4px; background: #60a5fa; }
.caption { clear: none; }- Gallery wraps its floats
- Caption clears floats
- Thumbs are still floated
CSS Float Quiz
5 questions to check what you learned.
What is float mainly used for today?
- Whole page layouts
- Centring elements
- Wrapping text around images and quotes
- Animations
What does clear: left do?
- Moves the element below any left floats before it
- Removes the element
- Floats the element left
- Clears its text
Why does a parent with only floated children collapse?
- Floats are invisible
- Parents can't have backgrounds
- The browser has a bug
- Floats are taken out of the normal flow for the parent's height
What does the classic clearfix use?
- An extra span
- JavaScript
- A negative margin
- An ::after pseudo-element with clear: both
What does shape-outside: circle(50%) change?
- The image becomes round
- Text wraps around a circle instead of the float's rectangle
- The float moves to the centre
- It clears the float
CSS Float Challenge
A magazine-style article
- Float
.quoteto therightwith awidthof40% - Give
.quoteamargin-leftof16px - Make
.articlecontain its floats withdisplay: flow-root - Make
.footerclear all floats
<div class="article">
<blockquote class="quote">"Good soil is the start of everything."</blockquote>
<p>Start with the soil. Dig in plenty of compost each autumn and the worms will do the rest of the work over winter.</p>
</div>
<p class="footer">Written by the garden team.</p>.article { width: 500px; font-family: sans-serif; background: #fefce8; }
.quote { margin: 0; padding: 12px; background: #fde68a; font-style: italic; }
- Quote floats right
- Quote is 40% wide
- Quote has a left margin
- Article contains its floats
- Footer clears floats