[英]Footer with variable-height content above it without using absolute positioning?
I've got the following ( http://jsfiddle.net/u2zedzob/1/ ) 我有以下内容( http://jsfiddle.net/u2zedzob/1/ )
<div class="wrapper">
<div class="main-content"></div>
<div class="footer"></div>
</div>
*, *:before, *:after {
box-sizing: border-box;
}
.wrapper {
background-color: red;
height: 300px;
width: 300px;
}
.main-content {
height: 100%;
width: 100%;
background-color: green;
}
.footer {
height: 30px;
width: 100%;
background-color: blue;
}
In this example, .footer
is placed below .main-content
, but breaks out of .wrapper
. 在此示例中,
.footer
放在.main-content
之下,但脱离.wrapper
。 I would like .main-content
to respect the height of .footer
when determining how tall it can be. 我希望
.main-content
在确定高度时可以尊重.footer
的高度。
I do not need to support older browsers. 我不需要支持较旧的浏览器。 Just the latest Chrome/FF.
只是最新的Chrome / FF。
I can think of a couple of ways to achieve this. 我可以想到几种方法来实现这一目标。
I don't like the position: absolute solution because it means that if I change the height of .footer
I have to know to update the bottom value of .main-content
. 我不喜欢这个位置:绝对解决方案,因为这意味着如果我更改
.footer
的高度,我必须知道更新.main-content
的底值。 Same goes with using calc. 使用calc也是如此。 I could store a variable in my CSS pre-processor to help assist with this, but it still doesn't feel right.
我可以在我的CSS预处理器中存储一个变量来帮助完成此任务,但是感觉仍然不合适。
I've had success with table-cell layouts before for similar scenarios when going horizontally, but I can't seem to get the CSS to work well for vertical. 对于类似的场景,在水平移动时,我之前在表格单元布局方面已经取得了成功,但是我似乎无法使CSS在垂直方向上正常工作。
I'm wondering what approach others would use on this scenario. 我想知道其他人在这种情况下将使用什么方法。 Thanks
谢谢
The solution was to use flexboxes: http://jsfiddle.net/u2zedzob/10/ 解决方案是使用弹性框: http : //jsfiddle.net/u2zedzob/10/
*, *:before, *:after {
box-sizing: border-box;
}
.wrapper {
background-color: red;
height: 300px;
width: 300px;
display: flex;
flex-direction: column;
}
.main-content {
height: 100%;
width: 100%;
background-color: green;
}
.footer {
min-height: 30px;
width: 100%;
background-color: blue;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.