Why does this CSS margin-top style not work?

前端 未结 12 1965
醉梦人生
醉梦人生 2020-11-21 04:54

I try to add margin values on a div inside another div. All works fine except the top value, it seems to be ignored. But why?

What I expected:
<

12条回答
  •  抹茶落季
    2020-11-21 05:12

    What @BoltClock mentioned are pretty solid. And Here I just want to add several more solutions for this problem. check this w3c_collapsing margin. The green parts are the potential thought how this problem can be solved.

    Solution 1

    Margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).

    that means I can add float:left to either #outer or #inner demo1.

    also notice that float would invalidate the auto in margin.

    Solution 2

    Margins of elements that establish new block formatting contexts (such as floats and elements with 'overflow' other than 'visible') do not collapse with their in-flow children.

    other than visible, let's put overflow: hidden into #outer. And this way seems pretty simple and decent. I like it.

    #outer{
        width: 500px;
        height: 200px;
        background: #FFCCCC;
        margin: 50px auto;
        overflow: hidden;
    }
    #inner {
        background: #FFCC33;
        height: 50px;
        margin: 50px;
    }
    

    Solution 3

    Margins of absolutely positioned boxes do not collapse (not even with their in-flow children).

    #outer{
        width: 500px;
        height: 200px;
        background: #FFCCCC;
        margin: 50px auto;
        position: absolute; 
    }
    #inner{
        background: #FFCC33;
        height: 50px;
        margin: 50px;
    }
    

    or

    #outer{
        width: 500px;
        height: 200px;
        background: #FFCCCC;
        margin: 50px auto;
        position: relative; 
    }
    #inner {
        background: #FFCC33;
        height: 50px;
        margin: 50px;
        position: absolute;
    }
    

    these two methods will break the normal flow of div

    Solution 4

    Margins of inline-block boxes do not collapse (not even with their in-flow children).

    is the same as @enderskill

    Solution 5

    The bottom margin of an in-flow block-level element always collapses with the top margin of its next in-flow block-level sibling, unless that sibling has clearance.

    This has not much work to do with the question since it is the collapsing margin between siblings. it generally means if a top-box has margin-bottom: 30px and a sibling-box has margin-top: 10px. The total margin between them is 30px instead of 40px.

    Solution 6

    The top margin of an in-flow block element collapses with its first in-flow block-level child's top margin if the element has no top border, no top padding, and the child has no clearance.

    This is very interesting and I can just add one top border line

    #outer{
        width: 500px;
        height: 200px;
        background: #FFCCCC;
        margin: 50px auto;
        border-top: 1px solid red;
    
    }
    #inner {
        background: #FFCC33;
        height: 50px;
        margin: 50px;
    
    }
    

    And Also

    is block-level in default, so you don't have to declare it on purpose. Sorry for not being able to post more than 2 links and images due to my novice reputation. At least you know where the problem comes from next time you see something similar.

提交回复
热议问题