On this page9 sections
  1. Learning Objectives
  2. 1. Floating an Image
  3. 2. clear: Start Below the Float
  4. 3. The Collapsed-Parent Problem
  5. 4. Floats Side by Side
  6. Common Beginner Mistakes
  7. Practice Questions
  8. Practice Answers
  9. Key Takeaways

Lecture 017: Float & Clear

Category4: Layout Basics
Lecture017 of 057
Estimated video length10 min
TargetCSS3 · any modern browser
LevelBeginner

float pushes 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

html
<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>
ValueEffect
leftElement moves left; content wraps on its right
rightElement moves right; content wraps on its left
noneDefault: no floating

2. clear: Start Below the Float

Example

html
<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

html
<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:

css
.clearfix::after { content: ""; display: table; clear: both; }

4. Floats Side by Side

Example: three floated columns (the old way to make a layout):

html
<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

  1. Float an image to the right with 10px of space on its left.
  2. What does clear: both do?
  3. Why does a parent with only floated children collapse?
  4. Which one line stops it collapsing?
  5. What's the main job of float today?

Practice Answers

  1. img { float: right; margin-left: 10px; }
  2. The element moves below any floated elements on either side.
  3. Floats are taken out of normal flow, so the parent has no content to size itself around.
  4. display: flow-root; on the parent.
  5. Wrapping text around images or small boxes.

Key Takeaways

  • float: left | right moves an element aside and wraps content around it.
  • clear makes content start below floats.
  • display: flow-root stops containers collapsing around floats.
  • Use Flexbox and Grid for layout; floats for text wrapping.