CSS margin Merging issues

Source: Internet
Author: User

Today, I stumbled across the problem of the merger of the outer margin, so I studied it. Take a note here.

  The so-called margin merging, refers to when two vertical margins meet, they will form an outer margin. The height of the merged margin is equal to the greater of the two of the height in which the merged margins occur.

For example, here are two blocks of elements:

<class= "Box1"></div><   class= "Box2"></div>

The styles given here are:

. Box1 {    width:150px;     height:150px;     background:Gray;    } . Box2 {    width:150px;     height:150px;     background:lightblue;}

First look at the effect:

  

expected, and then give. Box1 a downward margin of 20px, the result is:

  

No problem, OK, then I'll give. Box2 an upward margin of 10px, here's the problem.

  

Will find that the spacing between the two elements has not changed, why is this? This is because they have a margin merge, because the outer margin of the first block element is greater than the margin of the second block element, and the margin between the two is subject to the margin of the first block element.

Here I change the code a little bit, and it becomes this way.

<class= "Box1">    <class= " Box2 "></div></div>

The style changes to this.

 .box1  { width : 150px ; : 150px ;  background : lightblue ;  Margin-top : 20px ;} .box2  {: 100px ; : 100px ;  Background-color :  LightGreen ;} 

At this time the effect is believed that everybody knows, but if I want to let. Box2 the parent element of the distance has a certain top margin what should I do? Maybe someone will say very simple, I give it directly to a margin (margin-top) Ah, sorry, no, then someone will say that I give it a padding (padding-top) it? Sorry, or not, this will only change the height of the element, then what should be done, in fact, it is very simple, is to give its parent element an upper padding (padding-top) on the line, of course, here its parent element's total height will change, at this time you will be based on the specific situation to set its height , the specific situation of specific analysis.

 

  Summarize:

1. When two vertical margins meet, they will form an outer margin. The height of the merged margin is equal to the greater of the two of the height in which the merged margins occur.

2. Margin merging is only possible with the vertical margin of the block box in the normal document stream. Margins between inline boxes, floating boxes, or absolute positioning are not merged.

3. When a block element is nested inside another block element, and you want the child block element to have a certain top margin from its parent element, it can be resolved by giving its parent an upper padding (padding-top).

CSS margin Merging issues

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.