Margin-top problem between parent element and child element

Source: Internet
Author: User
To the child element box a vertical margin margin-top, the parent element box will also go down the value of margin-top, encounter this problem friends can refer to this article may have unexpected harvest hack:
The parent element's box contains a child element box, a vertical margin margin-top to the child element box, the parent element box also goes down the value of margin-top, and the margins of the child and parent elements are not changed.

HTML code :

The code is as follows:

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

CSS style :

The code is as follows:

. 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, add padding-top style simulation (padding-top:1px; common)
2. Add Overflow:hidden for parent element; style (perfect)
3. Declare floats for parent or child elements (float:left; available)
4. Add border for parent element (border:1px solid transparent available)
5. Absolute positioning for parent element or child element declaration

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.