html - 位置绝对但相对
问题描述
目前我使用这个 CSS 将一个 div (.child_bottom) 放置在其父级的底部,并在其上方放置另一个 div (.child),因为我知道 (.child_bottom) 的高度。
高度可变的父级。
.parent
{
position:relative;
}
.child
{
position:absolute;
bottom:250px;
}
.child_bottom
{
position:absolute;
bottom:0;
height:250px;
}
<div class="parent">
<div class="child"></div>
<div class="child_bottom"></div>
</div>
但是我想用.child_bottom的可变高度获得同样的东西,怎么办?
预先感谢您的帮助。
解决方案
使用 flex 将允许您删除所有绝对定位:
.parent {
height: 400px;
outline: 1px solid blue;
display: flex;
flex-direction: column;
justify-content: flex-end;
}
.child {
background: green;
}
.child_bottom {
background: orange;
height: 250px;
}
<div class="parent">
<div class="child">CHILD</div>
<div class="child_bottom">CHILD_BOTTOM</div>
</div>
如果您之前确实希望添加内容.child
,请删除justify-content
并制作此内容flex: 1
:
.parent {
display: flex;
flex-direction: column;
/* justify-content: flex-end; */
height: 400px;
outline: 1px solid blue;
}
.child {
background: green;
}
.child_bottom {
background: orange;
height: 250px;
}
.other_content {
background: yellow;
flex: 1;
}
<div class="parent">
<div class="other_content">
OTHER_CONTENT (Fills the space)
</div>
<div class="child">CHILD</div>
<div class="child_bottom">CHILD_BOTTOM</div>
</div>
推荐阅读
- twitter - 使用一个帐户的 Twitter API 从另一个帐户发送推文?
- python-3.x - Python:不使用命令自动引发自定义异常 raise CustomException("Something Happened")
- python - 使用 OneHotEncoder() 的更好方法是什么?
- python - 运行烧瓶应用程序时出现“flask.cli.NoAppException”
- c++ - 如何验证用户输入的日期月份输入?
- python - 使用 matplotlib.animation.FuncAnimate 对具有周期性边界的多车道高速公路进行动画处理
- sql - 快速计算十分位数
- python - 使用 subprocess.check_output 运行 flake8
- express - 猫鼬代码仅在我发出 2 个 http 请求时才有效
- google-cloud-platform - 为什么 du 没有为添加到 GCP 中的实例的磁盘返回正确的编号