CSS: CSS Float & Clear

🗓 Last updated: 2026-11-30👤 Written by: Casey✓ Reviewed by: MOTO🏷 Category: CSS Tutorial

Float was CSS's primary layout method before Flexbox and Grid. While modern layouts prefer Flexbox/Grid, understanding float is still important — you'll encounter it in legacy codebases.

Float Properties Quick Reference

Property Common Values Purpose
float left, right, none Float direction
clear left, right, both, none Clear float effects

Properties in this section: float left right clear

1. Float Property

float makes elements "float" to one side of their container, with other content wrapping around them.

▶ Example

▶ Try it Yourself

(1) Float Behavior

▶ Example

▶ Try it Yourself

2. Float Problems: Height Collapse

When all children are floated, the parent "forgets" to wrap them:

▶ Example

▶ Try it Yourself

Result: Parent border hugs the top, children overflow — because floated children don't occupy document flow space.


3. Clearing Floats

(1) Using clear Property (Traditional)

▶ Example

▶ Try it Yourself

(2) Parent overflow

▶ Example

▶ Try it Yourself

Uses BFC (Block Formatting Context) behavior.

The standard, recommended method:

▶ Example

▶ Try it Yourself

4. Modern Alternative: Flexbox

Most float layouts can now be replaced with Flexbox or Grid:

▶ Example

▶ Try it Yourself

Properties in this section: float left right clear

❓ FAQ

Q Is float still used for layouts?
A Not recommended. Float was designed for text wrapping, not layouts. Flexbox and Grid have perfectly replaced float layouts. Only use float for "image + text wrap" effects — that's its correct modern use case.
Q What is clearfix and when do I need it?
A clearfix is a classic hack for clearing float effects. Add ::after { content: ""; display: block; clear: both; } to the parent container to make it "wrap" floated children. You'll need it maintaining legacy code with collapsed parents; with Flexbox or Grid, clearfix is unnecessary.
Q What to do about float parent height collapse?
A Height collapse happens because floated elements leave the document flow — the parent can't detect their height. Solutions: set overflow: hidden on parent, use clearfix, or switch to Flexbox layout.
Q What's the difference between float and flex, and which should new projects use?
A float pulls elements out of normal flow to drift sideways while text wraps around them — a typesetting feature; flex is one-dimensional elastic layout where alignment, wrapping, and spacing are first-class, and the container fully controls children. Except for wrapping text around images, new projects should use flex/grid: no height collapse, no clearfix, clearer intent.
Q What does clear: both mean — and what about clear: left and right?
A clear goes on the block element that follows floated elements, declaring "no floated elements allowed on my left/right/either side of this line". clear: both is the most common — regardless of left or right floats, start a new line. Note clear is written on the affected element, not on the floated one itself.
Q How do I make an image float left with text wrapping around it?
A Three lines: img { float: left; margin-right: 12px; margin-bottom: 8px; } and text wraps naturally. Key points: give the image margin or text touches it; to force a paragraph below the wrap, add clear: both to that paragraph. This is float's irreplaceable modern use case.
Q What are the ways to clear floats, and which is recommended?
A Four common moves: parent overflow: hidden (shortest, but clips overflow), clearfix pseudo-element (classic, no side effects), display: flow-root (the modern standard — its semantics are literally "create a new BFC"), or switching to flex/grid entirely (fixes the root cause). New code prefers flow-root or flex; use clearfix when maintaining legacy code.
Q Two floats should sit side by side, but the second drops to the next line — why?
A When floated widths sum beyond the container's usable width, the second drops. Common triggers: missing border-box so padding inflates real width, default gaps between images, or a narrower container than expected. Set box-sizing: border-box on both, then verify total width ≤ 100%; leave headroom with percentage widths like width: 48%.
Q Text after a floated element doesn't wrap — it just sits below. What's happening?
A Check clear first: if that text has clear set, it deliberately starts a new line instead of wrapping. Another case — text lands below instead of beside the float because the parent already has clearfix or overflow: hidden wrapping the floats, so later content flows under naturally. Use clear to control where wrapping stops.

📖 Summary

📝 Exercises

  1. Create an image-and-text page with the image floating left and text wrapping around it.
  2. Use float to implement a "three-column layout" — left 200px, right 200px, center adaptive.
  3. Demonstrate "height collapse" and solve it with at least three methods (clearfix, overflow, pseudo-element).
  4. Rewrite the float three-column layout using Flexbox and compare code differences.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏