Why Margin-top is not acting on the parent element

Source: Internet
Author: User

Why Margin-top is not acting on the parent element:
As for the basic usage of the Margin-top attribute, it is simply to set the top margin of an object and see the following code example:

<!DOCTYPE HTML><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><Metaname= "Author"content= "http://www.51texiao.cn/" /><title>Ant Tribe</title><styletype= "Text/css"> * {margin:0px;padding:0px; }Div{width:100px;Height:100px;Background-color:Green;Margin-top:50px; } </style> </Head> <Body>   <Div></Div> </Body> </HTML>

The above code can set the top margin of the div to 50px, everything works well, without any problems, and then look at the next piece of code:

<!DOCTYPE HTML><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><Metaname= "Author"content= "http://www.51texiao.cn/" /><title>Ant Tribe</title><styletype= "Text/css">#parent{width:200px;Height:200px;Background-color:Red; }#children{width:60px;Height:60px;Background-color:Green;margin:0px Auto;Margin-top:50px; } </style> </Head> <Body> <DivID= "Parent">   <DivID= "Children"></Div> </Div> </Body> </HTML>

The original intention of the above code is to let the child element's top distance from the parent element 50px, but in fact did not achieve the desired effect, but the child element at the top of the parent element, and Margin-top seems to be transferred to the parent element, so that the parent element to produce the upper margin. This is actually a typical margin merge issue, but not all browsers do this, and IE6 and IE7 do not appear in this state without margin merging. The conditions for the upper margin merge to occur:
1. There is no border between the top margin of the parent element and the top margin of the child element.
2. There is no non-empty content between the top margin of the parent element and the top margin of the child element.
3. There is no padding between the top margin of the parent element and the top margin of the child element.
3. Positional properties (except static and relative), overflow (except for visible) and Display:inline-block are not set in parent and child elements.
4. Neither the parent element nor the resource is floating.
Note: The above conditions must be met before you can. The way to solve this situation is also simple, as long as it destroys one of the above situations.

The original address is: http://www.51texiao.cn/div_cssjiaocheng/2015/0501/506.html

Why Margin-top is not acting on the parent element

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.