What is float?

float is a CSS property that removes an element from the normal document flow and pushes it to the left or right side of its container. Text and other inline content then wraps around it.

Originally, float was designed for a single purpose: wrapping text around images in magazine-style layouts. However, developers abused it for page layouts, which led to all sorts of hacks and complications. Today, flexbox and grid are the modern, cleaner way to build layouts โ€” but floats are still the best choice for their original purpose: text wrapping.

Key point:
float: left or float: right takes an element out of normal flow. Text wraps around it. float: none (the default) keeps the element in normal flow.

float: left and float: right

The two main float values are left and right:

  • float: left โ€” Pushes the element to the left edge of its container. Content flows to the right of it.
  • float: right โ€” Pushes the element to the right edge of its container. Content flows to the left of it.
  • float: none โ€” The default. The element stays in normal flow.
float-example.html Floating an image left

When you float an element, it:

  1. Gets removed from the normal document flow
  2. Moves to the left or right edge of its container
  3. Becomes as wide and tall as its content (unless you explicitly set width/height)
  4. Text and inline content wraps around it

Try it yourself: Float an image left

Edit the code below. Float an image left with text wrapping, then try floating a second image right. See how the text flows around both.

โœ๏ธ Try It Yourself โ€” Image Float
Preview

The clear property

clear stops an element from wrapping around floated elements. It forces the element to move below any floated content.

The values are:

  • clear: left โ€” Don't wrap around floats on the left
  • clear: right โ€” Don't wrap around floats on the right
  • clear: both โ€” Don't wrap around any floats (most common)
  • clear: none โ€” The default. Allow wrapping.
clear-example.html Using clear to stop wrapping

The clearfix hack

When a parent container has floated children, it can collapse (shrink vertically) because floats are removed from the flow. The clearfix hack forces the parent to expand and contain its floated children.

clearfix-example.html Using ::after pseudo-element to clearfix

The clearfix works by:

  1. Using the ::after pseudo-element to insert content after the container
  2. Setting display: table to create a block-level box
  3. Using clear: both to clear the float

This forces the parent container to expand vertically to contain its floated children. An older alternative is using overflow: auto on the container, but ::after is cleaner.

Try it yourself: Clearfix in action

See the difference between a container without clearfix (floated children collapse it) vs with clearfix (container expands).

โœ๏ธ Try It Yourself โ€” Clearfix
Preview

Float-based layouts vs. flexbox and grid

Before flexbox (2015) and grid (2017), developers had to use floats to build multi-column layouts. It worked, but it was awkward and required lots of hacks.

layout-comparison.html Float layout vs flexbox layout

Why flexbox and grid are better for layouts:

  • No clearfix needed โ€” Children don't collapse the parent
  • Alignment is easier โ€” Center, space-between, space-around, etc. all built-in
  • Responsive design is simpler โ€” Change direction or wrap with one property
  • No unwanted text wrapping โ€” Floats can cause text to wrap unexpectedly
  • Better semantics โ€” The CSS clearly expresses intent
Best practice:
Use flexbox or grid for page layouts. Use float only for text wrapping around images.

When are floats still useful?

Despite being "old," floats are still the best choice for one scenario: wrapping text around images. This is their original and most elegant use case.

๐Ÿ“ฐ
Common Use Case
Blog posts and magazine articles

When you want an image in the middle of text, with the text flowing around it, float is perfect. It's simple, intuitive, and requires no layout hacks.

Example: Medium.com, Wikipedia, most news sites.

In all other cases โ€” page layouts, component layouts, grids โ€” use flexbox or grid. They're designed for that job and make your CSS cleaner.

Challenge: Build a Magazine Article

Task: Create a blog article layout using floats:

  • Float an image left at the start of the article
  • Write a paragraph of text that wraps around it
  • Add a "pull-quote" (highlighted quote) floated right in the middle
  • Use the clearfix hack to ensure the article container expands properly
  • Add a footer with clear: both so it sits below everything

Hint: Start with an article container with the clearfix hack (::after with clear: both). Float the opening image left, the pull-quote right, and let the paragraph text flow naturally around both. Style the pull-quote with a border or background to make it stand out.

Key takeaways

  • float removes an element from normal flow and lets text wrap around it.
  • float: left and float: right are the two main values.
  • clear stops an element from wrapping around floats. clear: both is most common.
  • The clearfix hack uses ::after with clear: both to make a parent expand around floated children.
  • Floats are legacy for layouts. Use flexbox or grid instead for page structure.
  • Floats are perfect for text wrapping around images. This is their original and best use case.

Test your knowledge

Question 1 of 5
What does float: left do?
Question 2 of 5
What property stops content from wrapping around a floated element?
Question 3 of 5
What is the clearfix hack used for?
Question 4 of 5
What is the modern replacement for float-based page layouts?
Question 5 of 5
When are floats still the best choice?
0
/5
Great work! You understand float and clear.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You just mastered CSS float and clear.
Imagine what you'll build next.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’