Parent element collapse
When designing the webpage layout, it will certainly use floating, but there will also be a problem in the use process, that is, the parent element collapse problem. Let's take a look at the Code:
<!DOCTYPE html>
As shown in the figure above, the borders set for the parent element overlap. If the parent element does not contain any visible background, this issue is hard to be noticed. After we set the float attribute for the child element, the child element is out of the parent element level (that is, not at the same level), and the parent element is not set to the height, this causes the height of the parent element to collapse.
Solution: 1. Set the height of the parent Element
2. Clear floating
3. The parent element also sets the float attribute so that the parent element and the child element are at the same level again.
4. Set overflow: hidden for the parent element (the overflow: hidden attribute is also a strange feature in css, which forces the parent element to be extended to include floating elements)
Tip: There are two methods to clear floating. The first is to add a div after the last sub-element and set the CSS property clear: both. The second is to use the pseudo class in CSS, the details are as follows:
.clearfix:after{ content:''; display:block; height:0; font-size:0; clear:both; overflow:hidden; }