【问题标题】:overflow auto with no height in flexflex中没有高度的溢出自动
【发布时间】:2020-10-06 03:20:29
【问题描述】:

我有一些复杂的 flex 布局。

默认布局

* {
  margin: 0
}

.root {
  width: 100%;
  height: 400px;
  background: gray;
  display: flex;
  flex-direction: column;
}

header {
  width: 100%;
  background: red;
  height: 80px;
}

main {
  width: 100%;
  background: blue;
  flex-grow: 1;
}

.container {
  display: flex;
  flex-direction: row;
  height: 100%;
}

.column {
  display: flex;
  flex-direction: column;
}

.column>.head {
  flex: 0 0 40px;
  background: white;
}

.column>.body {
  overflow: auto;
  flex-grow: 1;
  background: skyblue;
}

.fixed {
  flex: 0 0 40px;
  background: black;
}
<div class="root">
  <header>

  </header>
  <main>
    <div class="container">
      <div class="column" style="flex-grow:6;background:yellow">
        <div class="container">
          <div class="column" style="flex-grow:6;background:pink">
            <div class="head">Head</div>
            <div class="body">
              <p>This space should be overflow auto</p>
            </div>
          </div>
          <div class="column" style="flex-grow:4;background:lime"></div>
        </div>
        <div class="fixed"></div>
      </div>
      <div class="column" style="flex-grow:4;background:orange">

      </div>
    </div>
  </main>

</div>

您可以在我的代码中看到skyblue 空格。

许多&lt;p&gt; 将位于skyblue 空间中。

所以它会像这样溢出。

我想让它溢出时滚动。

* {
  margin: 0
}

.root {
  width: 100%;
  height: 400px;
  background: gray;
  display: flex;
  flex-direction: column;
}

header {
  width: 100%;
  background: red;
  height: 80px;
}

main {
  width: 100%;
  background: blue;
  flex-grow: 1;
}

.container {
  display: flex;
  flex-direction: row;
  height: 100%;
}

.column {
  display: flex;
  flex-direction: column;
}

.column>.head {
  flex: 0 0 40px;
  background: white;
}

.column>.body {
  overflow: auto;
  flex-grow: 1;
  background: skyblue;
}

.fixed {
  flex: 0 0 40px;
  background: black;
}
<div class="root">
  <header>

  </header>
  <main>
    <div class="container">
      <div class="column" style="flex-grow:6;background:yellow">
        <div class="container">
          <div class="column" style="flex-grow:6;background:pink">
            <div class="head">Head</div>
            <div class="body">
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
            </div>
          </div>
          <div class="column" style="flex-grow:4;background:lime"></div>
        </div>
        <div class="fixed"></div>
      </div>
      <div class="column" style="flex-grow:4;background:orange">

      </div>
    </div>
  </main>

</div>

条件

我不想声明 heightskyblue 空间。

身体的高度应该是100vh,但在这个代码中是400px

【问题讨论】:

标签: css


【解决方案1】:

flex: 1 0 0; 添加到.column&gt;.body,您将有skyblue 空间滚动溢出。

* {
  margin: 0
}

.root {
  width: 100%;
  height: 400px;
  background: gray;
  display: flex;
  flex-direction: column;
}

header {
  width: 100%;
  background: red;
  height: 80px;
}

main {
  width: 100%;
  background: blue;
  flex-grow: 1;
}

.container {
  display: flex;
  flex-direction: row;
  height: 100%;
}

.column {
  display: flex;
  flex-direction: column;
}

.column>.head {
  flex: 0 0 40px;
  background: white;
}

.column>.body {
  overflow: auto;
  flex: 1 0 0; /* Change from flex-grow:1  to flex: 1 0 0 */
  background: skyblue;
}

.fixed {
  flex: 0 0 40px;
  background: black;
}
<div class="root">
  <header>

  </header>
  <main>
    <div class="container">
      <div class="column" style="flex-grow:6;background:yellow">
        <div class="container">
          <div class="column" style="flex-grow:6;background:pink">
            <div class="head">Head</div>
            <div class="body">
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
              <p>This space should be overflow auto</p>
            </div>
          </div>
          <div class="column" style="flex-grow:4;background:lime"></div>
        </div>
        <div class="fixed"></div>
      </div>
      <div class="column" style="flex-grow:4;background:orange">

      </div>
    </div>
  </main>

</div>

【讨论】:

  • @eddy 我看到 ROOT 已经回答了你的问题。您可以使用正值top 来定位.custom-tooltip,而不是使用负值bottom。例如。 top: 4rem 由于您在.button-container 中使用了rem,因此您可以在.custom-tooltip 中使用相同的单位进行定位。
【解决方案2】:

尝试使用max-height

.column>.body {
    max-height: 450px;
    overflow-y: auto;
    flex-grow: 1;
    background: skyblue;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-08
    • 2012-04-04
    • 2011-04-11
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2021-01-10
    • 2020-12-23
    相关资源
    最近更新 更多