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

Floated Left

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

Float Left
This element has clear: left applied. It appears below the left-floated element instead of wrapping around it.

Using Clear: Right

Similarly, clear: right prevents an element from being adjacent to right-floated elements.

Example: Clear Right

Float Right
This element has clear: right applied. It clears the right-floated element and positions itself below it.

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

Left Float
Right Float
This element uses clear: both. It clears all floated elements and appears below both the left and right floated boxes.

Interactive Example: Clear Property Demonstration

Interactive Clear Property Demo

Box 1
Box 2
Box 3
Clearable Element – Use the buttons below to apply different clear values and see the effect.



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

Main Content
This is the main content area with some floated elements.
Sidebar
This sidebar is floated to the right.
Footer – This footer uses clear: both to appear below all floated content.

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

Image

Section 1: This content wraps around the floated image.

Chart

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

A
B

Notice how the red border container collapsed because it only contains floated elements.

Solution: Clearfix

A
B

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

Main Content
Flexbox automatically handles layout without needing floats or clear.
Sidebar
No clearing needed!

CSS Grid Alternative

Using CSS Grid

Main Content
CSS Grid provides even more control over layout.
Sidebar
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.