[英]How to evenly space columns using FlexBox?
We have a set of results that should be displayed in three columns. 我们有一组结果应显示在三列中。 Using
Flexbox
we were able to get a majority of what we wanted to accomplished (ie All have the same height, properly aligned at the top (see flex__top
, and properly aligned at the bottom (see flex__bottom
). 使用
Flexbox
我们可以实现我们想要完成的大部分任务(即,所有组件都具有相同的高度,在顶部正确对齐(请参见flex__top
,在底部正确对齐)(请参见flex__bottom
)。
However, we have ran into an issue where when one of the flex__bottom
elements has a height different then the others, the alignment is off. 但是,我们遇到了一个问题,当
flex__bottom
元素之一的高度不同于其他元素的高度时,对齐方式将关闭。
How do we align all columns to have the same height while having the flex__bottom
section start in the same positions? 在
flex__bottom
节开始于相同位置的同时,如何对齐所有列以使其具有相同的高度?
Goal : 目标 :
flex__bottom
should have a border-top
that aligns with all others. flex__bottom
内的每个项目,都应有一个与所有其他项目对齐的border-top
。 Issue 问题
Codepen is available here in case it gets difficult to replicate in the inline code snippet 如果很难在嵌入式代码段中进行复制,可以在此处使用Codepen
/* Columns */ @media screen and (min-width: 320px) { .col-m-12 { width: 100%; padding: 16px; display: flex; justify-content: center; align-items: stretch; } } @media screen and (min-width: 768px) { .col-t-4 { width: calc(33% - 48px); } } /* Flex */ .flex__wrapper { display: flex; flex-wrap: wrap; justify-content: center; align-items: stretch; flex-direction: row; flex: 0 1 auto; } .flex__link { display: flex; align-items: stretch; justify-content: space-between; flex-direction: column; background-color: #eee; } .flex__top { padding: 16px; } .flex__bottom { border-top: 1px solid; } /* Image */ img { height: auto; display: block; width: 100%; } /* Lists */ ul { list-style: none; } ul>li { display: inline-flex; border: 1px solid; margin: 4px; padding: 4px; } /* Link */ a { text-decoration: none; color: #000; }
<div class="flex__wrapper"> <div class="col-m-12 col-t-4"> <a class="flex__link" href="#"> <div class="flex__top"> <img src="https://placeimg.com/640/480/arch" class="flex__img"> <h2 class="flex__h2">Check back tomorrow;</h2> <p>I will see if the book has arrived.</p> </div> <div class="flex__bottom"> <ul> <li>Learn More</li> </ul> </div> </a> </div> <div class="col-m-12 col-t-4"> <a class="flex__link" href="#"> <div class="flex__top"> <img src="https://placeimg.com/640/480/nature" class="flex__img"> <h2 class="flex__h2">I am happy</h2> <p>I am never at home on Sundays.</p> </div> <div class="flex__bottom"> <ul> <li>Learn More</li> <li>Progressively matrix</li> <li>Uniquely build extensible</li> <li>seamless customer</li> </ul> </div> </a> </div> <div class="col-m-12 col-t-4"> <a class="flex__link" href="#"> <div class="flex__top"> <img src="https://placeimg.com/640/480/people" class="flex__img"> <h2 class="flex__h2">Malls are great places to shop</h2> <p>I can find everything I need under one roof.</p> </div> <div class="flex__bottom"> <ul> <li>maximizing</li> <li>Monotonectally whiteboard</li> </ul> </div> </a> </div> <div class="col-m-12 col-t-4"> <a class="flex__link" href="#"> <div class="flex__top"> <img src="https://placeimg.com/640/480/tech" class="flex__img"> <h2 class="flex__h2">I want to buy</h2> <p>but know it won't suit me.</p> </div> <div class="flex__bottom"> <ul> <li>Learn More</li> <li>task error-free</li> <li>Energistically</li> <li>practices</li> <li>ethical methodologies</li> <li>task error-free</li> </ul> </div> </a> </div> <div class="col-m-12 col-t-4"> <a class="flex__link" href="#"> <div class="flex__top"> <img src="https://placeimg.com/640/480/animals" class="flex__img"> <h2 class="flex__h2">Last Friday in three week</h2> <p>Blue worm shake hands with a legless lizard.</p> </div> <div class="flex__bottom"> <ul> <li>Learn More</li> <li>incubate</li> </ul> </div> </a> </div> <div class="col-m-12 col-t-4"> <a class="flex__link" href="#"> <div class="flex__top"> <img src="https://placeimg.com/640/480/animals/grayscale" class="flex__img"> <h2 class="flex__h2">She advised</h2> <p>him to come back at once.</p> </div> <div class="flex__bottom"> <ul> <li>Learn More</li> <li>Seamlessly</li> <li>build excellent</li> </ul> </div> </a> </div> </div>
If you can limit the amount of content in item heading and descriptions you could add a min-height to .flex-top
. 如果您可以限制项目标题和描述中的内容量,则可以在
.flex-top
添加一个最小高度。
/* Columns */ @media screen and (min-width: 320px) { .col-m-12 { width: 100%; padding: 16px; display: flex; justify-content: center; align-items: stretch; } } @media screen and (min-width: 768px) { .col-t-4 { width: calc(33% - 48px); } } /* Flex */ .flex__wrapper { display: flex; flex-wrap: wrap; justify-content: center; align-items: stretch; flex-direction: row; flex: 0 1 auto; } .flex__link { display: flex; flex-direction: column; background-color: #eee; } .flex__top { padding: 0 16px; min-height: 350px; } .flex__bottom { border-top: 1px solid; } /* Image */ img { height: auto; display: block; width: 100%; } /* Lists */ ul { list-style: none; } ul>li { display: inline-flex; border: 1px solid; margin: 4px; padding: 4px; } /* Link */ a { text-decoration: none; color: #000; }
<div class="flex__wrapper"> <div class="col-m-12 col-t-4"> <a class="flex__link" href="#"> <div class="flex__top"> <img src="https://placeimg.com/640/480/arch" class="flex__img"> <h2 class="flex__h2">Check back tomorrow;</h2> <p>I will see if the book has arrived.</p> </div> <div class="flex__bottom"> <ul> <li>Learn More</li> </ul> </div> </a> </div> <div class="col-m-12 col-t-4"> <a class="flex__link" href="#"> <div class="flex__top"> <img src="https://placeimg.com/640/480/nature" class="flex__img"> <h2 class="flex__h2">I am happy</h2> <p>I am never at home on Sundays.</p> </div> <div class="flex__bottom"> <ul> <li>Learn More</li> <li>Progressively matrix</li> <li>Uniquely build extensible</li> <li>seamless customer</li> </ul> </div> </a> </div> <div class="col-m-12 col-t-4"> <a class="flex__link" href="#"> <div class="flex__top"> <img src="https://placeimg.com/640/480/people" class="flex__img"> <h2 class="flex__h2">Malls are great places to shop</h2> <p>I can find everything I need under one roof.</p> </div> <div class="flex__bottom"> <ul> <li>maximizing</li> <li>Monotonectally whiteboard</li> </ul> </div> </a> </div> <div class="col-m-12 col-t-4"> <a class="flex__link" href="#"> <div class="flex__top"> <img src="https://placeimg.com/640/480/tech" class="flex__img"> <h2 class="flex__h2">I want to buy</h2> <p>but know it won't suit me.</p> </div> <div class="flex__bottom"> <ul> <li>Learn More</li> <li>task error-free</li> <li>Energistically</li> <li>practices</li> <li>ethical methodologies</li> <li>task error-free</li> </ul> </div> </a> </div> <div class="col-m-12 col-t-4"> <a class="flex__link" href="#"> <div class="flex__top"> <img src="https://placeimg.com/640/480/animals" class="flex__img"> <h2 class="flex__h2">Last Friday in three week</h2> <p>Blue worm shake hands with a legless lizard.</p> </div> <div class="flex__bottom"> <ul> <li>Learn More</li> <li>incubate</li> </ul> </div> </a> </div> <div class="col-m-12 col-t-4"> <a class="flex__link" href="#"> <div class="flex__top"> <img src="https://placeimg.com/640/480/animals/grayscale" class="flex__img"> <h2 class="flex__h2">She advised</h2> <p>him to come back at once.</p> </div> <div class="flex__bottom"> <ul> <li>Learn More</li> <li>Seamlessly</li> <li>build excellent</li> </ul> </div> </a> </div> </div>
However, maybe grid is a better option for this layout 但是,对于这种布局,也许网格是更好的选择
@media screen and (min-width: 640px) { .grid__wrapper { display: grid; grid-template-columns: repeat(3, 1fr); grid-gap: 10px; } } .grid__item { background: #ccc; padding: 10px; margin-bottom: 10px; } @media screen and (min-width: 640px) { .grid__item { margin-bottom: 0; } } @media screen and (min-width: 640px) { .grid__link { height: 100%; display: grid; grid-auto-rows: 1fr; } } .item__bottom { border-top: 1px solid; } img { height: auto; display: block; width: 100%; } ul { list-style: none; } ul>li { display: inline-flex; border: 1px solid; margin: 4px; padding: 4px; } a { text-decoration: none; color: #000; }
<div class="grid__wrapper"> <div class="grid__item"> <a class="grid__link" href="#"> <div> <img src="https://placeimg.com/640/480/arch" class="flex__img"> <h2>Check back tomorrow;</h2> <p>I will see if the book has arrived.</p> </div> <div class="item__bottom"> <ul> <li>Learn More</li> </ul> </div> </a> </div> <div class="grid__item"> <a class="grid__link" href="#"> <div> <img src="https://placeimg.com/640/480/nature" class="flex__img"> <h2>I am happy</h2> <p>I am never at home on Sundays.</p> </div> <div class="item__bottom"> <ul> <li>Learn More</li> <li>Progressively matrix</li> <li>Uniquely build extensible</li> <li>seamless customer</li> </ul> </div> </a> </div> <div class="grid__item"> <a class="grid__link" href="#"> <div> <img src="https://placeimg.com/640/480/people" class="flex__img"> <h2>Malls are great places to shop</h2> <p>I can find everything I need under one roof.</p> </div> <div class="item__bottom"> <ul> <li>maximizing</li> <li>Monotonectally whiteboard</li> </ul> </div> </a> </div> <div class="grid__item"> <a class="grid__link" href="#"> <div> <img src="https://placeimg.com/640/480/tech" class="flex__img"> <h2>I want to buy</h2> <p>but know it won't suit me.</p> </div> <div class="item__bottom"> <ul> <li>Learn More</li> <li>task error-free</li> <li>Energistically</li> <li>practices</li> <li>ethical methodologies</li> <li>task error-free</li> </ul> </div> </a> </div> <div class="grid__item"> <a class="grid__link" href="#"> <div> <img src="https://placeimg.com/640/480/animals" class="flex__img"> <h2>Last Friday in three week</h2> <p>Blue worm shake hands with a legless lizard. Blue worm shake hands with a legless lizard. Blue worm shake hands with a legless lizard. Blue worm shake hands with a legless lizard. </p> </div> <div class="item__bottom"> <ul> <li>Learn More</li> <li>incubate</li> </ul> </div> </a> </div> <div class="grid__item"> <a class="grid__link" href="#"> <div> <img src="https://placeimg.com/640/480/animals/grayscale" class="flex__img"> <h2>She advised</h2> <p>him to come back at once.</p> </div> <div class="item__bottom"> <ul> <li>Learn More</li> <li>Seamlessly</li> <li>build excellent</li> </ul> </div> </a> </div> </div>
To achieve expected result, set height for flex__bottom 为了达到预期的效果,请为flex__bottom设置高度
Issue: flex__bottom height is not specified and child elements are not even , default height: auto will adjust height according to flex__bottom elements 问题:未指定flex__bottom高度,子元素也不均匀,默认高度:auto将根据flex__bottom元素调整高度
.flex__bottom {
border-top: 1px solid;
height: 100px;
}
https://codepen.io/nagasai/pen/LMgxLg https://codepen.io/nagasai/pen/LMgxLg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.