html - 具有固定页眉、页脚和可滚动内容的响应式网格布局

标签 html css layout flexbox

我正在尝试使用 flexbox 创建 2 列全高设计。当我将滚动添加到整个中间部分时,我有一个奇怪的行为。如果父容器有滚动条,flex-grow/stretch 似乎不会增长/拉伸(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 个场景。如果我不需要滚动它工作正常但是一旦我有一个滚动元素不能正确拉伸(stretch):

1. Main content 'smaller' than left

2. Main content 'bigger' than left

最佳答案

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

  1. margin: 0 添加到 body 以允许 container flex 到视口(viewport)高度。

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

  3. 将内部 部分设为全高 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 布局中。答案是它不是为此而设计的。但是CSS Grids是 - 看看一切是多么容易就位:

  1. 使 #container 成为全视口(viewport)高网格元素。

  2. 中间部分设为网格容器,其中grid-template-columns: 100px 1fr连同 overflow 属性,您就快完成了。

请看下面的演示:

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>

关于html - 具有固定页眉、页脚和可滚动内容的响应式网格布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39454280/

相关文章:

html - 使用 CSS 动画和背景颜色显示文本

ios - 旋转设备时,动画 uiview 切换回其原始状态

java - 添加文本监听器导致未找到异常

javascript - 如何查询以相同ID开头的多个元素,然后检查输入是否发生变化

jquery - 滚动后如何显示饼图动画

html - 如何使用CSS创建横向div?

android - 如何在 android studio 中将 2 列中的 6 个图像按钮与 3 行对齐?

html - 导航栏内的元素如何在 Twitter Bootstrap 3 中垂直居中?

html - 移动 safari 中的网页 url 栏不是 "shrinking"

javascript - 单击 div 后通过 jquery 更改 css