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.
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.
When you float an element, it:
- Gets removed from the normal document flow
- Moves to the left or right edge of its container
- Becomes as wide and tall as its content (unless you explicitly set width/height)
- 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.
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 leftclear: rightโ Don't wrap around floats on the rightclear: bothโ Don't wrap around any floats (most common)clear: noneโ The default. Allow 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.
The clearfix works by:
- Using the
::afterpseudo-element to insert content after the container - Setting
display: tableto create a block-level box - Using
clear: bothto 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).
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.
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
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.
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.
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: bothso 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
floatremoves an element from normal flow and lets text wrap around it.float: leftandfloat: rightare the two main values.clearstops an element from wrapping around floats.clear: bothis most common.- The clearfix hack uses
::afterwithclear: bothto make a parent expand around floated children. - Floats are legacy for layouts. Use
flexboxorgridinstead for page structure. - Floats are perfect for text wrapping around images. This is their original and best use case.
Test your knowledge
float: left do?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 โ