The CSS clear
property is a fundamental tool for controlling how elements interact with floated content. When working with CSS layouts, especially those involving floated elements, understanding the clear property is essential for preventing unwanted wrapping behavior and maintaining proper document flow.
What is the CSS Clear Property?
The clear
property specifies which sides of an element cannot be adjacent to earlier floating elements within the same block formatting context. It effectively “clears” the float on one or both sides, forcing the element to move below any floated elements rather than wrapping around them.
Syntax and Values
clear: none | left | right | both | inline-start | inline-end;
The clear property accepts the following values:
- none (default): The element can be adjacent to floated elements on both sides
- left: The element cannot be adjacent to left-floated elements
- right: The element cannot be adjacent to right-floated elements
- both: The element cannot be adjacent to floated elements on either side
- inline-start: Clears floats on the start side of the containing block
- inline-end: Clears floats on the end side of the containing block
Understanding Float Wrapping Behavior
Before diving into the clear property, it’s important to understand how floated elements behave. When an element is floated, it’s removed from the normal document flow and positioned to the left or right of its container. Subsequent content then wraps around the floated element.
Example: Basic Float Behavior
This text wraps around the floated element. Notice how the content flows around the green box that’s floated to the left. This is the default behavior when no clear property is applied.
Using Clear: Left
The clear: left
value prevents an element from being positioned next to left-floated elements. The element will move below any left-floated elements in its vicinity.
Example: Clear Left
Using Clear: Right
Similarly, clear: right
prevents an element from being adjacent to right-floated elements.
Example: Clear Right
Using Clear: Both
The clear: both
value is the most commonly used and clears floated elements on both the left and right sides. This ensures the element appears below all floated elements.
Example: Clear Both
Interactive Example: Clear Property Demonstration
Interactive Clear Property Demo
Current clear value: none
Common Use Cases for the Clear Property
1. Clearing Footer Elements
One of the most common uses of the clear property is ensuring footer elements appear below floated content in the main content area.
Example: Footer Clearing
This is the main content area with some floated elements.
This sidebar is floated to the right.
2. Creating Section Breaks
The clear property is useful for creating distinct sections in a layout where you want to ensure content doesn’t wrap around floated elements from previous sections.
Example: Section Breaks
Section 1: This content wraps around the floated image.
Section 2: This section is cleared and starts fresh below the previous floated content.
The Clearfix Technique
A common problem occurs when a container holds only floated elements – the container collapses because floated elements are removed from the normal document flow. The clearfix technique solves this using the clear property.
Problem: Container Collapse
Notice how the red border container collapsed because it only contains floated elements.
Solution: Clearfix
The green border container properly contains the floated elements using a clearfix.
Modern Clearfix CSS
.clearfix::after {
content: "";
display: table;
clear: both;
}
Clear Property vs Modern Layout Methods
While the clear property remains important for understanding CSS fundamentals and maintaining legacy code, modern CSS provides better alternatives for layout:
Flexbox Alternative
Using Flexbox Instead of Float + Clear
Flexbox automatically handles layout without needing floats or clear.
No clearing needed!
CSS Grid Alternative
Using CSS Grid
CSS Grid provides even more control over layout.
Grid eliminates the need for float and clear entirely.
Best Practices and Tips
1. Use Clear: Both for Maximum Compatibility
When in doubt, clear: both
is usually the safest choice as it clears floated elements on both sides.
2. Apply Clear to Block-Level Elements
The clear property only affects block-level elements. For inline elements, you may need to change the display property first.
.clear-element {
display: block;
clear: both;
}
3. Consider Modern Alternatives
For new projects, consider using Flexbox or CSS Grid instead of float-based layouts, as they provide more predictable behavior and eliminate the need for clearing.
4. Use Clearfix for Container Collapse
When working with floated layouts, always include a clearfix to prevent container collapse.
Browser Support and Compatibility
The CSS clear property has excellent browser support across all modern and legacy browsers:
- Chrome: Full support from version 1
- Firefox: Full support from version 1
- Safari: Full support from version 1
- Internet Explorer: Full support from version 4
- Edge: Full support from version 12
The newer logical values (inline-start
and inline-end
) have more limited support and are primarily used in modern, internationalization-aware layouts.
Common Pitfalls and Troubleshooting
1. Clear Not Working on Inline Elements
Remember that the clear property only affects block-level elements. If clear isn’t working, check the element’s display property.
2. Margin Collapse Issues
Cleared elements can sometimes experience unexpected margin behavior. Use padding or border to prevent margin collapse when necessary.
3. Specificity Problems
Ensure your clear declarations have sufficient CSS specificity to override other styles that might be interfering.
Conclusion
The CSS clear property is a fundamental tool for controlling float behavior and preventing unwanted wrapping in layouts. While modern CSS layout methods like Flexbox and Grid have largely superseded float-based layouts, understanding the clear property remains important for maintaining existing code and grasping CSS fundamentals.
Key takeaways include using clear: both
for maximum effectiveness, applying clearfix techniques to prevent container collapse, and considering modern layout alternatives for new projects. With proper understanding and application, the clear property helps create more predictable and maintainable CSS layouts.
Whether you’re working with legacy code or learning CSS fundamentals, mastering the clear property will improve your ability to create robust, cross-browser compatible layouts that behave as intended across different devices and screen sizes.