CSS Float
In this page:
selector {
float: left | right | none;
}
.clearfix::after {
content: "";
clear: both;
display: table;
}
The Floating Concept
The float property pushes an element to the left or right of its container, allowing surrounding inline text and inline elements to wrap around it. This property was originally designed to format text wrapping around images in news articles.
- Use float: left or float: right to wrap text cleanly around media illustrations.
- Accepted values:
- left — floats the element to the left
- right — floats the element to the right
- none — no float (default)
- inline-start | inline-end — direction-aware floats
- clear — left | right | both | none stops wrapping around floats
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: The Floating Concept
<style>
img {
float: left;
margin-right: 10px;
}
</style>
<div>
<img src="https://placehold.co/100x100" alt="Floated image">
<p>Text wraps around the floated image</p>
</div>
The Collapsed Parent Problem
Because floated elements are removed from the normal page flow, parent containers do not calculate their height, collapsing to 0 and causing subsequent elements to run under the floated items.
Example: The Collapsed Parent Problem
<style>
.parent {
border: 2px solid red;
}
.child {
float: left;
background: lightblue;
padding: 8px;
}
</style>
<div class="parent">
<div class="child">Floated child collapses the parent's height</div>
</div>
Clearing Floats with clear
The clear property stops elements from wrapping around preceding floated elements. By applying clear: left, clear: right, or clear: both, you instruct the browser to push the element down below any floated items.
- Use clear: both on subsequent headers or footers to keep them from wrapping around floated columns.
- Accepted values:
- none — may sit beside floats (initial value)
- left — below any left floats
- right — below any right floats
- both — below floats on both sides
- inline-start | inline-end — direction-aware versions
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Clearing Floats with clear
<style>
.box {
float: left;
background: lightblue;
padding: 8px;
}
.footer {
clear: both;
background: lightgray;
padding: 8px;
}
</style>
<div class="box">Floated box</div>
<div class="footer">Pushed below the floated box</div>
The Modern clearfix Hack
The clearfix hack is a standard CSS design pattern used to clear floated elements. By applying a pseudo-element (:after) to the parent container, you can instruct it to clear floats automatically without adding extra, empty div tags to your HTML.
Example: The Modern clearfix Hack
<style>
.clearfix {
border: 2px dashed #999;
}
.clearfix::after {
content: "";
display: block;
clear: both;
}
.child {
float: left;
background: lightblue;
padding: 8px;
}
</style>
<div class="clearfix">
<div class="child">Floated</div>
</div>
Floats vs. Modern Layout Grids
Before Flexbox and Grid were introduced, developers had to use float: left to build multi-column website layouts. While floats are still highly useful for text-wrapping designs, modern layouts should use Flexbox or Grid for column grids.
Example: Floats vs. Modern Layout Grids
<style>
.old-way {
float: left;
width: 50%;
background: lightblue;
padding: 8px;
box-sizing: border-box;
}
.modern-way {
display: flex;
}
.modern-way div {
background: lightgreen;
padding: 8px;
}
</style>
<div class="old-way">Legacy float column</div>
<div class="modern-way">
<div>Flexbox column</div>
</div>
- Forgetting to clear floated elements, causing parent containers to collapse and subsequent content to overlap.
- Using floats to build complex, multi-column website layouts instead of modern Flexbox or Grid.
- Omitting width settings on floated block elements, causing them to expand to 100% and stack vertically instead of floating.
- The float property pushes elements to the left or right of their container, allowing surrounding text to wrap around them.
- Floated elements are removed from the natural page flow, which can cause parent containers to collapse.
- Use overflow: auto or the clearfix hack to clear floated elements and contain parent heights cleanly.
The float and clear properties have absolute native support across all modern and legacy web browsers.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: