Problems and countermeasures: CSS margin collapse (collapse)

Source: Internet
Author: User

From: http://blog.chinaunix.net/uid-22414998-id-3138656.html

 

For the following simple code:

<!DOCTYPEHTML PUBLIC"-//W3C//DTD HTML 4.0 Transitional//EN">

If you think it should be like this:

That's wrong. The result is as follows:

Because there is a margin collapse in CSS, that is, the border collapse or overlap. For the upper and lower Divs, the preceding Div's margin-bottom and the following Div's margin-top will collapse, that is, the maximum value in the upper and lower margin will be taken as the display value, so in this sense:The designers of CSS and browsers hope that we can only set one of the top or bottom margin blocks if there are two parallel content blocks in the layout..

However, the parent Div's inner div is explained in another CSS convention, that is:For an element with block-level sub-elements, if the element does not have a vertical border or fill, the height is the distance between the top and bottom border edge of its sub-element.. So for the Code:

<Div
Class = "father"> </div> the height of the father div is 0, because it cannot support the content of the div. If it changes:
<Div
Class = "father"> I am here. </div> the height is the height of the text, because the text is holding the div. In other words, a div and its sub-Div pay special attention to the vertical border or fill, which is like a pot with a pot in it. Can you hold the pot inside? It mainly depends on the lid, vertical border or fill is the "lid ". Therefore, there are at least two solutions: Conclusion: To solve the problem of margin cllapse on the top of parent and child Div, you need to set the parent Div: 1. border, of course, you can set the border to transparent;Border: 1 pxsolidtransparent
  1. Or
  2. Border-top: 1 pxsolidtransparent

2. Add padding to the parent Div, or at least add padding-top;
In addition, you can also use over-flow to write to the parent Div:Over-flow: hidden;

Hope to help you.

 

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.