Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set element to be 2fr instead of 1fr CSS Grid Column

Tags:

html

css

css-grid

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 1

like image 719
Jose Avatar asked Jan 27 '19 08:01

Jose


People also ask

What is the difference between auto and 1fr in CSS grid?

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

What is Fr in grid template column?

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.

What does 1fr mean in CSS 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.

What is auto fill in CSS grid?

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.


1 Answers

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>
like image 128
Temani Afif Avatar answered Sep 30 '22 13:09

Temani Afif