html - CSS相对响应高度
问题描述
#parent {
position: fixed;
width: 600px;
height: 300px;
background: red;
}
#child {
position: relative;
width: 200px;
margin-bottom: 10px;
margin-top: 10px;
height: auto;
background: blue;
}
<div id="parent">
<div id="child"></div>
</div>
我想让孩子拥有父母的身高减去边距。为什么高度是0px?
解决方案
height: auto
只是“自动与它所包含的内容一样大”。如果你想明确地使它的高度减去边距,你可以设置height: calc(100% - 20px);
:
#parent {
position: fixed;
width: 600px;
height: 300px;
background: red;
}
#child {
position: relative;
width: 200px;
margin-bottom: 10px;
margin-top: 10px;
height: calc(100% - 20px);
background: blue;
}
<div id="parent">
<div id="child"></div>
</div>
推荐阅读
- angular - Angular 6中的依赖验证未显示在视图中
- r - 根据存在的数据框行分配分组变量R
- vba - Excel VBA:错误处理中断中间代码
- haskell - 哈斯克尔咖喱地图
- python - 将 C++ STL 容器传递给 Python?
- javascript - Javascript 自动填充选择
- python - 展平锯齿状 numpy 数组列表的列表
- python - 使用 array.contains() 对 postgresql 数组列进行查询过滤
- java - 可运行的 .jar 文件在我的 Windows 设备上运行,但不会在其他设备上运行(Windows 也是)
- python - 如何计算二维 python 列表中相似元素的总和?