我找到了一种方法,使一个div容器占据至少一个页面的全部高度,通过设置最小高度:100%;。然而,当我添加一个嵌套的div并设置高度:100%;,它不会延伸到容器的高度。有办法解决吗?

Html, body { 高度:100%; 保证金:0; } #控制{ 最小高度:100%; 背景:粉色; } # containment-shadow-left { 高度:100%; 背景:水; } < div id = "容器" > < div id = " containment-shadow-left " > 你好世界! < / div > < / div >


当前回答

这是在@jackocnr的评论中添加的,但我错过了。对于现代浏览器,我认为这是最好的方法。

如果容器太小,它会让内部元素填充整个容器,但如果容器太大,它会扩展容器的高度。

#containment {
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

#containment-shadow-left {
  flex: 1;
}

其他回答

为父容器添加高度:1px。工作在Chrome, FF, Safari。

在尝试了我们的之后!chrome理解,当我设置显示值为inline block时,我希望子元素的高度为100%。顺便说一句,设置浮动会导致它。

display:inline-block

更新

这行不通。解决方案是获得parentnode offsetheight,并使用它在页面的和javascript。

<script>
    SomedivElement = document.getElementById('mydiv');
    SomedivElement.style.height = String(nvleft.parentNode.offsetHeight) + 'px';    
</script>

目前实现这一目标的最佳方法是使用display: flex;。然而,当你试图支持IE11时,你可能会遇到一个问题。根据https://caniuse.com使用flexbox:

当使用min-height时,IE 11不能正确地垂直对齐项目

Internet Explorer兼容解决方案

解决方案是使用display: table;在父元素和显示元素上:table-row;对孩子双方身高:100%;作为min-height的替换:100%;

这里列出的大多数解决方案使用display: table, table-row和/或table-cell,但它们不会复制与min-height: 100%;相同的行为,即:

继承父类的计算高度(而不是继承它的height属性); 允许父节点的高度超过其min-height;

当使用这个解决方案时,行为是相同的,属性min-height是不需要的,这允许绕过bug。

解释

它工作的原因是,与大多数元素不同,使用display: table和table-row的元素将始终与它们的内容一样高。这使得它们的height属性的行为类似于min-height。

实际解决方案

html, body { height: 100px; margin: 0; } #containment { display: table; height: 100%; background: pink; width: 100%; } #containment-shadow-left { display: table-row; height: 100%; background: aqua; } #content { padding: 15px; } #demo { display: none; background-color: red; height: 200px; } #demo-checkbox:checked ~ #demo { display: block; } <div id="containment"> <div id="containment-shadow-left"> <div id="content"> <input id="demo-checkbox" type="checkbox"> <label for="demo-checkbox">Click here for overflow demo</label> <div id="demo">This is taller than #containment</div> </div> </div> </div>

已经有一段时间了,但是,如果其他人还在挣扎,我找到了我的变通办法。 我需要的父母有一个最小的高度,和孩子(这将包含几个图像/文件预览)必须占据整个父母,所以..

我简单地定义了一个最小的高度给孩子,让父母没有定义高度,所以它可以随着孩子的内容增长。

CSS

.parent{
    border: 1px solid black;
    border-radius: 5px;
}
.children{
  width: 100%;
  min-height: 100px;
}

HTML:

<div class="parent">
    <div class="children">
        some content inside
    </div>
</div>

因为谷歌员工:

这个jquery解决方案使#containment自动获取高度(by, height: auto),然后获取实际高度作为像素值分配。

<script type="text/javascript">
<!--
    $(function () {

        // workaround for webkit-bug http://stackoverflow.com/a/8468131/348841

        var rz = function () {
            $('#containment')
            .css('height', 'auto')
            .css('height', $('#containment').height() + 'px');
        };

        $(window).resize(function () {
            rz();
        });

        rz();

    })
-->
</script>