仅使用CSS填写剩余的div高度

前端之家收集整理的这篇文章主要介绍了仅使用CSS填写剩余的div高度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个div:s在一个容器中.
第一个div应该有自动高度,占用所需的空间.
第二个div应该具有容器的剩余高度.

我希望不使用Javascript这样做.

是不是只有CSS解决方案?也许通过利用display:table / display:table-cell或display:flex?

非工作的例子

<!DOCTYPE html>
<html>
<head>
<title>Stretch</title>
<style type="text/css">
    html { height: 100%; }
    body {
        padding: 0 0;
        margin: 0 0;
        height: 100%;
        background: grey;
    }
    #container { background: pink; }
    #autoHeight { background: blue; }
    #remainingHeight { background: green; }
</style>
</head>

<body>
    <div id="container">
        <div id="autoHeight">Lorem ipsum dolor sit amet,consectetur adipisicing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
            Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris
            nisi ut aliquip ex ea commodo consequat.</div>

        <div id="remainingHeight">This div should fill out the rest,making the display
            mainly green. And no,I don't mean to just have green background. It will
            contain other things.</div>
    </div>
</body>
</html>

解决方法

我可以使用display来实现这一点:table和display:table-row(不适用于IE7或更早版本).

http://cssdeck.com/labs/elars8pj

html { height: 100%; }
body {      
    padding:0 0;
    margin:0 0;
    height: 100%;
    background:grey;
}
#container {
    display:table;
    height:100%;
    width:100%;
    background:pink;
}
#autoHeight {
    display:table-row;
    background:blue;
}
#remainingHeight {
    display:table-row;
    height:100%;
    background:green;
}
原文链接:https://www.f2er.com/css/216169.html

猜你在找的CSS相关文章