Warm tip: This article is reproduced from serverfault.com, please click

How to create a CSS GRID with 3 distinct head elements

发布于 2020-12-04 09:40:51

I get a list using the CSS display: grid as shown below enter image description here

My task has to be edited so that the list will look like this when it is displayed on PC, while on my mobile it will look like the first image. The first 3 elements will be larger than the rest. And responsive support

enter image description here

Here is my code:

HTML:

.grid {
    width: 638px;
    display: grid;
    grid-row-gap: 68px;
    grid-column-gap: 12px;
    grid-template-rows: auto auto;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    justify-content: space-between;
}

.item {
   border: 3px solid green;
   width: auto;
   height: 150px;
}
<div class="grid">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

I tried to fix it but it doesn't work like, thanks everyone!

Questioner
Tran Thai Hoang
Viewed
0
G-Cyrillus 2020-12-04 18:38:50

you may rethink it from a 12 columns grid and reset grid-column spanning to the item.

possible example

.grid {
    width: 638px;
    display: grid;
    grid-row-gap: 68px;
    grid-column-gap: 12px;
    grid-template-rows: auto auto;
    grid-template-columns: repeat(auto-fit, minmax(35px, 1fr));/* 35 x 4 = 140 */
    justify-content: space-between;
}

.item {
   border: 3px solid #4285F4;
   border-radius:3px;
   width: auto;
   height: 150px;
   grid-column:auto / span 4; /* about 35px X 4 of width */
   margin-right:8px;
}
.item:nth-child(3), .item:nth-child(3)~.item {
margin-right:0;
}
.item:nth-child(3)~.item {
grid-column:auto / span 3;
}
<div class="grid">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

spanning columns rules and grid-template-columns can be reset via the mediaquerie your want to use.