Parent element collapse

Source: Internet
Author: User

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; } 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.