<h2>Grid</h2>
<ul class="grid">
  <li class="grid__item">1</li>
  <li class="grid__item">2</li>
  <li class="grid__item">3</li>
  <li class="grid__item">4</li>
  <li class="grid__item">5</li>
  <li class="grid__item">6</li>
  <li class="grid__item">7</li>
  <li class="grid__item">8</li>
  <li class="grid__item">9</li>
  <li class="grid__item">10</li>
  <li class="grid__item">11</li>
  <li class="grid__item">12</li>
  <li class="grid__item">13</li>
  <li class="grid__item">14</li>
  <li class="grid__item">15</li>
  <li class="grid__item">16</li>
  <li class="grid__item">17</li>
  <li class="grid__item">18</li>
  <li class="grid__item">19</li>
  <li class="grid__item">20</li>
</ul>

<hr style="margin-top: 50px">

<h2>Flex-box</h2>
<div class="flex">
  <ul class="flex__inner">
    <li class="flex__item">1</li>
    <li class="flex__item">2</li>
    <li class="flex__item">3</li>
    <li class="flex__item">4</li>
    <li class="flex__item">5</li>
    <li class="flex__item">6</li>
    <li class="flex__item">7</li>
    <li class="flex__item">8</li>
    <li class="flex__item">9</li>
    <li class="flex__item">10</li>
    <li class="flex__item">11</li>
    <li class="flex__item">12</li>
    <li class="flex__item">13</li>
    <li class="flex__item">14</li>
    <li class="flex__item">15</li>
    <li class="flex__item">16</li>
    <li class="flex__item">17</li>
    <li class="flex__item">18</li>
    <li class="flex__item">19</li>
    <li class="flex__item">20</li>
  </ul>
</div>
$gap: 10px;

body {
  font-family: monospace;
  font-size: 1.25rem;
}

.grid {
  margin: 0;
  padding: 0;
  list-style: none;
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  grid-auto-rows: 50px;
  gap: $gap;
  text-align: center;
  line-height: 50px;
}

.grid__item {
  grid-column: auto / span 2;
  background-color: #ccc;
}

.grid__item:nth-child(9n - 3) {
  grid-column-start: 2;
}

@media (max-width: 768px) {
  .grid {
    grid-template-columns: repeat(8, 1fr);
  }
  .grid__item:nth-child(n) {
    grid-column-start: auto; /* reset nth-child */
  }
  .grid__item:nth-child(7n - 2) {
    grid-column-start: 2;
  }
}

@media (max-width: 576px) {
  .grid {
    grid-template-columns: repeat(6, 1fr);
  }
  .grid__item:nth-child(n) {
    grid-column-start: auto; /* reset nth-child */
  }
  .grid__item:nth-child(5n - 1) {
    grid-column-start: 2;
  }
}

/* =============== flex-box =============== */

.flex {
  overflow: hidden;
}

.flex__inner {
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  text-align: center;
  line-height: 50px;
  margin: -$gap #{-$gap / 2} 0 #{-$gap / 2};
}

.flex__item {
  width: calc(20% - #{$gap});
  background-color: #ccc;
  margin: $gap #{$gap / 2} 0 #{$gap / 2};
}

.flex__item:nth-child(9n - 3) {
  margin-left: #{$gap / 2 + 1px};
}
.flex__item:nth-child(9n) {
  margin-right: #{$gap / 2 + 1px};
}

@media (max-width: 768px) {
  .flex__item {
    width: calc(25% - #{$gap});
  }
  .flex__item:nth-child(n) {
    margin-left: #{$gap / 2}; /* reset nth-child */
    margin-right: #{$gap / 2};
  }
  .flex__item:nth-child(7n - 2) {
    margin-left: #{$gap / 2 + 1px};
  }
  .flex__item:nth-child(7n) {
    margin-right: #{$gap / 2 + 1px};
  }
}

@media (max-width: 576px) {
  .flex__item {
    width: calc(33.333% - #{$gap});
  }
  .flex__item:nth-child(n) {
    margin-left: #{$gap / 2}; /* reset nth-child */
    margin-right: #{$gap / 2};
  }
  .flex__item:nth-child(5n - 1) {
    margin-left: #{$gap / 2 + 1px};
  }
  .flex__item:nth-child(5n) {
    margin-right: #{$gap / 2 + 1px};
  }
}
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.