Tutorials CSS Float & Clear
Ch 22 / 60
Chapter 22 Beginner 6 min read CSS

CSS Float & Clear

float was the backbone of web layouts for over a decade before Flexbox and Grid arrived. Today it is rarely used for full page layouts, but it still has an important and legitimate role — wrapping text around images. Understanding it also helps you read and fix legacy CSS code confidently.

What you'll learn
What float does and how it removes elements from flow
float: left, right, none, inherit
The parent collapse problem and why it happens
clear — left, right, both, none
The clearfix hack — modern pseudo-element approach
When to use float and modern alternatives

What Does float Do?

float takes an element out of the normal document flow and pushes it to the left or right edge of its container. Surrounding inline and text content wraps around it — block-level siblings still stack normally but their content flows around the floated element.

Partially out of flow
Floated elements are taken out of flow but still affect the layout of inline content. Block siblings ignore the float but their text flows around it.
Shrinks to content
Unless given an explicit width, a floated element shrinks to the width of its content — even if it was originally a full-width block element like a div.
Designed for text wrapping
The original and still-valid use case: wrapping text around images in articles and blog posts — exactly like print magazine layouts.
float: left
Pushes Left
Element floats to the left edge. Surrounding text wraps along the right side.
float: right
Pushes Right
Element floats to the right edge. Surrounding text wraps along the left side.
float: none
Default
No float — element stays in normal flow. Default value. Used to override inherited float.
float: inherit
Inherit
Inherits the float value from the parent element. Rarely needed in practice.

float: left & float: right — Live Demo

The image below is floated left. Text content wraps naturally around its right side — just like a newspaper article. The bottom demo shows float right:

float: left — text wraps on the right
float: left

This text flows around the floated element on the right-hand side. The floated element is pushed to the left edge of its container and the text wraps naturally along the right. This is exactly how newspapers and magazine layouts work — text flowing around images. The float continues until the text runs out or a clear property is applied. As you can see, the text occupies the remaining horizontal space beside the floated element, and once the text height exceeds the float's height, it continues below normally.

float: right — text wraps on the left
float: right

This text flows to the left of the floated element. The image is pushed to the right edge and text wraps on the left side. Notice how the orange element sits at the right while this paragraph fills the space on the left. This is the classic pull-quote style often seen in editorial magazine layouts — important content pushed to a side with text flowing around it.

CSS — float
/* Float an image left — text wraps right */
.article img {
  float:        left;
  width:        200px;
  margin:       0 16px 8px 0;  /* right + bottom gap */
}

/* Float image right — text wraps left */
.pull-quote {
  float:        right;
  width:        240px;
  margin:       0 0 8px 16px;  /* left + bottom gap */
}

/* Reset float */
.no-float { float: none; }

/* Float makes block elements shrink-wrap to content width
   unless an explicit width is set */
div         { width: 100%; }  /* before float */
div.floated { float: left; }   /* now shrinks to content */

The Parent Collapse Problem

When all children of a container are floated, the parent collapses to zero height — because floated elements are removed from flow and the parent has no in-flow children to measure. This is one of the most common float bugs.

❌ Parent collapses (broken)
child 1
child 2
Next sibling — rises up!
✓ Clearfix applied (fixed)
child 1
child 2
Next sibling — stays below ✓
Why it happens
Floated elements are removed from the normal flow. The parent container only measures the height of its in-flow children. When all children float, there are no in-flow children — so the parent's calculated height is zero. It visually "collapses" and the next sibling element moves up to fill the now-empty space.

clear — Stopping Wrap

The clear property tells an element to move below any floated elements — stopping the wrap effect for that element and everything after it.

float:left then clear:left
float: left
clear: left — drops below left float
float:right then clear:right
float: right
clear: right — drops below right float
both floats, then clear:both
float: left
float: right
clear: both — drops below all floats
CSS — clear
.clear-left  { clear: left; }   /* drops below left floats */
.clear-right { clear: right; }  /* drops below right floats */
.clear-both  { clear: both; }   /* drops below all floats */
.no-clear    { clear: none; }   /* default — no clearing */

/* Clearing with an empty element (old method — avoid) */
<div style="clear:both"></div>  /* adds empty HTML just for layout */

/* ↑ Bad practice — markup should not contain presentational empty divs */

Clearfix — Fixing the Collapse

The clearfix is a pattern applied to the parent container to force it to expand around its floated children. There have been many variants over the years — here are the main ones from oldest to most modern:

Modern clearfix (recommended)
✓ Recommended
Uses ::after pseudo-element. Clean — no extra HTML. Works everywhere.
display: flow-root
✓ Modern
Creates a new block formatting context — the cleanest single-property solution. Excellent browser support (2017+).
overflow: hidden / auto
OK but limited
Forces BFC, fixing collapse. Side effect: clips overflow content. Use only if overflow isn't needed.
Empty div (legacy)
✗ Old — avoid
Adding <div style="clear:both"></div> in HTML. Pollutes markup. Don't use.
CSS — Clearfix patterns
/* ── Method 1: Modern clearfix (most portable) ── */
.clearfix::after {
  content: '';
  display: table;   /* or display: block */
  clear:   both;
}

/* Usage */
<div class="clearfix">
  <div style="float:left">...</div>
  <div style="float:right">...</div>
</div>

/* ── Method 2: display: flow-root (cleanest modern way) ── */
.container { display: flow-root; }
/* Creates a new block formatting context — automatically
   contains floated children. No extra pseudo-elements needed. */

/* ── Method 3: overflow (works but clips content) ── */
.container { overflow: hidden; }  /* or auto */

/* ── Old method: DO NOT USE ── */
<div style="clear:both"></div>  /* empty div in HTML */

When to Use float Today

Float's legitimate use cases in modern CSS are narrow but important. For anything else, use Flexbox or Grid:

📖

Article text that wraps naturally around the floated image element, just like a newspaper or magazine article. This is float's original and still-valid purpose in modern web design.

Text wrap around image
Float's original purpose — wrapping editorial text around images in articles and blog posts.
Fig. 1

Body text flowing beside the figure and its caption as a grouped unit.

Figure + caption group
Float a <figure> element to keep image and caption together with wrapping text.
L

orem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Drop cap
Float a large first letter for typographic drop-cap effects in editorial content.
float: left
main
float: right
sidebar
Full-page layouts
Don't use float for multi-column layouts. Use Flexbox or Grid — they're cleaner, more powerful and avoid all collapse issues.

Float vs Flexbox vs Grid

Choosing the right tool for the job — float has a specific niche, but Flexbox and Grid have superseded it for almost everything else:

Task float Flexbox Grid
Text wrapping around image ✓ Best tool ✗ Not possible ✗ Not possible
Horizontal nav bar ✗ Fragile ✓ Best tool OK
Card grid layout ✗ Fragile OK (with wrap) ✓ Best tool
Centring content ✗ Hard ✓ Trivial ✓ Trivial
Two-column page layout ✗ Requires clearfix ✓ Easy ✓ Best tool
Drop cap / pull quote ✓ Best tool ✗ Not applicable ✗ Not applicable
Equal height columns ✗ Impossible ✓ Default ✓ Default
Reordering items ✗ Not possible ✓ order property ✓ grid-area
Practical advice
In new projects, use float only for text-wrapping around images and drop caps. For everything else — layouts, navigation, grids, centring — reach for Flexbox or Grid. When maintaining old CSS code that uses floats for layout, understand how clearfix works so you can debug confidently.
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
Complete reference covering float, clear, clearfix, Flexbox and CSS Grid properties.
Download PDF

Try It Yourself

The editor shows all float and clear scenarios. Try removing the clearfix class, changing float direction, or switching the image to float: right:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 22

Questions & Answers
float takes an element out of normal flow and pushes it to the left or right. The key difference from absolute: floated elements still affect the layout of inline and text content — surrounding text wraps around them. Block-level siblings ignore the float but their text content still flows around it.

position: absolute, by contrast, completely removes the element from flow — surrounding content ignores it entirely and no wrapping occurs.

Another difference: float shrinks the element to its content width unless given an explicit width. Absolute doesn't affect sibling sizing at all.

Use float when you want text to flow around an element (images in articles). Use absolute when you want precise placement without affecting any surrounding content.
The parent calculates its height based on its in-flow children. When all children are floated, they are removed from the normal flow — the parent has no in-flow children to measure, so it calculates a height of zero and visually collapses.

The visual result is the parent's background and border appear as a zero-height line (or disappear entirely), and the next sibling element rises up to where the floated children visually are.

This is the most common and frustrating float bug. Solutions include the clearfix (adding a clearing pseudo-element after the parent), using display: flow-root on the parent, or using overflow: hidden/auto on the parent to create a new block formatting context.
The modern clearfix uses a CSS ::after pseudo-element on the parent container:

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

This inserts an invisible empty element at the end of the parent. Because it has clear: both, it drops below all floated children. Because it's an in-flow element, the parent must expand to contain it — forcing the parent to grow tall enough to enclose all the floated children above.

The advantage over the old empty-div approach: no extra HTML elements — it's entirely CSS. Just add the clearfix class to the parent and the floated children are contained.

The cleanest modern alternative is display: flow-root on the parent — one property, no pseudo-element needed.
The clear property has four values:

none — default. No clearing. The element stays beside any floats normally.

left — the element drops below any left-floated elements. Right-floated elements are ignored.

right — the element drops below any right-floated elements. Left-floated elements are ignored.

both — the element drops below all floated elements, both left and right. The most commonly used value — in practice you almost always want to clear both directions.

clear is applied to the element that should not sit beside the float — typically the next sibling or a clearfix pseudo-element.
For layouts: No. Flexbox and CSS Grid have entirely superseded float for layout purposes since around 2017–2019 when browser support became universal. Float-based layouts require clearfix hacks, can't do equal-height columns, can't centre content easily, and can't reorder items responsively. Every layout task is simpler and more powerful with Flex or Grid.

For text wrapping: Yes, still the only option. The one thing float does that neither Flexbox nor Grid can replicate is making text flow around an element — the classic newspaper/magazine image-beside-text pattern. CSS shapes (shape-outside) extends this to non-rectangular shapes.

Other valid uses: drop caps (floating a large first letter), pull quotes, figures with captions.

When you encounter float-based layouts in legacy codebases, understanding clearfix helps you maintain and debug them without rewriting everything.
Multiple Choice Questions
Question 1 of 5
What happens to surrounding text when an image has float: left?
Question 2 of 5
Why does a parent container collapse to zero height when all children are floated?
Question 3 of 5
Which CSS property is used to prevent an element from sitting beside a float?
Question 4 of 5
What is the cleanest single-property modern fix for the float parent collapse problem?
Question 5 of 5
For which task is float still the best CSS tool in 2025?
Chapter Summary
  • float takes an element out of flow and pushes it left or right. Text and inline content wraps around it. Block siblings ignore it but their content still wraps around it.
  • Values: float: left, right, none (default), inherit. Floated elements shrink-wrap to content width.
  • Parent collapse — when all children float, the parent height collapses to zero. The fix is clearfix.
  • clear — prevents an element from sitting beside floats. Values: left, right, both (most used), none.
  • Modern clearfix: .clearfix::after { content:''; display:table; clear:both; } on the parent. No extra HTML needed.
  • Cleanest modern fix: display: flow-root on the parent — creates a new block formatting context in one property.
  • Use float for: text wrapping around images, drop caps, pull quotes — its original purpose.
  • Do not use float for: multi-column layouts, navigation, card grids, centring. Use Flexbox or CSS Grid instead.
~6 min read
Beginner level
Chapter 22 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel