CSS: height- 填充 div 的其余部分?

标签 css height

我想知道这是否可以通过简单的 css 实现,或者我是否必须为此使用 javascript?

我的网站上有一个侧边栏。一个简单的 div#sidbar 它通常高约 1024 像素,但高度会根据其内容动态变化。

让我们想象一下以下情况:

<div id="sidebar">
   <div class="widget"></div> //has a height of 100px
   <div class="widget"></div> //has a height of 100px
   <div id="rest"></div> //this div should have the rest height till to the bottom of the sidebar
</div>

我希望 div#rest 填充侧边栏的其余部分,直到它到达 div#sidebar 的底部。

这可以用纯 css 实现吗?

最佳答案

如果您知道 #widget 的确切高度(在您的例子中是 100px),您可以通过使用绝对定位来避免使用 JavaScript:

#sidebar
{
height: 100%;
width: ...;
position: relative;
}

.widget
{
height: 100px;
}

#rest
{
position: absolute;
left: 0;
width: 100%;
top: 200px;
bottom: 0;
}

关于CSS: height- 填充 div 的其余部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3411317/

相关文章:

android - 在为 MDPI 和 HDPI ANdroid 设计 UI 界面时,我如何解决高度问题?

jquery - 更改 tr 背景颜色

css - css 高度问题 100%

html - 将滑动菜单悬停在 h1 上的优先级

css - 为什么不在 HTML5 中创建我自己的语义元素?

css - float div 100%不影响外层div

css - 为什么包含的 div 无法识别其中元素的高度?

jquery - 如何设置 div 的高度匹配响应式内容?

css - 仅针对 nexus 10 的媒体查询?

javascript - fieldset 破坏了我在 angularjs-material 中的 flex 布局