【问题标题】:3 rows, 100% height layout, with flexbox3 行,100% 高度布局,带有 flexbox
【发布时间】:2017-05-31 11:33:39
【问题描述】:

我想知道:是否可以使用 flexbox 构建 3 行布局、100% 高度?

<header> The header content goes here. </header>
<div class="content"> The main content goes here. </div>
<footer> The footer content goes here. </footer>

固定高度的页眉和页脚,而内容是液体部分。

我的意思是,像这样但没有绝对定位:

* {
  margin: 0;
}
header {
  position: absolute;
  width: 100%;
  height: 64px;
  top: 0;
  background: red;
}
footer {
  position: absolute;
  width: 100%;
  height: 64px;
  bottom: 0;
  background: green;
}
.content {
  position: absolute;
  width: 100%;
  top: 64px;
  bottom: 64px;
  background: blue;
}
<header>The header content goes here.</header>

<div class="content">The main content goes here.</div>

<footer>The footer content goes here.</footer>

http://jsfiddle.net/BMxzn/

【问题讨论】:

  • 类似this
  • @Michael_B 好的,谢谢,我添加了自己的答案以及适当的文档。

标签: html css flexbox


【解决方案1】:

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
}
.content {
  flex: 1;     /* this is the key; consumes all available height */
  background: blue;
}
header {
  height: 64px;
  background: red;
}
footer {
  height: 64px;
  background: green;
}
* {
  margin: 0;
}
<header>The header content goes here.</header>
<div class="content">The main content goes here.</div>
<footer>The footer content goes here.</footer>

【讨论】:

    【解决方案2】:

    我在这里添加我自己接受的答案,因为它也解决了其他问题。

    我注意到通常建议的代码在 4.4.4 之前的 Android 中存在问题。通过更好地了解 > this> this,我发现问题是 > this,即使 Android 上没有提到受影响的浏览器列表。所以,我的解决方案是在内容中添加 flex-shrink: 0:

    body{
      display: flex;
      flex-direction: column;
      height: 100%;
    }
    
    .main-content{
      flex: 1 0 auto; // flex-shrink:0 > android 4.4.2 fix (and some other browsers too)
    }
    

    为页眉和页脚分配某种 flex 属性也很好。我注意到在 Android 442 上,否则 bg 颜色消失了:

    .main-header,
    .main-footer{
        flex: none; // or flex something.
    }
    

    另外请注意,我使用的是 Autoprefixer。否则,您不应该在主要内容上使用快捷方式(IE shit-fix):

    .main-content{
      flex-grow: 1;
      flex-shrink:0;
      flex-basis:auto;
    }
    

    【讨论】:

      【解决方案3】:

      与这些问题非常相似:this & this

      你只需要 3 行代码:

      display:flex;
      flex-flow:column;
      height:/* whatever height needed */
      

      然后flex:1;到需要填充剩余空间的容器

      * {
        margin: 0;
      }
      body {
        display: flex;
        flex-flow: column;
        height: 100vh;/* if you relay on flex, then vh is also understood */
      }
      body>* {
        padding: 1em;
      }
      header {
        background: red;
      }
      footer {
        background: green;
      }
      .content {
        flex: 1;
        background: blue;
        color: white;
        /* optionnal if you want to keep footer at screen 
        overflow:auto; */
      }
      <header>The header <b>of any height</b> content goes here.</header>
      
      <div class="content">The main content goes here.</div>
      
      <footer>The footer <b>of any height</b> content goes here.</footer>

      无需为页脚或页眉设置高度,但您可以在主容器中添加overflow:auto

      【讨论】:

      • @Stratboy ,是否在问题中指定? IE也可以吗? :) 你能在 IE 需要的 android jsfiddle.net/10ohr6wy/9 (flex:1 1 auto;) 中反馈给我吗
      • 由于问题被阻止,我无法自己添加答案,因此我创建了另一个问题。无论如何,我在另一个线程上的回答更好,因为它解决了 android 问题。所有这些都记录在我随答案发布的外部链接中,因此您可以参考这些内容。写上面的评论只是为了通知未来的人,所以没有什么私人的。祝你有美好的一天。
      • @Stratboy ,好吧,我没有看到涉及 android 的其他问题 :) 。你有没有看一下我之前链接的关于它在 android 中的行为的小提琴(关于溢出的另一个问题,我只是想知道 IE 修复是否也适用于 android ......或 buggy)
      • 嗯,我不太明白,很抱歉。但不管怎样,android 的修复似乎和 IE 的一样。
      猜你喜欢
      • 2017-06-11
      • 2020-07-21
      • 2013-10-13
      • 2011-09-03
      • 1970-01-01
      • 2013-02-16
      • 2017-10-19
      • 2010-09-06
      • 2013-05-07
      相关资源
      最近更新 更多