简体   繁体   English

在不使用绝对定位的情况下,具有可变高度内容的页脚?

[英]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. 我可以想到几种方法来实现这一目标。

  • position: absolute, .footer { bottom: 0 }, .main-content { bottom: 30 } 位置:绝对,.footer {底部:0} 、. main-content {底部:30}
  • .main-content { height: calc(100% - 30px) } .main-content {高度:calc(100%-30px)}
  • potentially display: table / display: table-cell with some other fussing? 可能显示:表格/显示:表格单元还有其他麻烦吗?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM