Modify Css Grid boxes Correctly

Viewed 36

i want to rearrange grid boxes by correcting my css code.

These boxes should be bigger: 1,6,7,12,17,22 and so on...

But, my code makes these boxes bigger: 3,4,9,10,15,16 and so on...

Note:

  1. I do not know how many boxes will be there because it will be generated automatically, that's why i wanna use nth-child which can make it dynamically adjustable. Please consider that too.
  2. I also want to avoid calling every child separately and style it separately.

Here is my code Snippet:

.grid {
  display: grid;
  grid-auto-flow: dense;
  grid-auto-rows: 100px;
  grid-auto-columns: 1fr;
  grid-gap: 5px;
}
.grid :nth-child(6n + 2) {
  grid-column: 1;
}
.grid :nth-child(6n + 3) {
  grid-area: span 2/2;
}
.grid :nth-child(6n + 4) {
  grid-row: span 2;
}

/**/
.grid {
  max-width: 600px;
  margin: auto;
  counter-reset: num;
}
.grid * {
  border: 2px solid;
  font-size: 30px;
  box-sizing: border-box;
  font-family: sans-serif;
  display: grid;
  place-content: center;
}
.grid *:before {
  content: counter(num);
  counter-increment: num;
}
<div class="grid">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
    <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>  <div></div>
   
</div>

1 Answers

Like below? I simply updated the indices of the nth-child()

.grid {
  display: grid;
  grid-auto-flow: dense;
  grid-auto-rows: 100px;
  grid-auto-columns: 1fr;
  grid-gap: 5px;
}

.grid :nth-child(6n + 5) {
  grid-column: 1;
}
.grid :nth-child(6n + 6) {
  grid-area: span 2/2;
}
.grid :nth-child(6n + 1) {
  grid-row: span 2;
}

/**/
.grid {
  max-width: 600px;
  margin: auto;
  counter-reset: num;
}
.grid * {
  border: 2px solid;
  font-size: 30px;
  box-sizing: border-box;
  font-family: sans-serif;
  display: grid;
  place-content: center;
}
.grid *:before {
  content: counter(num);
  counter-increment: num;
}
<div class="grid">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
    <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>  <div></div>
   
</div>

Related