CSS Layout - clear and clearfix
The clear Property
clear property specifies what
elements can float beside the cleared element and on which side.
clear property can have one of the
- none - Allows floating elements on both sides. This is default
- left - No floating elements allowed on the left side
- right- No floating elements allowed on the right side
- both - No floating elements allowed on either the left or the right side
- inherit - The element inherits the clear value of its parent
The most common way to use the
property is after you have used a
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):
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
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:
You will learn more about the
::after pseudo-element in a later chapter.