π What Is float in CSS?
The float property allows an element to shift to the **left** or **right** of its container, letting text and inline elements wrap around it. Originally designed for magazine-style layouts (like images wrapped by text),float was later used for layout hacks before Flexbox and Grid existed.
Note
β Not recommended for full layout systems today
β Must be cleared to prevent layout breaking
π¦ Syntax
float syntax
float: left | right | none | inline-start | inline-end;π Values Explained
| Value | Description |
|---|---|
| left | Pushes element to the left |
| right | Pushes element to the right |
| none | Disables floating |
| inline-start | Float based on writing direction (LTR β left) |
| inline-end | Float opposite of inline-start |
π― Basic Example β Image Wrapped by Text
Float image left
img {
float: left;
margin: 0 1rem 1rem 0;
}The text flows around the image just like in newspapers or blogs.
π Clearing Floats
Floating elements are removed from normal document flow. That means parent containers may collapse unless you **clear** them.
β Using clear
clear examples
.footer {
clear: both;
}clear options:
- clear: left;
- clear: right;
- clear: both;
β The Clearfix Hack
A classic solution for containers collapsing due to floated children.
Clearfix hack
.clearfix::after {
content: "";
display: block;
clear: both;
}Add class="clearfix" to the parent.
π Float for Side-by-Side Elements (Old-School Layout)
Float layout
.box {
float: left;
width: 33.33%;
}Before Flexbox/Grid, floating was the only way to create multi-column layouts. Today, **avoid using floats for layout**.
π Inline-Flow Floats (Writing Modes)
inline-start float
.banner {
float: inline-end;
}Floats adjust automatically based on language direction (LTR/RTL).
π§ͺ Real-World Examples
1οΈβ£ Blog Image with Text Wrap
Blog-style float
img.blog-img {
float: right;
margin: 0 0 1rem 1rem;
}2οΈβ£ Product Info & Small Thumbnail
Product float example
.thumb {
float: left;
width: 120px;
margin-right: 1rem;
}3οΈβ£ Magazine Layout Text Box
Magazine text box
.highlight-box {
float: right;
width: 200px;
padding: 10px;
background: #f5f5f5;
margin-left: 1rem;
}β οΈ Limitations of Float
- β Not meant for full layouts (use Flexbox or Grid instead)
- β Requires clearfix methods
- β Hard to center elements
- β Doesnβt support vertical alignment
Note
π₯ Summary
The float property allows elements to shift left or right and lets surrounding text wrap around them. While historically used for full layouts, today its primary use is for text-flow effects like blog images, side notes, and magazine-style content.
Learn more βMDN Docs π