Flexbox页脚未在其他div下堆叠

时间:2019-04-01 16:20:00

标签: html css css3 flexbox

我使用css和Flexbox创建了一个布局,问题是页脚div在加载时显示在页面的底部,但是内容超出了它,因此,当您滚动页脚时,该页脚正浮在页面中间。我不确定要更改什么。

我将页脚更改为粘性,将底部更改为0px。它有点可以调整其他div的边距,但是它不是很干净。我希望继续使用flexbox属性,而只是将它们堆叠,但这似乎不起作用?我还调整了其他div的min-max高度,但是一旦窗口缩小到最小高度以上,页脚就会浮在其余内容上。

链接到代码JSFiddle

.footer{
height:40px;
display:flex;
align-items:center;
justify-content:center;
width:100%;
background-color:purple;
}

我怀疑页脚会遵守堆叠顺序,只是在其余内容下显示,就像主体在页眉下一样。

3 个答案:

答案 0 :(得分:1)

这是您在'.content'类上设置的高度。将height: calc(100vh - 100px)更改为min-height: calc(100vh - 100px)

除非您希望页脚和页眉始终可见,否则只需添加overflow: auto即可使内容滚动

答案 1 :(得分:0)

height: calc(100vh - 100px);类中删除.content

body,
html {
  height: 100%;
  width: 100%;
  padding: 0;
  margin: 0;
}

.bodywrap {
  display: flex;
  flex-wrap: wrap;
  align-content: space-between;
  
  background-color: black;
}
.header {
  display: flex;
  justify-content: space-between;
  width: 100%;
  height: 60px;
  background-color: brown;
}
.hleft {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 250px;
  background-color: lightgreen;
}
.hmid {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-grow:1;
  font-size: calc(1.5vw);
  background-color: orange;
}
.hright {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 400px;
  background-color: pink;
}
.content {
  display: flex;
  
  justify-content: space-between;
  background-color: darkblue;
}
.lmenu {
  display: flex;
  width: 250px;
  flex-wrap: wrap;
  align-content: space-between;
  height: 100%;
  min-height: 600px;
  background-color: lightgrey;
  overflow: hidden;
}
.ltop {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 150px;
  width: 100%;
  background-color: blue;
}
.lmid {
  height: 50px;
  width: 100%;
  background-color: green;
}
.lbot {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
  width: 100%;
  background-color: yellow;
}
.rmaincont {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  width: calc(100vw - 250px);
  background-color: grey;
}
.note {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: lightblue;
  height: 50px;
}
.main {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  height: calc(100vh - 50px);
  min-height: 550px;
  width: 100%;
  padding-left: 20px;
  background-color: red;
}
.footer {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  background-color: purple;
}
<!DOCTYPE html>
<html>

<head>
  <title>Mid-Valley Intranet</title>
  <link rel="stylesheet" type="text/css" href="css/cstyle.css">
</head>

<body>

  <div class="bodywrap">

    <header class="header">
      <div class="hleft">Left</div>
      <div class="hmid">Mid</div>
      <div class="hright">Right</div>
    </header>

    <div class="content">
      <div class="lmenu">
        <div class="ltop">
          Top
        </div>
        <div class="lmid">
          Mid
        </div>
        <div class="lbot">
          Bot
        </div>
      </div>
      <div class="rmaincont">
        <div class="note">
          Notice
        </div>
        <div class="main">
          Main Content
        </div>
      </div>
    </div>

    <footer class="footer">
      Footer Text
    </footer>

  </div>

</body>

</html>

答案 2 :(得分:0)

要完成这项工作,需要做一些事情:

  • 最初,滚动发生在身体上。我在主体上添加了"batchSize",并通过“ bodywrap”类将overflow: hidden添加到了div。

  • 我添加了位置粘滞和底部0,但带有供应商前缀:

overflow-y: auto
  • 我还使“ bodywrap”类的div的高度等于100vh减去页脚的高度(这样,滚动内容不会在底部截断)。您可能要为此40px的高度设置一个sass变量或其他内容。
bottom: 0;
position: -webkit-sticky;
position: -moz-sticky;
position: -ms-sticky;
position: -o-sticky;

这是新版本的演示:

jsfiddle.net/webwhizjim/6f84b7su/3/