This site is mobile accessible. Press the "Tap Here" button to use a smaller font-size.

Smartphone icons created by Freepik - Flaticon

6.5.1 Clear

The clear property specifies what elements can float beside the cleared element and on which side. The clear property can have one of the following values:

The most common way to use the clear property is after you have used a float property on an element.

When clearing floats, you should match the clear to the float: If an element is floated to the left, then you should clear to the left. Your floated element will continue to float, but the cleared element will appear below it on the web page. The following example clears the float to the left. Means that no floating elements are allowed on the left side (of the div):

Example 1: float clear
div {
  clear: left;
}

The clearfix Hack

If an element is taller than the element containing it, and it is floated, it will "overflow" outside of its container:

Then we can add overflow: auto; to the containing element to fix this problem:

Example 2: clearfix hack
.clearfix {
  overflow: auto;
}

The overflow: auto clearfix works well as long as you are able to keep control of your margins and padding (else you might see scrollbars). The new, modern clearfix hack however, is safer to use, and the following code is used for most webpages:

Example 3: new clearfix hack
.clearfix::after {
  content: "";
  clear: both;
  display: table;
}

You will learn more about the ::after pseudo-element in a later chapter.