【问题标题】:Flexbox shrinking main container when main container is horizontal flex box当主容器是水平弹性盒时弹性盒收缩主容器
【发布时间】:2023-04-10 22:20:02
【问题描述】:

我想为我的 SPA 创建一个简单的布局,其中有一个标题组件,它应该一直显示。页脚组件,它应该位于屏幕底部,或者如果主要内容组件比屏幕大,它应该位于内容组件下方。如果屏幕大于内容,则主内容组件应该获得其余空间,因此页脚组件将呈现在底部。

目前我已经在这个codepen 中复制了我的布局。但是,如果您将 codepen 的结果窗口缩小到足够的程度,您将无法看到 Test2 文本,因为页脚组件位于其顶部。我期望的行为是,我可以看到 Test2 文本,并且可以向下滚动到页脚组件。

如果内容组件不是带有flex-direction: row 的弹性框,它可以工作。任何想法为什么这不起作用?

在我的 SPA 中,我使用的是 React,所以我不想为此使用任何 JavaScript。

* {
  padding: 0;
  margin: 0;
  font-size: 2rem;
}

.page {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100%;
  background: yellow;
}

.container {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: scroll;
}

.main {
  flex: 1;
  background: red;
  display: flex;
  flex-direction: row;
  min-height: 30px;
}

.test {
  display: block;
}

.footer {
  background: green;
}
<div class="page">
  <div class="header">Header</div>
  <div class="container">
    <div class="main">
      Main
      <div class="test">Test1<br />Test2<br/></div>
    </div>
    <div class="footer">Footer</div>
  </div>
</div>

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    只需将 flex: 1 0 auto 添加到 .main 类。 Flex 属性是 flex-grow、flex-shrink 和 flex-basis。所以用 0 表示不要收缩。

    * {
      padding: 0;
      margin: 0;
      font-size: 2rem;
    }
    
    .page {
      display: flex;
      flex-direction: column;
      height: 100vh;
      width: 100%;
      background: yellow;
    }
    
    .container {
      display: flex;
      flex-direction: column;
      flex: 1;
      overflow: scroll;
    }
    
    .main {
      flex: 1 0 auto;
      background: red;
      display: flex;
      flex-direction: row;
      min-height: 30px;
    }
    
    .test {
      display: block;
    }
    
    .footer {
      background: green;
    }
    <div class="page">
      <div class="header">Header</div>
      <div class="container">
        <div class="main">
          Main
          <div class="test">Test1<br />Test2<br/></div>
        </div>
        <div class="footer">Footer</div>
      </div>
    </div>

    【讨论】:

      【解决方案2】:

      我让它工作了。我需要在我的内容周围添加一个带有display: block; 的div,并将justify-content: space-between; 添加到我的容器组件中。有点hacky,但它有效......

      这是我的最终代码:

      * {
        padding: 0;
        margin: 0;
        font-size: 2rem;
      }
      
      .page {
        display: flex;
        flex-direction: column;
        height: 100vh;
        width: 100%;
        background: yellow;
      }
      
      .container {
        display: flex;
        flex-direction: column;
        flex: 1;
        justify-content: space-between;
        overflow: scroll;
      }
      
      .block {
        display: block;
      }
      
      .main {
        background: red;
        display: flex;
        flex-direction: row;
      }
      
      .test {
        display: block;
      }
      
      .footer {
        background: green;
      }
      <div class="page">
        <div class="header">Header</div>
        <div class="container">
          <div class="block">
          <div class="main">
            Main
            <div class="test">Test1<br />Test2<br/></div>
          </div>
          </div>
          <div class="footer">Footer</div>
        </div>
      </div>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-05
        • 1970-01-01
        • 1970-01-01
        • 2015-09-09
        • 2017-05-20
        • 2015-05-07
        • 2018-02-21
        • 2021-04-16
        相关资源
        最近更新 更多