【问题标题】:Responsive grid layout with fixed header, footer and scrollable content具有固定页眉、页脚和可滚动内容的响应式网格布局
【发布时间】:2017-01-20 03:00:36
【问题描述】:

我正在尝试使用 flexbox 创建 2 列全高设计。当我将滚动添加到整个中间部分时,我有一个奇怪的行为。如果父容器有滚动条,似乎 flex-grow/stretch 不会增长/拉伸其他项目。

这是我的fiddle。代码如下:

html, body {
    height: 100%;    
}
#container {
    display: flex;
    flex-direction: column;
    height: 100%;
    
    width: 50%;
    background-color: red;
}

#container header {
    background-color: gray;
}
#container section {
    flex: 1 1 auto;
    overflow-y: auto;
    min-height: 0px;
}
#container footer {
    background-color: gray;
}
aside {
  width : 100px;
  background-color: blue;
}
article{
  width: 100%;
  display:flex;
  flex-direction: column;
}
article > div{
  flex: 1 1 auto;
}

#content {
  display:flex;   
}
<section id="container" >
<header id="header" >This is a header</header>
<section id="content">
  <aside>
    test<br />
    test<br />
    test<br />
    test<br />
    test<br />
    test<br />
    test<br />
    test<br />
    test<br />
    test<br />
    test<br />
    test<br />
    test<br />
    test<br />
    test<br />
    test<br />
    test<br />
    test<br />
    test<br />
    test<br />
    test<br />
  </aside>
  <article id="article" >
    <div>               
      This is the content that
      <br />
      With a lot of lines.
      <br />
      With a lot of lines.
      <br />
      This is the content that
      <br />
      With a lot of lines.
      <br />
      <br />
      This is the content that
      <br />
      With a lot of lines.
      <br />
      <br />
      This is the content that
      <br />
      With a lot of lines.
      <br />
    </div>
    <footer id="footer" >This is a page footer</footer>      
  </article>
</section>
<footer id="footer" >This is a footer</footer>
</section>

基本上我试图涵盖 2 个场景。如果我不需要滚动,它可以正常工作,但是一旦我滚动,项目就不能正确拉伸:

【问题讨论】:

    标签: html css layout flexbox


    【解决方案1】:

    为了使这种布局在今天最新的 Firefox 和 Chorme 中工作(从 2016 年修订这个答案),我进行了以下更改:

    1. margin: 0 添加到body 以允许container 弯曲到视口高度。

    2. #content 元素上的内容包装在另一个section 中,并使其成为 flexbox。

    3. 使内部的section成为一个全高的flexbox并给min-height: max-contentflex: 1

    请看下面的演示:

    html,
    body {
      height: 100%;
      margin: 0; /* ADDED */
    }
    
    #container {
      display: flex;
      flex-direction: column;
      height: 100%;
      width: 50%;
      background-color: red;
    }
    
    #container header {
      background-color: gray;
    }
    
    #container > section { /* ADDED > selector */
      display: flex; /* ADDED */
      flex-direction: column; /* ADDED */
      flex: 1 1 auto;
      overflow-y: auto;
      min-height: 0px;
    }
    
    #container > section > section{ /* ADDED */
      display: flex;
      height: 100%;
      min-height: max-content; /* fixes chrome */
      flex: 1; /* fixes firefox */
    }
    
    #container footer {
      background-color: gray;
    }
    
    aside {
      width: 100px;
      background-color: blue;
      min-height: content;
    }
    
    article {
      width: 100%;
      display: flex;
      flex-direction: column;
    }
    
    article>div {
      flex: 1 1 auto;
    }
    <section id="container">
      <header id="header">This is a header</header>
      <section id="content">
        <section>
          <aside>
            test<br /> test
            <br /> test
            <br /> test
            <br /> test
            <br /> test
            <br /> test
            <br /> test
            <br /> test
            <br /> test
            <br /> test
            <br /> test
            <br /> test
            <br /> test
            <br /> test
            <br /> test
            <br /> test
            <br /> test
            <br /> test
            <br /> test
            <br /> test
            <br />
          </aside>
          <article id="article">
            <div>
              This is the content that
              <br /> With a lot of lines.
              <br /> With a lot of lines.
              <br /> This is the content that
              <br /> With a lot of lines.
              <br />
              <br /> This is the content that
              <br /> With a lot of lines.
              <br />
              <br /> This is the content that
              <br /> With a lot of lines.
              <br />
            </div>
            <footer id="footer">This is a page footer</footer>
          </article>
        </section>
      </section>
      <footer id="footer">This is a footer</footer>
    </section>

    上面的解决方案充其量是 hacky,它向我们展示了为什么 flexbox 在 2D 布局中是 weak 的。答案是它不是为它设计的。但是CSS Grids 是 - 看看一切顺利到位:

    1. 使#container成为一个完整的视口高grid元素。

    2. 使 middle section 成为具有 grid-template-columns: 100px 1froverflow 属性的 grid 容器,您就差不多完成了。

    请看下面的演示:

    body {
      margin: 0;
    }
    
    #container {
      display: grid;
      width: 50%;
      height: 100vh;
      background-color: red;
    }
    
    header, footer {
      background-color: gray;
    }
    
    #container section {
      display: grid;
      grid-template-columns: 100px 1fr;
      overflow-y: auto;
    }
    
    aside {
      background-color: blue;
    }
    
    article {
      display: flex;
      flex-direction: column;
    }
    
    article > div {
      flex: 1 1 auto;
    }
    <section id="container">
      <header id="header">This is a header</header>
      <section id="content">
        <aside>
          test<br /> test
          <br /> test
          <br /> test
          <br /> test
          <br /> test
          <br /> test
          <br /> test
          <br /> test
          <br /> test
          <br /> test
          <br /> test
          <br /> test
          <br /> test
          <br /> test
          <br /> test
          <br /> test
          <br /> test
          <br /> test
          <br /> test
          <br /> test
          <br />
        </aside>
        <article id="article">
          <div>
            This is the content that
            <br /> With a lot of lines.
            <br /> With a lot of lines.
            <br /> This is the content that
            <br /> With a lot of lines.
            <br />
            <br /> This is the content that
            <br /> With a lot of lines.
            <br />
            <br /> This is the content that
            <br /> With a lot of lines.
            <br />
          </div>
          <footer id="footer">This is a page footer</footer>
        </article>
      </section>
      <footer id="footer">This is a footer</footer>
    </section>

    【讨论】:

    • 它不起作用。如果我从容器中删除高度,那么我有一个大卷轴。我只想将滚动添加到中间部分并保持页眉和公共页脚 statick
    • @Vi_Ros 已编辑答案...请检查并让我知道这是否是您需要的。谢谢!
    • 现在我有 2 个单独的滚动条用于左右部分。但我试图达到两个块的一个滚动条。我已经附上了我要修复的图像。
    • @Vi_Ros 已经编辑了答案......现在看看它是否有效,让我知道...... :)
    • 现在它的工作方式与我的示例完全相同。请自行尝试更改并尝试向下滚动。
    猜你喜欢
    • 2015-03-12
    • 2014-09-12
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多