The vertical margin problem of margin

Source: Internet
Author: User

Yesterday, when the practice encountered the margin problem, to the body of the sub-element div set margin-top:50px, why the DIV element does not open the body (that is, the top of the div from the body 50px)??? Instead, the Div, together with the body, was pulled down, body distance

The code structure is as follows:

html{width:100%;height:100%;background:red;} Body{margin:0;padding:0;width:100%;height:100%;background: #47c9af;} div{width:800px;height:200px;margin:50px Auto;background:yellow;}

Such as:

Online access to some information, know that this is the margin vertical margin merger problem.

When two vertical margins meet, they 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. You can look at the CSS margin merge to learn this basic knowledge.
In practical work, the vertical margin merging problem is common in the margin-top of the first child element to open the spacing between the parent element and the adjacent element of the parent element, and only under the standard browser (Firffox, Chrome, Opera, Sarfi) problems, IE is performing well.

If according to the CSS specification, ie "good performance" is actually a wrong performance, because IE's haslayout rendering led to this "good performance" appearance. Other standard browsers exhibit a "problematic" appearance. The reason for this problem is that , according to the specification, if a box does not have a filler (padding-top) and a top border (border-top), the top margin of the box overlaps with the top margin of the first child in its internal document flow .
Besides, the white point is: The top margin of the first child element of the parent element margin-top if a valid border or padding is not touched. The trouble of "leading" (parent element, ancestor Element) can be found in a layer. As long as the leadership to set an effective border or padding can effectively control the goal of the non-leader of the margin to prevent it leapfrog, false preach Imperial decree, their own margin when the leader of the margin execution.
The solution for vertical margin merging has been explained above, adding a border-top or padding-top to the BODY element in the parent element example to solve the problem.

Change the body style in the example above to

body{    margin:0;    padding-top:1px;/* or border-top:1px solid transparent;*/      width:100%;    height:100%;    Background: #47c9af;}

The result of the operation is as follows:

Http://wenku.baidu.com/link?url=  Knqaeypgj1butkfqwmj1jgj8tti0bktlvpal49tbhd0ccjrdhgcruvykjprooccmj6mznqm6qbo15kin1s9ahgb_pv9cfukyhethr7quxzy Reference links

The first time to write a blog, continue to refuel!

Vertical margin problem for margin

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.