Float & Clear
What is Float?
The float property removes an element from normal flow and
pushes it to the left or right, allowing text to wrap around it. Originally
designed for magazine-style layouts.
Float Left
float: left pushes the element to the left side, with content
wrapping around the right.
Float Right
float: right pushes the element to the right side, with
content wrapping around the left.
Multiple Floats
Multiple floated elements stack horizontally until they run out of space, then wrap to the next line.
The Clear Property
clear prevents an element from wrapping around
floated elements. Values: left, right,
both, none.
Clearfix Technique
When all children are floated, the parent collapses to zero height. The clearfix hack solves this problem.
Summary
float: left- Push element left, content wraps rightfloat: right- Push element right, content wraps leftclear: both- Prevent wrapping around floats- Clearfix prevents parent collapse when all children float
- Best for: Images with text wrapping
- For layouts: Use Flexbox or Grid instead
Quick Quiz
Which property prevents an element from wrapping around floated elements?
Enjoying these tutorials?