Flex Grow 1不允许正确滚动

时间:2019-04-18 19:26:40

标签: html css css3 scroll flexbox

我遇到的是Flex的一个小问题。我正在尝试获得一个简单的布局(如链接的jsfiddle中所示)。但是,当右侧的div(红色)溢出其高度时,该元素将在视口中溢出,并且仅当它在视口的高度中溢出时才可以滚动。

JSfiddle

我很确定这是因为我在很多元素中使用了height: 100%;,但是我需要它们尽可能的大(以完全填充视口高度)。

我想要的是由这三个部分完全填充的视口:导航,左侧栏和右侧内容(请参见下图)。正确的内容部分在视口溢出时应在何处滚动。

What I want

因此,可以得出一个结论。.这里的问题是,可滚动内容首先溢出视口,并且当div与视口相同的高度(看起来像)时,它开始是可滚动的。我显然希望div在视口溢出时可滚动。

下图基本上描述了问题所在和我想要的内容。 Left: what's wrong. Right: What I want

我将如何实现?感谢您的宝贵时间。

代码(在JSFiddle中也可用)

HTML            

  <div id="content-container">
    <div id="content">
      <div id="left">
        IM LEFT
      </div>
      <div id="right">
        <div id="inner">
          IM RIGHT
        </div>
        IM RIGHT
      </div>
    </div>
  </div>
</div>

CSS

html, body {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

#main {
  height: 100%;
  background: black;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: flex-start;
  width: 100vw;
  height: 100vh;
}

#main nav {
  background: orange;

  width: 100%;
  height: 96px;
}

#main #content-container {
  background: darkgreen;
  color: white;
  width: 100%;
  flex-grow: 1;
  max-height: 100%;
}

#main #content-container #content {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: flex-start;
  height: 100%;
  max-height: 100%;
}

#main #content-container #content #left {
  background: darkblue;
  width: 30%;
  height: 100%;
}

#main #content-container #content #right {
  background: darkred;
  height: 100%;
  flex-grow: 1;
  overflow-y: scroll;
}

#main #content-container #content #right #inner {
  font-size: 25rem;
}

4 个答案:

答案 0 :(得分:1)

html,
body {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

#main {
  height: 100%;
  background: black;
  display: flex;
  flex-wrap: wrap;
}

#main nav {
  background: orange;
  width: 100%;
  height: 96px;
}

#main #content-container {
  background: darkgreen;
  color: white;
  width: 100%;
  height: calc(100% - 96px);
  max-height: calc(100% - 96px);
}

#main #content-container #content {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: flex-start;
  height: 100%;
  max-height: 100%;
}

#main #content-container #content #left {
  background: darkblue;
  width: 30%;
  height: 100%;
}

#main #content-container #content #right {
  background: darkred;
  height: 100%;
  width: 70%;
  overflow-y: auto;
  overflow-x: hidden;
  word-break: break-all;
}

#main #content-container #content #right {
  font-size: 27em;
}
<div id="main">

  <nav>
    <ul>
      <li>
        <a href="/">Home</a>
      </li>
      <li>
        <a href="/games">Games</a>
      </li>
      <li>
        <a href="/create">Create</a>
      </li>
    </ul>
  </nav>

  <div id="content-container">
    <div id="content">
      <div id="left">
        IM LEFT
      </div>
      <div id="right">
        IM RIGHT
      </div>
    </div>
  </div>

</div>

答案 1 :(得分:1)

您可以像下面那样简化代码:

body {
  margin: 0;
}

#main {
  height: 100vh; /*full height*/
  background: black;
  display: flex;
  flex-direction: column;
}

#main nav {
  background: orange;
  height: 96px; /*fixed height*/
}

#content-container {
  background: darkgreen;
  color: white;
  flex-grow: 1; /*fill the remaining height*/
  min-height:0;  /*enable the shrink*/
}

#content {
  display: flex;
  flex-direction: row;
  height: 100%;
}

#left {
  background: darkblue;
  width: 30%;
}

#right {
  background: darkred;
  overflow:auto;
}


/*Irrelevant*/
#inner {
  font-size: 25rem;
}
<div id="main">

  <nav>
    <ul>
      <li>
        <a href="/">Home</a>
      </li>
      <li>
        <a href="/games">Games</a>
      </li>
      <li>
        <a href="/create">Create</a>
      </li>
    </ul>
  </nav>

  <div id="content-container">
    <div id="content">
      <div id="left">
        IM LEFT
      </div>
      <div id="right">
        <div id="inner">
          IM RIGHT
        </div>
        IM RIGHT
      </div>
    </div>
  </div>

</div>

答案 2 :(得分:0)

一种解决方案是在使用#content定义calc(100% - 96px)的高度时考虑nav元素的高度,96px是nav元素的高度:

#main #content-container #content {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: flex-start;
  height: calc(100% - 96px);
  max-height: calc(100% - 96px);
}

答案 3 :(得分:0)

#main #content-container #content #right {
  overflow-y: auto;
}