簡體   English   中英

使用 flexbox 使 2 列具有相同的高度

[英]Make 2 columns the same height with flexbox

你好,我想讓我的 2 列具有相同的高度(最小高度為 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,另一列將更短,並且不會擴展到與較大列相同的高度。

更新答案:據我所知,您想要的是 .wrapper 的.wrapper高度,其中有 2 列。 並且每當每列中的一列變得太長(超過 100vh)時,其行為將變為滾動而不是使其父滾動。 這是提煉后的代碼,看看是不是你想要的。 我做的是:

  • .wrapper的特定height100vh
  • .wrapper中的每一列都有max-height: 100%
  • 當列的內容高度超過.wrapper的高度時,它的行為變成滾動,通過overflow-y: auto

您可以在此處閱讀有關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>

 .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>

如果你想自動拆分使用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 的更多信息!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM