Environment: When you set float to an element, and it’s parent’s width or height follows the element’s width or height. Then, the parent will lose its width or height and it’s called height crash. Here are some solutions.
Set the height of the parent to a fixed number. This will work but, when the child element changed, the parent element won’t change. So this solutions works but a good one.
According to the Standard of W3C, every element in the page has a hidden attribute called BFC, short for Block Formatting Context. This attribute is off by default.
Set float to parent item
This way will make the parent item lose its width, and following item will still move
Set absolute position for parent item
The same as the first one
Set inline-block
for item
Also lose width
Set the value of overflow
to a non-visible value
This works!
1 | overflow: hidden; |
If there is a relative positioned item in the parent element and the item is out of the range, you won’t be able to see it.
In IE6 or lower, the browser doesn’t support BFC, it has an attribute call hasLayout, familiar to BFC.
1 | /* IE6 */ |
Zoom means enlarge the element. Value 1 means the element is the same size as before.
Use this way to enable hasLayout.
Set width to a certain value for the parent element.
Since this element is not float, the parent height will be stretched.
Basically has no side effect.
1 | <style> |
However, this will add redundant struct in page. So this is not recommended.
:after
1 | .clearfix:after { |
In IE6, :after
is not supported, so we still need zoom
.
1 | .clearfix { |