元素(不是浏览器)上的100%高度和滚动条的嵌套flexbox [英] Nested flexbox with 100% height and scrollbar on element (not browser)

查看:148
本文介绍了元素(不是浏览器)上的100%高度和滚动条的嵌套flexbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,如何使文章容器自动增长以消耗其下面的剩余垂直空间,但滚动条仅保留该元素。

In the code below, how do I make the article container auto grow to consume the remaining vertical space below it, but the scroll bar to remain only for that element.

换句话说,我只想让文章内部滚动而不是整个浏览器窗口。

In other words, I want only the inside of article to scroll and not the entire browser window.

有没有一个纯CSS解决方案?或者我需要javascript来检测浏览器窗口的大小,并动态调整文章的高度属性吗?

Is there a pure css solution? Or do I need javascript to detect the size of the browser window and adjust the height property of article dynamically?

html, body {
  //height: 100%;    

}
#outer_container {
  display: flex;
  flex-direction: row;
}
#outer2 {
  flex: 1 0 auto;
}
#outer2 {
  flex: 1 0 auto;
}
#container {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 50%;
  background-color: red;
}
#container header {
  background-color: gray;
}
#container article {
  flex: 1 1 auto;
  overflow-y: auto;
  height: 0px;
}
#container footer {
  background-color: gray;
}

<div id="outer_container">
  <div id="outer1">
    <h2>Outer 1</h2>
  </div>

  <section id="container">
    <header id="header">This is a header</header>
    <article id="content">
      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 />
    </article>
    <footer id="footer">This is a footer</footer>
  </section>


  <div id="outer2">
    <h2>Outer 2</h2>
  </div>
</div>

http://jsfiddle.net/ch7n6/907/

它最初基于此问题的答案:

使用NEWER flexbox api在全高度应用程序中进行Flexbox和垂直滚动

It was originally based on the answer to this question:
Flexbox and vertical scroll in a full-height app using NEWER flexbox api

推荐答案

您可以尝试将 position:fixed 设置为您的外容器( http://jsfiddle.net/ch7n6/909/ ):

You can try setting position:fixed to your outer container (http://jsfiddle.net/ch7n6/909/):

#outer_container{
display:flex;
flex-direction:row;
top:0;
bottom:0;
position:fixed;
}

如果它不适用于您的设计,使用 window.onresize 事件。

If it doesn't work for your design, you can change the container dimensions using window.onresize event.

这篇关于元素(不是浏览器)上的100%高度和滚动条的嵌套flexbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆