Skip to content
Back to Concepts
CSS UniverseTrack 2: GeometryFloats & Clearfix
Legacy & Editorial Typography · Track 2 CapstoneCore Concept 09

Floats & Clearfix

The newspaper article effect. How text flows gracefully around images, why floating elements cause parent heights to collapse, and how to tame them with modern display: flow-root.

1. The Purpose of Floats

Why Floats Were Invented (And Why NOT To Use Them For Layouts)

Floats were designed for one specific job: wrapping text around an image or pull-quote in an editorial article. Before Flexbox and Grid arrived, web developers hijacked floats to build entire multi-column page layouts—leading to fragile code and countless clearfix bugs!

✅ Use Floats For:
  • Wrapping article text around an editorial photo.
  • Floating a large decorative first-letter drop cap.
  • Pull quotes floating to the margin of a blog post.
❌ NEVER Use Floats For:
  • Building navbar layouts (Use display: flex).
  • Multi-column grid cards (Use display: grid).
  • Centering elements vertically (Use Flexbox/Grid).
2. The Zero-Height Parent Collapse

Why Does the Parent Box Collapse?

When an element floats, it is taken out of normal block flow. If a parent container has only floated children, the parent collapses to 0px height because it thinks it is empty!

float:
Floated Photofloat: left

In the heart of the CSS cosmos, elements drift through the document river. When a photo is floated to the left, typography bends around its rectangular perimeter just like in a classic print magazine.

Notice how text seamlessly fills the remaining inline space. Without containment, the parent border would snap above the floated image!

✅ Parent is properly containing the floated image with modern display: flow-root.
3. The Modern Solution: display: flow-root

Say Goodbye to Micro-Clearfix Hacks

In older tutorials, you will see messy pseudo-elements used to clear floats. Today, modern CSS has a built-in one-liner that creates a clean Block Formatting Context:

/* Old Hacky Way (Avoid): */
.container::after {
content: "";
display: table;
clear: both;
}
/* Modern Clean Standard (Use This): */
.container {
display: flow-root;
}
Instantly establishes a new BFC containing all internal floats!
4. Floats Lab Workbench

Interactive Floats Lab

Interactive Lab

Floats & Clearfix

Toggle float direction and clearfix to see how surrounding text wraps.

Float: left · Clearfix: on

Try these

  • Float left vs right and watch text wrap.
  • Disable clearfix to see parent collapse.
  • Set float to none to restore normal flow.
Floated card

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras porttitor, quam a vulputate volutpat, elit ante condimentum magna, nec ultrices ligula nisi nec leo. Vivamus placerat magna vitae dui.

Donec euismod, nisl eget consectetur tempor, nisl nunc aliquet nibh, id ultrices nisl nunc eget lorem.

CSS
.float-box { float: left; width: 160px; }
.clearfix { overflow: auto; }
HTML
<div class="clearfix">
  <div class="float-box">Floated card</div>
  <p>Lorem ipsum dolor sit amet...</p>
</div>
Track 2 Complete! Next: Track 3

10. Flexbox Visualizer

Main axis, cross axis, justify-content, align-items, flex-grow, and gap.

Begin Track 3: Flexbox