When a child element is added with margin-top, it will be added to the parent element. The element margin-top

Source: Internet
Author: User

When a child element is added with margin-top, it will be added to the parent element. The element margin-top

Suppose we have the following code:

<Div id = "father" style = "height: 400px; width: 400px; background: # e4393c;">
<Div id = "child" style = "background: green; height: 100px; width: 100px; margin-top: 40px;"> </div>
</Div>

The results are as follows:

The sub-element's margin-top does not separate the sub-element from the parent element, but the effect is equivalent to adding a margin-top to the parent element;

The solution is as follows:

1. Modify the height of the parent element and add the padding-top style simulation (padding-top: 1px ;)

2. Add overflow: hidden to the parent element;

3. float: left (available but not recommended)

4. Add border (border: 1px solid transparent) to the parent Element)

5. Declare absolute positioning for the parent or child element

6. add content generation to the parent element # father: before {content: ''; display: table };

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.