css - 如何让一个DIV填满浏览器窗口剩余的垂直空间?

标签 css html

我有这个简化的代码:

This is a line of text<br/>
<div style="background-color: orange; height: 100%">And this is a div</div>

div 高度最终是浏览器窗口客户空间高度的 100%,加上文本行的高度,大于窗口高度,因此您必须滚动。

如何设置 div 高度,使其采用浏览器窗口的高度减去文本行?

或者,换句话说,我怎样才能让 div 垂直占据所有其他 DOM 对象已经占据的可用空间?

最佳答案

我也遇到了同样的问题。这是我找到的解决方案:

<style>
.container{
    position: relative;
    height: 100%;
}
.top-div{
    /* height can be here. if you want it*/
}
.content{
    position:absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: 1em; /* or whatever height of upper div*/
    background: red;
}
</style>
<div class="container">
    <div class="top-div">This is a line of text</div>
    <div class="content">And this is a div</div>
</div>

来源 - http://www.codingforums.com/archive/index.php/t-142757.html

关于css - 如何让一个DIV填满浏览器窗口剩余的垂直空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4980308/

相关文章:

CSS,<div> 标签的高度和滚动

javascript - Gatsby 中的外部 CSS/JS

html - 只有底部边框的输入文本字段

javascript - 函数没有被调用

javascript - 如何创建只能复制一侧的两列内容?

javascript - jQuery .scroll 像电影一样改变背景

php - 将 php 页面连接到 mySql

javascript - 如何用JavaScript统计页面上的所有图像(包括CSS和img)

javascript - 将更新附加到表而不刷新整个表

html - 我可以更改初始包含 block 的大小吗?