Skip to main content

🌊 Float and Clear Techniques

Before flexbox and grid, floats built the entire web's multi-column layouts. Today they've stepped back to a smaller, sharper role — but understanding them is still essential for wrapping text around images, reading legacy code, and truly grasping the document flow.

🎯 Learning Objectives

By the end of this lesson, you will be able to:

  • Use float: left and float: right to make content wrap around an element
  • Explain how a floated element leaves normal flow and what that does to its container
  • Apply the clear property to push content below floats
  • Fix the container-collapse problem with clearfix and modern display: flow-root
  • Decide when float is still the right tool and when to reach for flexbox or grid instead

Estimated Time: 35–45 minutes  •  Difficulty: Beginner–Intermediate

Hands-on: Build a magazine-style article with a floated image, a pull quote, and a drop cap.

In This Lesson

What Float Really Does

The float property is one of the oldest layout mechanisms still in daily use. It was invented for a single, humble purpose: letting text wrap around an image, exactly the way a newspaper flows a column of words around a photo.

Picture dropping a cork into a stream. The cork rises and stays put on one side, and the water flows around it. A floated element behaves the same way — it's pulled to the left or right edge of its container, pinned there, and the inline content that follows curves around it.

Float was never designed to lay out whole pages. Developers cleverly bent it to that job for a decade — until flexbox and grid arrived and gave us tools built for the purpose.
graph LR A[float] --> B[left] A --> C[right] A --> D[none] B --> E[Content wraps on the other side] C --> E E --> F[Container may collapse] F --> G[Fix with clearfix / flow-root]

The Float Property

Float takes three everyday values:

  • float: none — the default; the element stays in normal flow.
  • float: left — the element hugs the left edge; following content wraps around its right side.
  • float: right — the element hugs the right edge; following content wraps around its left side.
Text wrapping around a left-floated box A square floated to the left sits against the container edge while lines of text wrap around its right-hand side. float: left
Figure 1 — The floated box sits against the left edge; text wraps beside it, then reclaims the full width once it drops below the box.
/* Float an image left and give the wrapping text some breathing room */
.article-image {
  float: left;
  margin: 0 15px 10px 0;   /* space on the right and below */
}

/* Float a pull quote to the right */
.pull-quote {
  float: right;
  width: 220px;
  margin: 0 0 10px 15px;
}

📖 What happens when you float an element

Leaves normal flow: it's lifted out of the vertical stack (though not as completely as an absolutely positioned element).

Becomes block-like: a floated element generates a block box regardless of its original display value.

Shrinks to fit: without an explicit width, it becomes only as wide as its content — unlike a normal block that fills its container.

Content wraps: the inline content that follows flows around it.

✅ The two evergreen uses of float

Text around images — the original job, still the best tool for magazine-style article layouts.

Pull quotes and asides — floating a highlighted quote lets the body text keep flowing beside it.

The Clear Property

Sometimes you want an element to stop wrapping and drop below the floats instead. That's the job of clear. It tells an element which sides must be free of floats before it may sit.

ValueEffect
clear: noneDefault — the element may sit beside floats.
clear: leftMoves down until the left side is clear of left floats.
clear: rightMoves down until the right side is clear of right floats.
clear: bothMoves below all floats — by far the most-used value.
/* A section heading that should always start below any floats above it */
.section-break {
  clear: both;
}

Float and clear are two halves of one idea: float pulls an element aside so content wraps around it, and clear is how the following content declares "no thanks, put me below all that."

The Container-Collapse Problem

Here is the single most infamous float bug. When a container holds only floated children, it collapses to zero height — because the floats have left normal flow, so the parent no longer "sees" them and stretches to fit nothing.

Collapsed container versus a contained one On the left, a parent border wraps tightly around zero height while two floated boxes spill below it. On the right, the same parent uses flow-root and properly encloses both floated boxes. Collapsed (no fix) parent, height 0 floats spill outside the parent Contained (flow-root) parent grows to hold both
Figure 2 — Left: the parent collapses because its only children are floated. Right: display: flow-root establishes a block formatting context so the parent contains its floats.

Fix 1 — The modern way: display: flow-root

The cleanest, purpose-built solution. It creates a new block formatting context, which forces the container to contain its floated descendants — no hacks, no extra markup.

.container {
  display: flow-root;
}

Fix 2 — The classic clearfix hack

For years, before flow-root had browser support, developers added an invisible generated element after the floats and cleared it. You'll still see this everywhere in older codebases:

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

Fix 3 — The overflow trick

Setting overflow to anything other than visible also creates a block formatting context. It works, but can clip shadows or dropdowns that need to escape the box, so prefer flow-root:

.container {
  overflow: auto;   /* or hidden — but watch for clipped content */
}

💡 A block formatting context (BFC) is the common thread

Clearfix, overflow, and flow-root all work by making the container establish a BFC. Once it does, it stops ignoring its floated children and grows to enclose them. display: flow-root simply says "make a BFC" out loud, with no side effects — which is why it's the recommended fix today.

Float in Modern CSS

Flexbox and grid have replaced float for page and component layout. But float hasn't disappeared — it has returned to what it was always best at.

Where float still shines

  • Text wrapping around images in article and blog content — flexbox and grid cannot do this.
  • Pull quotes and asides embedded inside flowing prose.
  • Drop caps — floating an oversized first letter so the paragraph wraps around it.
  • Maintaining legacy code that still uses float-based grids.
/* A classic drop cap */
.article p:first-of-type::first-letter {
  float: left;
  font-size: 3.2em;
  line-height: 0.8;
  margin: 0.05em 0.1em 0 0;
  font-weight: 700;
}

Advanced: shape-outside

Float gained a modern superpower with shape-outside, which lets text wrap around a shape rather than a rectangle. It only works on floated elements, so it's an extension of float rather than a replacement:

.round-image {
  float: left;
  width: 160px;
  height: 160px;
  margin-right: 15px;
  shape-outside: circle(50%);   /* text hugs the circle, not the box */
  clip-path: circle(50%);
}

⚠️ Don't build page layouts with float anymore

For rows of columns, cards, navbars, and app shells, use flexbox (one dimension) or grid (two dimensions). They give you real alignment, gaps, and source-order control that float never could — and no clearfix is ever needed.

/* The float-based three columns you'd inherit from old code... */
.legacy-columns { display: flow-root; }
.legacy-columns .col { float: left; width: 33.33%; }

/* ...become this with flexbox */
.columns { display: flex; gap: 20px; }
.columns .col { flex: 1; }

Hands-on Exercise

🏋️ Build a Magazine-Style Article

Objective: Combine float, clear, and a modern clearfix in a realistic layout.

Instructions:

  1. Create an .article with a heading and two or three paragraphs of text.
  2. Float an image (or a placeholder box) to the left of the first paragraph with a right margin.
  3. Add a pull quote floated to the right in the middle of the article.
  4. Give the first paragraph a drop cap using ::first-letter and float: left.
  5. Add a footer that uses clear: both so it always sits below every float.
💡 Hint

Wrap the whole article in a container with display: flow-root so it contains its floats. Give every floated element a margin on the side the text wraps against, so the text isn't jammed up against it.

✅ Sample solution
<article class="article">
  <h1>The Return of Float</h1>
  <div class="hero"></div>
  <p>Floats once ruled web layout...</p>
  <blockquote class="pull">"Float is perfect for pull quotes."</blockquote>
  <p>...and today they've found a sharper, smaller role.</p>
  <footer class="end">End of article</footer>
</article>
.article { display: flow-root; max-width: 640px; }
.hero {
  float: left;
  width: 160px; height: 120px;
  background: #cbd5e1;
  margin: 0 16px 8px 0;
}
.article p:first-of-type::first-letter {
  float: left;
  font-size: 3em; line-height: 0.8;
  margin: 0.05em 0.1em 0 0; font-weight: 700;
}
.pull {
  float: right;
  width: 200px;
  margin: 0 0 10px 16px;
  padding: 10px 14px;
  border-left: 4px solid #6366f1;
  font-style: italic;
}
.end { clear: both; padding-top: 12px; }

Best Practices

✅ Do

  • Use float for its intended jobs: wrapping text around images, pull quotes, and drop caps.
  • Contain floats with display: flow-root — the cleanest modern fix.
  • Add box-sizing: border-box so padding and borders don't blow out your float widths.
  • Give floated elements a margin on the wrap side so text has breathing room.

⚠️ Don't

  • Don't build new page or component layouts with float — use flexbox or grid.
  • Don't forget to contain floats, or the parent will collapse and following content will overlap.
  • Don't lean on overflow: hidden as a clearfix when the box needs visible overflow (shadows, tooltips, dropdowns).
  • Don't mix fixed pixel widths into a responsive float layout — use percentages and stack at breakpoints.

Summary & Quiz

🎉 Key Takeaways

  • float pulls an element to one edge and lets following content wrap around it.
  • A floated element leaves normal flow, becomes block-like, and shrinks to fit if it has no width.
  • clear pushes an element below floats — clear: both is the everyday choice.
  • Containers of only floats collapse; fix it with display: flow-root (modern), clearfix, or overflow.
  • Today float means text-around-images and pull quotes; flexbox and grid own real layout.

🎯 Quick Quiz

Question 1: A container holds only two floated boxes and appears to have zero height. What is the cleanest modern fix?

Question 2: Which use case is float still the best tool for in modern CSS?

Question 3: What does clear: both do?

📚 Further Reading

🚀 What's Next?

Next we zoom out to the foundation beneath floats and positioning: normal flow — the default way browsers lay out every element before you change a thing. Understanding it is what makes everything else predictable.

🎉 Nicely done!

You can now wrap, clear, and contain floats — and you know exactly when to let flexbox and grid take over.