flexbox儿童可以使用溢出滚动条吗?

时间:2016-01-27 05:20:00

标签: html css html5 css3 flexbox

我想使用CSS3 flexbox model创建跨设备布局,并找到我用作起点的a nice example layout by HugoGiraudel

布局基本上如下:

enter image description here

以下是HTML的结构:

<div class="wrapper">
  <header class="header">Header</header>
  <article class="main">
    ...
  </article>
  <aside class="aside aside-1">Aside 1</aside>
  <aside class="aside aside-2">Aside 2</aside>
  <footer class="footer">Footer</footer>
</div>

这里是CSS最重要的部分:

.wrapper {
  display: flex;
  flex-flow: row wrap;
}
.wrapper > * {
  padding: 10px;
  flex: 1 100%;
}
.aside { flex: 1 auto; }
.main    { flex: 3 0px; }

我的问题是当文章包含的内容多于屏幕上的内容时,外部.wrapper旁边会出现一个垂直滚动条,而且页脚不再可见。

I tried to add overflow: scroll and flex-flow: column wrap to the article's styling,但没有成功 - 滚动条可见但文章总是增长以完全包含其内容。

如果内容增长,如何保持页脚可见并且文章滚动?

1 个答案:

答案 0 :(得分:0)

您的文章内容扩展整个布局的原因是它没有任何高度限制。像max-height这样的东西会限制它的增长,然后会出现垂直滚动条。

这是您的代码,只需进行一些调整:

HTML (为文章和旁白添加了嵌套的Flex容器)

<div class="wrapper">
  <header class="header">Header</header>
  <section class="inner-wrapper">
    <article class="main">
      <p>Pellentesque habitant morbi tristique senectus et netus et malesuada 
         fames ac turpis egestas...</p>
      <p>Pellentesque habitant morbi tristique senectus et netus et malesuada 
         fames ac turpis egestas...</p>
      <p>Pellentesque habitant morbi tristique senectus et netus et malesuada 
         fames ac turpis egestas...</p>
                            .
                            .
                            .
    </article>
    <aside class="aside aside-1">Aside 1</aside>
    <aside class="aside aside-2">Aside 2</aside>
  </section>
  <footer class="footer">Footer</footer>
</div>

CSS (仅限关键调整)

html, body {
  height: 100%;
}

.wrapper {
  display: flex;  
  height: 100%;
  flex-direction: column;        /* switch main container from row direction */
}

.inner-wrapper {
  display: flex;                 /* nested flex container; row direction */
  flex: 0 0 50%;                 /* limit height of container; adjust as necessary */
  min-height: 0;                 /* addresses a scrolling bug in Firefox;
                                    http://stackoverflow.com/a/34982902/3597276 */
}

.header {
    flex: 1;                     /* header to occupy all available height */
}

.footer {
  flex: 1;                       /* footer to occupy all available height */
}

.main {
  overflow-y: scroll;            /* enable vertical scrollbar */
}

Revised Codepen

注意:

  • 现在主要的Flex容器(.wrapper)有三个垂直堆叠的弹性项目。
  • 每个项目的高度可以单独设置。在上面的代码中,页眉和页脚被告知要占用所有可用空间(flex: 1)。中间的flex项(.inner-wrapper)被限制为50%,这使得滚动。尝试25%和75%作为替代示例。
  • 中间项目兼作灵活容器,并在文章中排列并且水平排列。每个项目的宽度可以单独设置。前面的观点也适用于此。