简体   繁体   中英

Why margin of child div doesn't collapse with parent margin div

I'm trying to learn when margins are collapsing.

I followed this article: What's the Deal with Margin Collapse? and still don't understand why the following margins don't collapse.

Which rule did I break?

 .container1 { background-color: red; border-style: solid; border-color: black; margin-top: 10em; } .container2 { background-color: green; border-style: solid; border-color: purple; margin-top: 10em; } 
 <div class="container1"> <div class="container2"> aaaaa </div> </div> 

According to the Cascading Style Sheets (CSS) - The Official Difinition from W3.ORG, they said:

Two margins are adjoining if and only if:

....

  • no line boxes, no clearance, no padding and no border separate them

...

In your case, try to remove the border-style: solid; like this:

    .container1 {
        background-color: red;
        border-color: black;
        margin-top: 100px;
    }

    .container2 {
        background-color: green;
        border-color: purple;
        margin-top: 100px;
    }

The collapsed margin will happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM