【问题标题】:Why Flex and max-height vh work not correct?为什么 Flex 和 max-height vh 工作不正确?
【发布时间】:2019-03-09 17:18:34
【问题描述】:

我有flex 容器。我的弹性容器中有right 和left 弹性元素。

我的问题:左右块的高度不同。 例如:

.container {
  display: flex;
  width: 100%;
  max-height: calc(100vh - 50px);
  overflow-y: auto;
  background: #fff;
}

.left {
  width: 200px
}

.right {
  width: calc(100% - 200px);
  background: #000;
}
<div class="container">
  <div class="left">
    "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"
  </div>
  <div class="right"></div>
</div>

而且我不明白为什么我的右方块没有高度 - 100%。 我该如何解决?我想要右块和左块相同的高度。

【问题讨论】:

  • 元素高度相同,但左边的元素溢出。尝试从容器中删除 max-height。
  • @Paulie_D 谢谢。我无法删除 max-height 因为我有弹出窗口并且他的高度必须是 100vh - 50px 所有分辨率
  • 很抱歉,我不确定这是什么意思。
  • 在左边添加overflow: auto;
  • @TemaniAfif 它对我不起作用

标签: css flexbox height


【解决方案1】:

元素高度相同,但垂直尺寸较小,左边的元素溢出。

如果max-height 很重要,请将其放在子 div 上。

.container {
  display: flex;
  background: #fff;
}

.container div {
  max-height: calc(100vh - 50px);
  overflow-y: auto;
}

.left {
  width: 200px;
  background: lightgrey;
   
}

.right {
  width: calc(100% - 200px);
  background: #000;
}
<div class="container">
  <div class="left">
    "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human
    happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues
    or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except
    to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"
  </div>
  <div class="right"></div>
</div>

【讨论】:

    【解决方案2】:

    为您的元素添加 +1 容器

    .container {
      max-height: calc(100vh - 50px);
    }
    
    .flex {
      display: flex;
      width: 100%;
      overflow-y: auto;
      background: #fff;
    }
    
    .left {
      width: 200px;
    }
    
    .right {
      width: calc(100% - 200px);
      background: #000;
    }
    <div class="container">
      <div class="flex">
        <div class="left">
        "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"
      </div>
      <div class="right"></div>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 2013-02-23
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      • 2021-12-06
      • 2019-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多