结合 flex 和绝对定位时的混淆

Tom*_*Tom 4 html css flexbox

我有一个应用程序,它本质上是一个页眉、主要内容和一个始终可见的页脚。页脚可以改变大小,我想在页脚上方的主内容面板上放置一些工具。主要布局是用 flex 完成的,我阅读文档的理解是绝对定位通过相对于最近的后代定位来与 flex 布局相结合,但这似乎不是我的经验。希望有人可以帮助我。

在下面的代码示例中,我希望看到位于页脚上方的“底部”div,但它实际上位于窗口的底部。这是 jsfiddle 链接:http : //jsfiddle.net/L809zbey/

HTML:

<div class="flex">
    <div class="header">Sweet header</div>
    <div class="content">Main content
        <div class="bottom">
          This guy should be fixed above the footer.
        </div>
    </div>

    <div class="footer">Equally sweet footer</div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.flex{
    border: 1px solid #ddd;
    font: 14px Arial;
    display: flex;
    flex-direction: column;
}

.header{
  background : #007AA2;
  flex: 0 0 auto;
}

.content{
  background : #FFF;
  flex: 1 1 auto;
  height: 200px;
}

.footer{
  background : #7FCADE;
  flex: 0 0 auto;
}

.bottom {
  position: absolute;
  bottom: 20px;
}
Run Code Online (Sandbox Code Playgroud)

jle*_*gio 5

尝试添加position:relative;到您的.flex班级。该.bottom元素当前是相对于正文的,因此为什么它被卡在页面底部。