【问题标题】:Flexbox footer not stacking under other divsFlexbox 页脚不堆叠在其他 div 下
【发布时间】:2019-08-22 21:07:52
【问题描述】:

我使用 css 和 Flexbox 创建了一个布局,问题是页脚 div 在加载时显示在页面底部,但内容会跳过它,所以当您滚动时,页脚只是浮动在页面中间。我不确定要更改什么。

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

链接到代码JSFiddle

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

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

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

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

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

    【讨论】:

    • 天哪,这太简单了。谢谢你的回答!完全按照现在的方式工作。谢谢!
    【解决方案2】:

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

    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>

    【讨论】:

      【解决方案3】:

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

      • 最初滚动发生在正文上。我在 body 上添加了overflow: hidden,在带有“bodywrap”类的 div 中添加了overflow-y: auto

      • 我添加了sticky和bottom 0的位置,但带有供应商前缀:

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

      这是新版本的演示:

      jsfiddle.net/webwhizjim/6f84b7su/3/

      【讨论】:

        猜你喜欢
        • 2013-02-14
        • 2017-03-14
        • 2020-11-29
        • 1970-01-01
        • 2021-01-24
        • 2018-02-26
        • 2015-02-12
        • 1970-01-01
        • 2013-05-04
        相关资源
        最近更新 更多