On this page9 sections
Lecture 017: Float & Clear
| Category | 4: Layout Basics |
| Lecture | 017 of 057 |
| Estimated video length | 10 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
floatpushes an element to the left or right and lets text wrap around it, like a photo in a magazine. Before Flexbox and Grid, floats were also used for whole page layouts. Today that's their main job again: wrapping text around images.
Learning Objectives
- Float elements left or right so text wraps around them
- Stop wrapping with
clear - Fix collapsed containers with
display: flow-root(the modern clearfix) - Know when to use Flexbox or Grid instead
1. Floating an Image
Example
<style>
img.left { float: left; margin: 0 14px 8px 0; border-radius: 8px; }
</style>
<p>
<img class="left" src="https://picsum.photos/id/1062/120/120" alt="A dog" width="120" height="120">
Hunza's apricot trees blossom in spring, turning the valley pink and white. Visitors travel
from all over the country to see it. The text flows around the floated image, then carries
on underneath it once it's past the bottom. This is the kind of layout magazines and
newspapers use all the time.
</p>| Value | Effect |
|---|---|
left | Element moves left; content wraps on its right |
right | Element moves right; content wraps on its left |
none | Default: no floating |
2. clear: Start Below the Float
Example
<style>
.box { float: left; width: 90px; height: 90px; margin-right: 10px; background: #74c0fc; }
.after { clear: left; background: #ffe066; padding: 6px; }
</style>
<div class="box"></div>
<p>This paragraph wraps beside the box.</p>
<p class="after">This one has clear: left, so it starts below the box.</p>clear can be left, right or both.
3. The Collapsed-Parent Problem
A parent that holds only floated children has nothing to measure, so it collapses to zero height.
Example
<style>
.wrap { border: 3px solid #e03131; margin-bottom: 30px; }
.wrap.fixed { border-color: #2f9e44; display: flow-root; }
.item { float: left; width: 80px; height: 60px; margin: 6px; background: #b197fc; }
</style>
<div class="wrap"><div class="item"></div><div class="item"></div></div>
<div class="wrap fixed"><div class="item"></div><div class="item"></div></div>The red box has collapsed; the green one wraps its floats, thanks to display: flow-root.
Older code uses a "clearfix" for the same job:
.clearfix::after { content: ""; display: table; clear: both; }4. Floats Side by Side
Example: three floated columns (the old way to make a layout):
<style>
.row { display: flow-root; }
.col { float: left; width: 33.33%; box-sizing: border-box; padding: 10px; color: white; }
</style>
<div class="row">
<div class="col" style="background: #1971c2;">Column 1</div>
<div class="col" style="background: #2f9e44;">Column 2</div>
<div class="col" style="background: #e8590c;">Column 3</div>
</div>It works, but Flexbox (Lecture 049) and Grid (Lecture 051) do it with less fuss, plus equal heights and easy alignment.
Common Beginner Mistakes
1. Floats for page layout today. Use Flexbox or Grid; floats are for wrapping text.
2. Forgetting to clear. Later content creeps up beside the float.
3. Collapsed parents. Add display: flow-root to the container.
4. No margin on the float. Text presses right up against the image.
Practice Questions
- Float an image to the right with 10px of space on its left.
- What does
clear: bothdo? - Why does a parent with only floated children collapse?
- Which one line stops it collapsing?
- What's the main job of
floattoday?
Practice Answers
img { float: right; margin-left: 10px; }- The element moves below any floated elements on either side.
- Floats are taken out of normal flow, so the parent has no content to size itself around.
display: flow-root;on the parent.- Wrapping text around images or small boxes.
Key Takeaways
float: left | rightmoves an element aside and wraps content around it.clearmakes content start below floats.display: flow-rootstops containers collapsing around floats.- Use Flexbox and Grid for layout; floats for text wrapping.