简体   繁体   中英

Make 2 columns the same height with flexbox

Hello I want to make my 2 columns the same height (with a min-height of 100vh)

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

my CSS is

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

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

this works fine but if the second (or first) column height larger than 100vh the other column will be shorter and will not expand to the same height as the larger one.

Update answer: As I understand, what you want is the height of 100vh for the .wrapper , which has 2 columns inside it. And everywhen one of each column getting too long (longer than 100vh), its behavior becomes scroll instead of make its parent scroll. Here's the code after refining, give it a look to see if that's what you want. What I do are:

  • .wrapper has a specific height of 100vh
  • each column inside .wrapper has the max-height: 100%
  • When the column's content height getting longer than .wrapper 's height, its behavior becomes scroll, by overflow-y: auto .

You can read more about overflow at here to see what's the difference between overflow: auto , overflow: scroll and overflow: visible (default), which I have struggled while seeking for this answer).

 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>

if you want to auto split use flex: 1 , so it will equal place, without any issue

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

Note Learn more about Flex!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM