TL;DR — CSS clear is for indicating whether HTML elements should move below the floating elements that come before it. In other words, clear specifies which sides of a floating element should not float.
Clearing floats
The CSS clear
controls the behavior of the floating element by preventing the overlapping of consecutive elements over the floating element.
The value of the property clear
specifies the side on which the floating element is not supposed to float. The values of clear
property can be none
, left
, right
, both
, inherit
, inline-start
, and inline-end
.
In the example, we make CSS clear floating elements on the left:
Without clear
With clear
Note: when you specify the same direction for CSS clear and float, the element moves below the floated element.
What clearfix is
When you need to make CSS clear floats, it is useful to apply CSS clearfix. For instance, when containers have only floated elements, they do not wrap around them and collapse. The CSS clearfix makes sure that the container wraps floated items by clearing children elements automatically.
Here is the syntax of CSS clearfix:
#container::after {
content: "";
display: block;
clear: both;
}
Note: the CSS clearfix works with the ::after pseudo element.