I'm trying to create a flexible CSS grid to display some cards. I've set my CSS code for the cards to repeat and auto-fill to a minimum of 330px and a max of 1fr. Everything is fine, but now I have a card that I require to be a little bigger, at 2fr. The problem is very simple, but I can find a way to make this one card to be 2fr instead of 1fr.
Cards container
.cards-row{
display: grid;
grid-template-columns: repeat(auto-fill, minmax(330px, 1fr));
grid-column-gap: 15px;
}
<div class="cards-row" style="margin-top: 30px;">
<div class="card-wrap">
</div>
<div class="card-wrap">
</div>
<div class="card-wrap">
</div>
<div class="card-wrap">
</div>
<div class="card-wrap">
</div>
</div>
This is basically what I want to achieve
Since the browser found a grid item of 1fr, it will allow such item to take all available space (that is 100% of the container), unless something else appears on that track. When the auto thing appears, it is only assigned, so to say it, enough space to exist (with its content, currently just a number).
The Fr Unit : Fr is a fractional unit. The Fr unit is an input that automatically calculates layout divisions when adjusting for gaps inside the grid.
Fr is a fractional unit and 1fr is for 1 part of the available space. The following are a few examples of the fr unit at work. The grid items in these examples are placed onto the grid with grid areas.
Auto-fill: The auto-fill property fills the rows with as many columns as it can fit. The newly added column may be empty but it will still occupy a space in the given row. It is an important property in the CSS grid that make a responsive layout without writing a media query for each grid.
Make the last element to span 2 columns:
.cards-row {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
grid-gap: 15px;
}
.card-wrap {
height: 100px;
background: red;
}
.card-wrap:last-child {
grid-column: span 2;
}
<div class="cards-row" style="margin-top: 30px;">
<div class="card-wrap">
</div>
<div class="card-wrap">
</div>
<div class="card-wrap">
</div>
<div class="card-wrap">
</div>
<div class="card-wrap">
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With