Why doesn't top: 0 work on absolutely-positioned elements relative to body?

前端 未结 5 617
难免孤独
难免孤独 2020-12-31 04:36

You can see in the code below that the h1 pushes down the body and the absolutely-positioned block .absolute does not stick to the top. But you als

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-31 04:40

    It's because collapsing margins. How to disable it, you could read in some articles/answers, for example, this.
    You are using overflow: hidden for .wrapper, and it disables collapsing margins for this block. From the specification:

    Margins of a box with ‘overflow’ other than ‘visible’ do not collapse with its children's margins.

    But it seems like overflow: hidden doesn't work for , because if I set
    height: 0/max-height: 0 it doesn't work too:

    body {
        height: 0;
        max-height: 0;
    }
    Some test text

    I think it's because (from the specification):

    UAs must apply the 'overflow' property set on the root element to the viewport. When the root element is an HTML "HTML" element or an XHTML "html" element, and that element has an HTML "BODY" element or an XHTML "body" element as a child, user agents must instead apply the 'overflow' property from the first such child element to the viewport, if the value on the root element is 'visible'.

    That's why margins between and first

    collapse (from MDN):

    Parent and first/last child
    If there is no border, padding, inline content, or clearance to separate the margin-top of a block with the margin-top of its first child block, or no border, padding, inline content, height, min-height, or max-height to separate the margin-bottom of a block with the margin-bottom of its last child, then those margins collapse. The collapsed margin ends up outside the parent.

    That's why margins between and don't collapse (from the specification):

    Margins of the root element's box do not collapse.

    I also noticed that if has default background-color and has specified background-color + margin, the background color fills the entire space of , for example:

    html, body {
        height: 100%;
    }
    
    body {
        margin: 20px;
        background-color: yellow;
    }

    But if you set background-color to , it works like a normal block, for example:

    html, body {
        height: 100%;
    }
    
    html {
        background-color: red;
    }
    
    body {
        margin: 20px;
        background-color: yellow;
    }

    I summary, I recommend you to use other approaches to disable collapsing margins, or add another wrapper for , for example:

    body {
        margin: 0;
        padding: 0;
    }
    
    .body-wrapper {
        position: relative;
        overflow: hidden;
        background-color: silver;
    }
    
    .absolute {
        position: absolute;
        top: 0;
        left: 0;
        width: 50px;
        height: 50px;
        background-color: darkblue;
    }
    
    .wrapper {
        position: relative;
        width: 50%;
        height: 200px;
        overflow: hidden;
        background-color: yellow;
    }
    
    .wrapper > .absolute {
        background-color: darkcyan;
    }

    Some title

    Some title

提交回复
热议问题