Margin-top between a CSS parent element and a child element

Source: Internet
Author: User

The parent element box contains a child element box that gives the child element box a vertical outer margin margin-top, and the parent element box goes down the margin-top value, while the child element and the parent element's margins do not change.

HTML code:

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

CSS style:

. Box1{height:200px;width:200px;background:gray;}
. box2{height:100px;width:100px;background:gold;margin-top:50px;}

Workaround:

1, modify the height of the parent element, increase padding-top style simulation (padding-top:1px; common)
2. Add Overflow:hidden to the parent element; style (perfect)
3, for the parent element or child element declaration float (float:left; available)
4, add border for the parent element (border:1px solid transparent available)
5. Declare absolute positioning for parent element or child element


in some browsers, when the elements in the layer set Margin-top, the elements in the layer is not margin, but the parent layer appears margin-top, which often makes people very puzzling it.

Solutions include:

1. Add to Parent layer: Overflow:hidden;
2. Add to Parent layer: border properties other than none
3. Add to Parent layer: Padding-top instead of margin-top effect.

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.