clear

Term side of the Card.
Topic: Alignment & Float
Unit title: clear
Categories: Property
Explanation side of the Card

Explanation: The property used to control the behavior of floating elements. It ensures that elements don't wrap around floated elements by moving the element below the floating elements, often used to manage layout and text flow.

Browser compatibility:
  • The chrome browser supports this feature starting from version 1.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 1.
  • The safari browser supports this feature starting from version 1.
Availability: 

Example

Wrongcss
.sidebar {
  float: left;
}
.footer {
  clear: right;
}
Rightcss
.sidebar {
  float: left;
}
.footer {
  clear: both;
}

'clear: right' only clears right floats, so the footer still wraps beside the left-floated sidebar, while 'both' moves it below any float.

Common mistakes

  • Adding empty <div style="clear: both"> elements to fix layouts.

Key points

  • Parents of floats contain them with display: flow-root instead of an empty clearing <div>.
  • clear is set on the element that should move below the float, not on the float itself.

Interview questions

What does clear: both do?

It moves the element below any left or right floats that come before it.

What is a clearfix?

An older hack using ::after with clear: both to make a parent wrap its floats. display: flow-root replaces it.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy