When the parent element sets min-height: 100%, the child element will not inherit the height.
P粉517814372
P粉517814372 2023-08-21 23:25:12
0
2
390

I found a way to make a div container take up at least the full height of the page, by setting min-height: 100%;. However, when I add a nested div and set height: 100%;, it does not extend to the height of the container. Is there any way to fix this problem?


html, body { height: 100%; margin: 0; } #containment { min-height: 100%; background: pink; } #containment-shadow-left { height: 100%; background: aqua; }
Hello World!


P粉517814372
P粉517814372

reply all (2)
P粉799885311

Addheight: 1pxin the parent container. Works in Chrome, FF and Safari.

    P粉903052556

    This is a reported webkit (chrome/safari) bug, child elements of a parent element with minimum height cannot inherit the height attribute:https://bugs.webkit.org/show_bug.cgi?id= 26559

    Apparently Firefox is also affected (cannot test in IE at this time)

    Possible solutions:

    • Add position:relative
    • in #containment
    • Add position:absolute
    • in #containment-shadow-left

    This bug will not show up when the inner element has absolute positioning.

    Please seehttp://jsfiddle.net/xrebB/

    Edited on April 10, 2014

    Since I'm currently working on a project that really requires a parent container withmin-heightand child elements to inherit the container height, I did a little more research.

    First of all:I'm no longer sure if the current browser behavior is actually a bug. The CSS2.1 specification says:

    If I set min-height on the container, I'm notexplicitlyspecifying its height - so my element should get anautoheight. This is exactly what Webkit - and all other browsers - do.

    Secondly, the solution I found:

    If I set the container element todisplay:tableand useheight:inherit, it behaves like giving it amin-heightof 100% Exactly the same. And - more importantly - if I set the child element todisplay:table-cell, it will inherit the container element's height perfectly - whether it's 100% or more.

    Full CSS:

    html, body { height: 100%; margin: 0; } #container { background: green; display: table; height: inherit; width: 100%; } #content { background: red; display: table-cell; }

    mark:

    content

    Seehttp://jsfiddle.net/xrebB/54/.

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!