Float & Clear

What You'll Learn

  • How floats remove elements from normal document flow and enable text wrapping
  • Using float: left and float: right for magazine-style layouts
  • Understanding and solving the float collapse problem
  • Using clear to control float behavior
  • Modern alternatives to floats for page layouts (Flexbox and Grid)
  • When floats are still the right tool (text wrapping and shape-outside)

Introduction to Floats

Before Flexbox and Grid, float was the primary way to create multi-column layouts. While less common today for page layouts, floats are still useful and appropriate for their original purpose: wrapping text around images and creating magazine-style layouts.

Floats remove an element from normal document flow and shift it to the left or right side of its container. Subsequent content then flows around the floated element. Understanding floats is important both for maintaining legacy code and for creating text-wrapping effects that modern layout systems don't handle as elegantly.

float: left - Text Wraps on the Right

When you float an element left, it moves to the left side of its container and subsequent content wraps around it on the right. This is the classic magazine layout where images sit on the left with text flowing alongside.

Syntax

.image { float: left; margin: 0 1rem 1rem 0; /* Add space around the image */ }

Example

<article> <img src="photo.jpg" class="image" alt="Article image"> <p>This text wraps around the floated image...</p> <p>More paragraphs continue to wrap...</p> </article>

Rendered Result:

float: right - Text Wraps on the Left

Floating an element right moves it to the right side, with content wrapping on the left. This is commonly used for pull quotes, sidebars, and images that should appear on the right side of an article.

Syntax

.pullquote { float: right; width: 40%; margin: 0 0 1rem 2rem; padding: 1.5rem; font-size: 1.3rem; font-style: italic; }

Example

<article> <blockquote class="pullquote"> "Floats are perfect for text wrapping!" </blockquote> <p>Article text flows around the pull quote...</p> </article>

Rendered Result:

The Float Collapse Problem

When all children of a container are floated, the container doesn't automatically expand to contain them. This is called float collapse and it happens because floated elements are removed from normal document flow. The container acts as if the floated children don't exist.

The Problem

<div class="container"> <div class="box float-left">Floated Box</div> <p>Some text...</p> </div> <!-- Container might not fully wrap the floated box! -->

Demonstration:

Solution 1: Clearfix (Classic Technique)

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

Solution 2: display: flow-root (Modern!)

.container { display: flow-root; /* Creates Block Formatting Context */ /* Automatically contains floats! */ }

clear: left, right, both

The clear property forces an element to move below any floated elements instead of wrapping around them. Use clear: both to ensure an element appears below all floats, regardless of direction.

Syntax

.element { clear: left; /* Clears left floats */ clear: right; /* Clears right floats */ clear: both; /* Clears all floats (most common) */ clear: none; /* Default - allows wrapping */ }

Example

<div class="container"> <div class="float-left">Float Left</div> <div class="float-right">Float Right</div> <p class="clear-both">This appears below all floats</p> </div>

Rendered Result:

Practical: Text Wrapping Around Images

This is the primary legitimate use case for floats in modern CSS. Floats allow text to wrap naturally around images, creating professional magazine-style layouts.

Syntax

.article-image { float: left; width: 250px; margin: 0 1.5rem 1rem 0; /* Right and bottom margins */ border-radius: 8px; }

Example

<article> <img src="article-photo.jpg" class="article-image" alt="Article photo"> <h2>Article Title</h2> <p>Text wraps naturally around the image, creating a professional magazine-style layout. Multiple paragraphs continue to flow...</p> <p>Additional content wraps until cleared...</p> </article>

Rendered Result:

Advanced: shape-outside

Modern CSS allows text to wrap around non-rectangular shapes using shape-outside. This only works with floated elements and creates elegant, magazine-quality text wrapping around circles, polygons, and images.

Syntax

.circle-image { float: left; width: 180px; height: 180px; border-radius: 50%; shape-outside: circle(50%); /* Text wraps around circle! */ margin: 0 1rem 1rem 0; }

Available Shapes

/* Circle shape */ shape-outside: circle(50%); /* Ellipse shape */ shape-outside: ellipse(50% 30%); /* Polygon shape */ shape-outside: polygon(0 0, 100% 0, 100% 100%); /* Use image alpha channel */ shape-outside: url(image.png);

Rendered Result:

When to Use Floats Today

Good Use Cases (Still Valid)

  • Text wrapping around images - The main legitimate use case
  • Pull quotes - Editorial/magazine-style highlighted quotes
  • Initial letter drop caps - Large decorative first letter
  • shape-outside effects - Text wrapping around non-rectangular shapes
  • Maintaining legacy code - When working with older projects

Use Modern Alternatives Instead

  • Page layouts → Use CSS Grid
  • Navigation bars → Use Flexbox
  • Card grids → Use CSS Grid
  • Sidebars → Use Grid or Flexbox
  • Centering → Use Flexbox or Grid
  • Equal-height columns → Use Grid or Flexbox

Migration Guide: Float to Modern CSS

If you're maintaining legacy code with float-based layouts, here's how to migrate to modern CSS:

Old: Float-based Sidebar Layout

/* Float-based sidebar (legacy) */ .sidebar { float: left; width: 250px; } .main { margin-left: 270px; } .container::after { content: ""; display: table; clear: both; }

New: Grid-based Sidebar Layout

/* Grid-based sidebar (modern) */ .container { display: grid; grid-template-columns: 250px 1fr; gap: 20px; } /* Simpler, more flexible, no clearfix needed! */

Old: Float-based Columns

/* Float-based columns (legacy) */ .col { float: left; width: 23%; margin-right: 2.66%; } .col:last-child { margin-right: 0; } .row::after { content: ""; display: table; clear: both; }

New: Grid-based Columns

/* Grid-based columns (modern) */ .row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 20px; } /* Auto-responsive with auto-fit */ .row { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 20px; }

Common Float Gotchas

1. Collapsed Containers

Containers don't automatically contain floated children. Use clearfix or display: flow-root.

2. Margin Collapsing

Floated elements don't participate in margin collapsing, which can cause unexpected spacing.

3. Source Order Dependency

HTML source order matters! You can't float an element above something that comes before it in the HTML.

4. Width Calculations

Percentage-based widths plus margins require careful math. Using box-sizing: border-box helps!

Key Takeaways