CSS explanation and solution for setting the height of the child element 0 after the float attribute;

Source: Internet
Author: User

CSS explanation and solution for setting the height of the child element 0 after the float attribute;
1 <! Doctype html public "-// W3C // dtd html 4.01 // EN" "http://www.w3.org/TR/html4/strict.dtd"> 2

Code running result:


We found that the parent element has no height at all (the review element shows that the parent element div # container height = 0)
Analysis:
The floating float attribute disconnects the element from the current HTML document stream, so that the current HTML document does not exist as the element that sets the float attribute. Therefore, because float is set for all the five child elements, it can be viewed as the parent element # There is no content in the container. When there is no content in the div, the height is equal to 0.
Solution:

1. Set the parent element float
For example:

#container { background-color: #f1f1f1; width: 80%; margin: 20px auto; float: left; } 

2. Add an empty div after the last child element with floating settings, and clear the floating div. You do not need to set the height of the parent level or the technical parent level, which is convenient for use. However, CSS and HTML tags are added.

For example:

 <div class="clear"></div>   .clear {clear: both;}

3. Set overflow: hidden; or overflow: auto; for the parent element ;. Because the overflow: hidden attribute is equivalent to keeping the parent level close to the content, so that the content in the object can be closely followed (including the div Box Using float), thus clearing the floating.

4. Do not use float. Instead, use the following sub-elements: display: inline-table or display: inline-block.

5. Use the following pseudo elements of CSS: after

After pseudo elements (note that this is not a pseudo class, but a pseudo element, representing the most recent element after an element ). Add a clearfix class to the container of the floating element, and then add an invisible Block element to the end of the after pseudo element to clear the floating.

.clearfix:after{   content: " ";    display: block;    height: 0;    clear: both;    visibility: hidden;   }   <div id="container" class="clearfix"></div>

 

Related Article

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.