Problems and countermeasures: CSS margin collapse (collapse) and margincollapse

Source: Internet
Author: User

Problems and countermeasures: CSS margin collapse (collapse) and margincollapse

 

1: <! Doctypehtml public "-// W3C // dtd html 4.0 Transitional // EN">

   2: 

   3: 

   4:     <title></title>
   5:     <metaname=""content="">
   6: <style>
   7: *{
   8:     margin:0px;
   9:     padding:0px;
  10: }
  11:  
  12: #no1{
  13:     background:#808000;
  14:     width:300px;
  15:     height:300px;
  16:     margin:100px 0 0 100px;
  17:     }
  18:  
  19: #no2{
  20:     background:#c0c0c0;
  21:     width:150px;
  22:     height:150px;
  23:     margin-left:20px;
  24:     margin-top:30px;
  25: }
  26: </style>
  27: 

  28:  
  29: <body>
  30:  
  31: <div id="no1">
  32:     <div id="no2">Span2</div>
  33: </div>
  34:  
  35: </body>
  36:  
  37: 

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..W3school also stipulates that when two vertical outer margins meet, they form an outer margin. The height of the merged margin is equal to the larger of the two merged margins.

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:

   1: <div class="father"></div>

The height of the father div is 0, because the content of the div cannot be opened. If it changes:

   1: <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 ". There are at least three solutions:

1. You need to set the border for the parent div. Of course, you can set the border to transparent:

   1: border:1px solid transparent
2: or
   3: border-top:1px solid transparent

2,Add padding to parent DIV, or at least add padding-top;

   1: padding:1px
2: or
3: padding-top: 1px;/* must be greater than 0 */

 3. Add overflow: hidden to the parent DIV

   1: over-flow:hidden;

 

Source: http://www.ido321.com/387.html


Css and margin Problems

Auto is automatic, that is, the default one! The upper-left corner should be margin:-50px 0 0-50px. You can try the effect. The following four parameters are top-right bottom left, and PX is pixel.

Css problems margin

Margin: 10px
Margin-top: 10px
Margin-left: 10px
Margin-right: 10px
Margin-bottom: 10px
The distance between the current tag and the parent tag.

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.