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.
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!
- 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.
- Building navbar layouts (Use
display: flex). - Multi-column grid cards (Use
display: grid). - Centering elements vertically (Use Flexbox/Grid).
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!
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!
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:
Interactive Floats Lab
Floats & Clearfix
Toggle float direction and clearfix to see how surrounding text wraps.
Try these
- Float left vs right and watch text wrap.
- Disable clearfix to see parent collapse.
- Set float to none to restore normal flow.
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.
.float-box { float: left; width: 160px; }
.clearfix { overflow: auto; }<div class="clearfix"> <div class="float-box">Floated card</div> <p>Lorem ipsum dolor sit amet...</p> </div>
10. Flexbox Visualizer
Main axis, cross axis, justify-content, align-items, flex-grow, and gap.