html - 如何设置div的高度匹配其容器

标签 html css

我有一个 div,它是容器。此 div 包含 2 个 div 标签,如下所示:

<style type="text/css">
#container{
    height:100%
}

#child1{
    height:35px;
}

#child2{
    overlow-y:auto;
    height: ??? /**to match remain space of container**/
}
</style>


<div id="container">
    <div id="child1">this is a child1</div>
    <div id="child2">this is a child2</div>
</div>

如果 child2 的内容太多,如何设置 child2 的高度以匹配垂直滚动条容器的剩余空间?

最佳答案

一种选择是混合使用相对和绝对定位:

#container{
    height:100%;
    position: relative;
}

#child1{
    height:35px;
}

#child2{
    overflow-y:auto;
    position: absolute;
    width: 100%;
    top: 35px;
    bottom: 0;
}

你实际上根本没有设置高度,你让元素从顶部开始 35px,从底部开始 0px

http://jsfiddle.net/AMQ8f/4/

关于html - 如何设置div的高度匹配其容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20026812/

相关文章:

html - 堆叠图像以实现无缝加载?

html - Div off page with a bg image,如何不滚动页面

html - 为什么当宽度为 100% 时这个 div 会溢出?

php - 有没有办法使 GET/POST 数据只能使用一次并在刷新时清除?

jquery - 在 HTML 选择标签中查找默认选择的选项?

javascript - AngularJS:如何在拖动 map 后获取标记的位置

javascript - 页面加载后显示 block 几秒钟

css - 如何修复 ASP.NET AjaxToolkit ComboBox 的样式?

css - 媒体查询不适用于移动设备。 CSS

javascript - anchor 标记中的上标无法正常工作