为什么Flex和max-height vh工作不正确?

时间:2018-10-04 10:35:37

标签: css flexbox height

我有flex个容器。而且我的flex容器内部有rightleft flex-element。

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

.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%。 我该如何解决?我想让右区块和左区块的高度相同。

2 个答案:

答案 0 :(得分:1)

为您的元素添加+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>

答案 1 :(得分:0)

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

如果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>