CSS clear Property
The clear property controls whether an element must be positioned below preceding floated elements on one or both sides.
When an element uses the float property, surrounding content can flow alongside it. The clear property prevents an element from appearing beside certain preceding floats by moving it downward when necessary.
It is commonly used with floated images, older float-based layouts, and elements that must begin below floated content. For modern page layouts, CSS Flexbox and Grid generally provide more direct control over element positioning.
clear Syntax
Use clear to specify which side of an element must be free of preceding floats.
selector {
clear: both;
}
The general syntax is:
selector {
clear: none | left | right | both | inline-start | inline-end;
}
No Clearing
.content {
clear: none;
}
Clear Left Floats
.content {
clear: left;
}
Clear Right Floats
.content {
clear: right;
}
Clear Both Sides
.content {
clear: both;
}
Logical Clearing
.content {
clear: inline-start;
}
CSS-Wide Keywords
.content {
clear: inherit;
}
.content {
clear: initial;
}
.content {
clear: unset;
}
.content {
clear: revert;
}
.content {
clear: revert-layer;
}
The property accepts one clearing value per declaration. It does not accept lengths, percentages, or colors.
Property Values
The clear property accepts physical and logical clearing values.
| Value | Description |
|---|---|
none |
Does not require the element to clear preceding floats. This is the initial value. |
left |
Requires the element to clear preceding left-floating elements. |
right |
Requires the element to clear preceding right-floating elements. |
both |
Requires the element to clear preceding floats on both sides. |
inline-start |
Clears preceding floats on the inline-start side, according to the writing direction. |
inline-end |
Clears preceding floats on the inline-end side, according to the writing direction. |
The logical values inline-start and inline-end are less consistently supported than the traditional physical values.
CSS-Wide Keywords
| Value | Description |
|---|---|
inherit |
Uses the computed value from the parent element. |
initial |
Resets the property to none. |
unset |
Resets the property to none because it is not inherited. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | none |
| Inherited | No |
| Applies to | Block-level elements |
| Computed value | Specified keyword |
| Animation type | Discrete |
| Percentages | Not applicable |
| Primary use | Controlling placement below preceding floated elements |
How It Works
The clear property works with elements affected by preceding floats in the same block formatting context.
Step 1: Float an Element
.floating-box {
float: left;
width: 150px;
height: 100px;
}
The floated box moves to the left, allowing subsequent content to flow alongside it.
Step 2: Add Following Content
<div class="floating-box">Floated Box</div>
<p>This paragraph can flow beside the floated box.</p>
<div class="next-section">Next Section</div>
Step 3: Clear the Float
.next-section {
clear: both;
}
The next section is moved below the preceding float when the float would otherwise occupy space beside it.
Step 4: Understand Clearance
When necessary, the browser introduces additional vertical separation, called clearance, to position the cleared element below the relevant float.
This is different from simply adding a fixed margin-top. The required separation depends on the float's position and size.
Step 5: Continue Normal Layout
After clearing, the element continues participating in normal document flow. The clear property does not remove the float or change its original float declaration.
clear: none
The none value allows an element to remain beside preceding floats when normal layout permits.
.content {
clear: none;
}
Example
<div class="float-left">Floated Box</div>
<div class="content">This content is not cleared.</div>
.float-left {
float: left;
width: 120px;
height: 80px;
}
.content {
clear: none;
}
With no clearing requirement, the content can be positioned beside the floated box.
Default Value
Because none is the initial value, elements do not require an explicit declaration unless an earlier rule has applied another clearing value.
Override an Existing Rule
.section {
clear: both;
}
.section.no-clear {
clear: none;
}
The second rule removes the clearing requirement for elements with both classes.
clear: left
The left value prevents an element from appearing beside preceding left-floating elements.
.content {
clear: left;
}
Left Float Example
<div class="left-box">Left Float</div>
<div class="content">Content below the left float.</div>
.left-box {
float: left;
width: 120px;
height: 90px;
}
.content {
clear: left;
}
The content must clear the left-floating box before continuing in normal flow.
Effect on Right Floats
The left value does not specifically require clearance below right-floating elements. A right float may still occupy space alongside the cleared element.
Typical Use
Use clear: left when only preceding left floats need to be cleared.
clear: right
The right value prevents an element from appearing beside preceding right-floating elements.
.content {
clear: right;
}
Right Float Example
<div class="right-box">Right Float</div>
<div class="content">Content below the right float.</div>
.right-box {
float: right;
width: 120px;
height: 90px;
}
.content {
clear: right;
}
The content is positioned below the preceding right float when clearance is required.
Effect on Left Floats
The right value does not specifically require clearance below left-floating elements.
Typical Use
Use clear: right when only preceding right floats need to be cleared.
clear: both
The both value requires an element to clear preceding floats on both the left and right sides.
.content {
clear: both;
}
Two Floats Example
<div class="left-box">Left Float</div>
<div class="right-box">Right Float</div>
<div class="content">Content below both floats.</div>
.left-box {
float: left;
width: 120px;
height: 80px;
}
.right-box {
float: right;
width: 120px;
height: 100px;
}
.content {
clear: both;
}
The content is positioned below both floats, including the taller right-floating box.
Common Use
The both value is useful when the direction of preceding floats is unknown or when a section must start beneath all preceding floats.
Clear Before a Footer
.page-footer {
clear: both;
}
This technique is commonly found in older float-based page layouts.
Logical Values
The inline-start and inline-end values express clearing relative to the inline direction of the text rather than fixed physical sides.
inline-start
.content {
clear: inline-start;
}
In a left-to-right horizontal writing mode, inline-start corresponds to the left side. In a right-to-left horizontal writing mode, it corresponds to the right side.
inline-end
.content {
clear: inline-end;
}
In a left-to-right horizontal writing mode, inline-end corresponds to the right side. In a right-to-left horizontal writing mode, it corresponds to the left side.
Right-to-Left Content
.rtl-container {
direction: rtl;
}
.rtl-container .content {
clear: inline-start;
}
The logical value follows the inline direction rather than always clearing the physical left side.
Compatibility Fallback
.content {
clear: left;
clear: inline-start;
}
In a left-to-right horizontal layout, the first declaration provides a physical fallback. Browsers supporting the logical value use the second declaration.
Logical clearing values have less consistent support than left, right, and both. Check compatibility before relying on them.
Clearing Floats
Clearing is useful when a block-level element must begin below floated content instead of appearing beside it.
Floated Image
<img src="sample-image.jpg" alt="Sample illustration" class="float-image">
<p>This paragraph wraps beside the floated image.</p>
<h2 class="next-heading">Next Section</h2>
.float-image {
float: left;
width: 150px;
margin: 0 15px 10px 0;
}
.next-heading {
clear: both;
}
The heading begins below the floated image rather than beside it.
Clearing Multiple Floats
.left-panel {
float: left;
width: 40%;
}
.right-panel {
float: right;
width: 40%;
}
.next-section {
clear: both;
}
Clearance Depends on Float Height
If the preceding float is already above the element's normal position, additional clearance may not be needed.
Clearing Does Not Remove a Float
The floated element remains floated. The clear declaration affects the placement of the element receiving the declaration.
Clearing Is Not Container Containment
Applying clear to a following element does not automatically make a parent container expand around its floated children. Container containment requires a different technique.
The Clearfix Technique
The clearfix technique is a traditional method for making a container account for its floated children.
The Problem
<div class="container-box">
<div class="floated-child">Floated Content</div>
</div>
.floated-child {
float: left;
width: 150px;
height: 100px;
}
Because floated elements are taken out of normal flow, a parent containing only floated children may not expand to their full height.
Add a Clearfix
.clearfix::after {
content: "";
display: block;
clear: both;
}
Apply the Class
<div class="container-box clearfix">
<div class="floated-child">Floated Content</div>
</div>
How It Works
The generated ::after pseudo-element creates a block after the floated children. Its clear: both declaration forces it below those floats, allowing the container to account for their height.
Complete Clearfix Example
.container-box {
border: 2px solid #1769aa;
padding: 10px;
}
.container-box::after {
content: "";
display: block;
clear: both;
}
.floated-child {
float: left;
width: 150px;
height: 100px;
background-color: #dcecff;
}
The clearfix technique remains useful when maintaining older float-based layouts.
Using flow-root
The display: flow-root declaration provides a modern way to contain floated children without adding a clearfix pseudo-element.
Create a New Block Formatting Context
.container-box {
display: flow-root;
}
Contain Floated Children
<div class="container-box">
<div class="floated-child">Floated Content</div>
</div>
.container-box {
display: flow-root;
border: 2px solid #1769aa;
padding: 10px;
}
.floated-child {
float: left;
width: 150px;
height: 100px;
}
Why flow-root Works
The flow-root value establishes a new block formatting context. The container includes its floated children when calculating its height.
Clearfix vs. flow-root
| Technique | Purpose |
|---|---|
clear: both |
Moves a block below preceding floats when necessary. |
| Clearfix pseudo-element | Uses a generated cleared block to make a container account for its floats. |
display: flow-root |
Creates a block formatting context that contains its floats without generated content. |
For new layouts that still require floats, display: flow-root is generally preferable to adding clearfix markup or pseudo-elements.
float vs. clear
The float and clear properties work together but serve different purposes.
| Property | Purpose |
|---|---|
float |
Positions an element to one side so surrounding content can flow alongside it. |
clear |
Controls whether an element must be placed below preceding floats. |
Float an Element
.image {
float: left;
}
Clear a Following Element
.next-section {
clear: left;
}
Use Both Properties
.image {
float: right;
width: 150px;
margin-left: 15px;
}
.next-section {
clear: right;
}
Effect on Modern Layouts
The clear property is designed for float layout. It does not move an element below neighboring flex items or grid items in the way it clears floats.
Use Flexbox or Grid alignment and placement properties for layouts built with those systems.
Practical Uses
The clear property remains useful for controlling content around floated elements.
Heading Below a Floated Image
.article-image {
float: left;
margin-right: 15px;
}
.article-heading {
clear: left;
}
New Section Below Both Floats
.new-section {
clear: both;
}
Footer Below Floated Columns
.page-footer {
clear: both;
}
Clear Only Right Floats
.content-block {
clear: right;
}
Remove an Existing Clearing Rule
.content-block {
clear: none;
}
Contain Floated Children
.float-container {
display: flow-root;
}
Legacy Clearfix
.clearfix::after {
content: "";
display: block;
clear: both;
}
Choose the technique based on whether you need to move a following element below floats or contain floats inside their parent.
clear Example
The following example compares a section without clearing to one using clear: both. Each example contains a floated box followed by a content block.
<div class="clear-demo">
<div class="demo-group">
<div class="float-box">Float</div>
<div class="content-box no-clear">clear: none</div>
</div>
<div class="demo-group">
<div class="float-box">Float</div>
<div class="content-box clear-both">clear: both</div>
</div>
</div>
.clear-demo .demo-group {
display: flow-root;
margin-bottom: 20px;
padding: 10px;
border: 1px solid #cccccc;
}
.clear-demo .float-box {
float: left;
width: 110px;
height: 75px;
padding: 10px;
box-sizing: border-box;
color: #ffffff;
background-color: #1769aa;
}
.clear-demo .content-box {
padding: 10px;
background-color: #e5f1fb;
}
.clear-demo .no-clear {
clear: none;
}
.clear-demo .clear-both {
clear: both;
}
In the first example, the content block does not clear the float, so its box can extend alongside the floated element. In the second example, clear: both moves the content block below the float.
The display: flow-root declaration on each demonstration group keeps its floated content contained within that group.
Important Considerations
Keep the following points in mind when using clear:
- Purpose: Controls placement relative to preceding floated elements.
- Initial value:
none. - Inheritance: The property is not inherited.
- Left clearing:
leftclears preceding left floats. - Right clearing:
rightclears preceding right floats. - Both sides:
bothclears preceding floats on either side. - Logical values:
inline-startandinline-endfollow the inline direction where supported. - Normal flow: A cleared element remains in normal document flow.
- Clearance: The browser adds the vertical separation needed to clear relevant floats.
- Float behavior: Clearing does not remove or change the preceding float.
- Formatting contexts: Clearing does not cross independent block formatting contexts in the same way it affects floats within one context.
- Container height: Clearing a following element is not the same as containing floated children.
- Clearfix: A cleared
::afterpseudo-element can make a container account for floated children. - flow-root:
display: flow-rootprovides a modern float-containment technique. - Flexbox and Grid: The property is not a substitute for flex or grid placement controls.
- Browser support: The traditional values are widely supported.
- Responsive layouts: Float clearing can behave differently as available space changes, so test narrow layouts.
Use clear when an element must begin below preceding floats. For containing floated children, prefer display: flow-root in modern code.
Browser Support
The clear property is widely supported in modern browsers, including Chrome, Edge, Firefox, and Safari.
The traditional values none, left, right, and both have long-established support. Logical values such as inline-start and inline-end have less consistent availability.
Widely Supported Example
.next-section {
clear: both;
}
Logical Value Feature Detection
.next-section {
clear: left;
}
@supports (clear: inline-start) {
.next-section {
clear: inline-start;
}
}
The physical value provides a fallback in a left-to-right horizontal layout, while supporting browsers can use the logical value.
For current compatibility details, visit Can I Use? and search for clear.
Specifications
The clear property is defined in the CSS 2.2 specification and further described in CSS Display Module Level 3.
These specifications describe how clearing affects block placement around preceding floated elements.
