【问题标题】:Flex grow 1 doesn't allow proper scrollingFlex grow 1 不允许正确滚动
【发布时间】:2019-04-18 19:26:40
【问题描述】:

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

JSfiddle

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

我想要的是由这三个部分完全填充的视口:导航、左侧栏和右侧内容(见下图)。当右内容部分溢出视口时应该可以滚动的位置。

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

什么是错的,我想要什么基本上在下图中描述。

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

代码(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;
}

【问题讨论】:

    标签: html css scroll flexbox


    【解决方案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】:

    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>

    【讨论】:

    • 认真的吗?你完全改变了你的回答,只是复制了我的:stackoverflow.com/a/55753888/2735479(但现在它至少可以工作了^^)
    • @Tim Krief 亲爱的蒂姆。起初我不明白这个问题的本质。而且我的母语是不同的。我没有抄袭你。 Property Calc 无所不知。我是一名工程师。从第一次开始,我并不总是能理解另一种语言的问题。时不时地,我帮忙。但是,您没有注意到我已经修复的其他问题。
    • 开个玩笑,我明明看到你的答案不一样,但最后还是用了同样的方法。但是,您的答案应该提供更多解释和上下文,而不仅仅是原始代码。另外,我注意到您修复的其他“问题”,例如断字。但这些“问题”不是 OP 问题的一部分。最后,我认为还有一种更优雅的方法可以解决 OP 的问题。
    • @Tim Krief 我给了一个提示,想要的美学 - 让主人自己。我喜欢态度友好的人。我会投票给你。如果需要,您可以在社交网络 vk.com 上找到我,我的页面列在我的个人资料中。如果我冒犯了你,我求求你——不要生我的气。
    • 答案似乎不错,但是,正如我在 Tim Krief 的回答中所说,我不想为导航栏定义特定高度。
    【解决方案3】:

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

    #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);
    }
    

    【讨论】:

    • 看起来不错,但我不想为导航栏定义特定高度
    【解决方案4】:
    #main #content-container #content #right {
      overflow-y: auto;
    }
    

    【讨论】:

    • 这可能不起作用,因为元素比它应该的大。
    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    猜你喜欢
    • 2020-01-15
    • 1970-01-01
    • 2023-01-05
    • 1970-01-01
    • 2021-08-12
    • 2020-05-07
    • 2021-11-07
    • 2011-07-01
    • 1970-01-01
    相关资源
    最近更新 更多