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 >
<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 >
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</span>
</div>
CodePudding user response:
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.
.row {
display: flex;
}
.col {
width: 200px;
min-height: 100px;
background-color: #eee;
margin-right: 20px;
padding: 10px;
}
<div >
<div >
<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 >
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</span>
</div>
</div>
CodePudding user response:
- wrap the content to
display:flex
. which will make direct child elements flexible. In simple language set next to each otherif(flex-direction:row)
. -thenflex-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 yesflex-wrap:wrap
will break 4th element to new line.
.row {
display: flex;
flex-wrap: wrap;
gap: 15px;
/*To give some space*/
}
.row .col {
flex: 1 0 calc(100% / 3 - 30px);
background-color: #eee;
padding: 10px;
}
<div >
<div >
<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 >
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</span>
</div>
<div >
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt utempor incididunt utempor incididunt utempor incididunt utempor incididunt ut labore et dolore magna aliqua.</span>
</div>
<div >
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt utempor incididunt utempor incididunt utempor incididunt utempor incididunt ut labore et dolore magna aliqua.</span>
</div>
</div>