๐ What Is clear?
The clear property is used to control the behavior of an element in relation to floated elements. When elements are floated using float: left or float: right, they may cause layout overlaps.clear ensures that an element is forced to move **below floated elements**.
Note
โ Prevents layout overlap
โ Commonly used in clearfix techniques
๐ฆ Syntax
Basic Syntax
clear: none | left | right | both | inline-start | inline-end;๐ Clear Values Explained
| Value | Meaning |
|---|---|
| none | Default. Allows the element to be next to floats. |
| left | Element must move below any left-floating elements. |
| right | Element must move below any right-floating elements. |
| both | Element must move below both left & right floats. |
| inline-start | Avoid floats at block-start side (depending on writing mode). |
| inline-end | Avoid floats at block-end side. |
๐ 1. Basic Example โ Prevent Overlapping Floats
Float + clear usage
img {
float: right;
width: 150px;
}
p.clear {
clear: right;
}The paragraph with clear: right will move below the floated image.
๐ 2. Clearing Both Sides
Clear both
footer {
clear: both;
background: #eee;
padding: 1rem;
}Ensures the footer never overlaps floating elements above.
๐ 3. Classic Clearfix Technique
When a container has floated children, its height collapses. The "clearfix" adds an invisible block after the container to clear floats.
Clearfix hack
.clearfix::after {
content: "";
display: block;
clear: both;
}Add class="clearfix" to any parent container.
๐ 4. Using Clear with Writing Modes
inline-start and inline-end adapt based ondirection: rtl or vertical writing modes.
inline-aware clear
.box {
clear: inline-start;
}๐งช Real-World Examples
1๏ธโฃ Blog Image with Wrapped Text
Avoid floating image overlap
img.blog-img {
float: left;
margin: 0 1rem 1rem 0;
}
.more-content {
clear: left;
}2๏ธโฃ Multi-Column Legacy Layout Fix
Old layout floats
.column {
float: left;
width: 33%;
}
.clearfix::after {
content: "";
display: block;
clear: both;
}3๏ธโฃ Promo Box Beside an Image
Promo box
.promo-box {
float: right;
width: 200px;
}
.text-section {
clear: right;
}โ ๏ธ Limitations of clear
- โ Only affects floated elements โ not normal block flow
- โ Clearfix is a workaround for float layout issues
- โ Modern layout engines (Flexbox/Grid) rarely require clear
- โ Overuse can lead to awkward spacing
Note
๐ฅ Summary
The clear property is essential when working with floated elements. It prevents overlapping, restores document flow, and is a key part of classic clearfix techniques. Although float-based layouts are outdated, clear remains useful for blog layouts and text-wrapping designs.
Learn more โMDN Docs ๐