如何调整列的高度以匹配兄弟姐妹?
我有许多列,每个列包含不同数量的内容。如何自动尺寸大小,以使每个列的高度与最满足最多的一列匹配?
.col {
display: inline-block;
float: left;
width: 200px;
min-height: 100px;
background-color: #eee;
margin-right: 20px;
padding: 10px;
}
<div class="col">
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</span>
</div>
<div class="col">
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</span>
</div>
I have a number of columns each containing a different amount of content. How can I automatically size these so the height of every column matches the one with the most content?
.col {
display: inline-block;
float: left;
width: 200px;
min-height: 100px;
background-color: #eee;
margin-right: 20px;
padding: 10px;
}
<div class="col">
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</span>
</div>
<div class="col">
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</span>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要在2022年使用
float
来对准目的。float
用于在文本块中浮动元素,而不是为了对齐目的。改用Flexbox或CSS网格。唯一适当的用例是电子邮件设备。您需要做的就是包裹列并应用
显示:flex
到包装元素。Don't use
float
in 2022 for alignign purpose.float
is for floating an element within a textblock not for aligning purpose. Use Flexbox or CSS-Grid instead. The only appropriate use-case would be email-templates.All you need to do is to wrap the columns and apply
display: flex
to the wrapping element.显示:flex
。这将使直接的子元素灵活。在简单的语言中,彼此相邻的if(flex方向:行)
。- 然后
flex-items
flex容器的孩子。现在,您的问题已经解决。但是,要使您的布局更加灵活,我将解释flex:1 0 calc(100%/3-30px);
在这里设置flex:1
时,它将使所有孩子相同的大小。但是,如果添加更多元素,它将通过挤压它们将它们全部彼此设置。为了阻止它们在一定尺寸后挤压,我们使用flex-basis
。因此,在这里计算(100%/3-30px);这里100%/3
将3个孩子置于行减去30px
ASgap:15px
也需要从儿童的每一方面计数SO2*15px
是30px
。- 是的
flex-wrap:包装
将把第四元素分解为新行。display:flex
. which will make direct child elements flexible. In simple language set next to each otherif(flex-direction:row)
.-then
flex-items
child of flex container. Now here your issue was already solved. But to make your layout more flexible I will explainflex: 1 0 calc(100% / 3 - 30px);
here when you setflex:1
it will make all child same size. But if add more elements it will set them all next to each other by squeezing them. To stop them squeezing after certain size we useflex-basis
. So, here calc(100%/3 - 30px); here100%/3
set 3 child in rowminus 30px
asgap:15px
also need to be count from each side of child so2*15px
it's30px
.-And yes
flex-wrap:wrap
will break 4th element to new line.