使用flexbox使两列高度相同

时间:2019-12-19 03:06:54

标签: css flexbox

你好,我想使两列的高度相同(最小高度为100vh)

<div class="wrapper">
    <div class="col">... little content... </div>
    <div class="col">...more content... </div>
</div>

我的CSS是

.wrapper {
    display: flex;
    height: 100vh;
}

.col {
    outline: 1px #000 solid;
}

这很好,但是如果第二(或第一)列的高度大于100vh,则另一列会更短,并且不会扩展到与较大的列相同的高度。

3 个答案:

答案 0 :(得分:5)

更新答案:据我了解,您想要的是.wrapper的高度为100vh,其中有2列。并且每当每列之一变得太长(超过100vh)时,其行为就会变为滚动而不是使其父级滚动。精炼后的代码如下,看看它是否就是您想要的。我要做的是:

  • .wrapper具有特定的height 100vh
  • .wrapper内的每个列都有max-height: 100%
  • 当列的内容高度大于.wrapper的高度时,其行为将滚动overflow-y: auto

您可以在here阅读有关overflow的更多信息,以了解overflow: autooverflow: scrolloverflow: visible(默认)之间的区别,我一直在努力在寻找此答案时。)

body {
  margin: 0;
  padding: 0;
}

.wrapper {
  display: flex;
  height: 100vh;
}

.col {
  outline: 1px green solid;
  max-height: 100%;
  overflow-y: auto;
  width: 120px;
}
<div class="wrapper">
  <div class="col">... little content... </div>
  <div class="col">..Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.... </div>
</div>

答案 1 :(得分:2)

如果您想自动拆分,请使用flex: 1,这样它就等于地方,没有任何问题

    .wrapper {
      display: flex;
      min-height: 100vh;
    }

    .col {
      display: flex;
      align-items: center;
      justify-content: center;
      text-align: center;
      flex: 1;
      flex-direction: column;
      background: grey;
      }
      
      .col:nth-child(2) { background: yellow }
    <div class="wrapper">
        <div class="col">... little content... </div>
        <div class="col">...more content... </div>
    </div>

注意了解有关Flex的更多信息!

答案 2 :(得分:2)

.wrapper {
  display: flex;
  flex-direction: row;
}

.col {
  outline: 1px #000 solid;
  display: flex;
  flex: 1;
  min-height: 100vh;
}
<div class="wrapper">
  <div class="col">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
    publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has
    a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search
    for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). There are many variations of passages of Lorem Ipsum available,
    but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden
    in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model
    sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</div>
  <div class="col">...more content... </div>
</div>