<section class="grid-container">
  <div class="block">1</div>
  <div class="block">2</div>
  <div class="block">3</div>
  <div class="block">4</div>
  <div class="block">5</div>
  <div class="block">6</div>
  <div class="block">7</div>
  <div class="block">8</div>
  <div class="block">9</div>
  <div class="block">10</div>
</section>
.grid-container {
  /** * User input values. */
  --grid-layout-gap: 1em;
  --grid-column-count: 4;
  --grid-item--min-width: 15em;

  /** * Calculated values. */
  --gap-count: calc(var(--grid-column-count) - 1);
  --total-gap-width: calc(var(--gap-count) * var(--grid-layout-gap));
  --grid-item--max-width: calc(
    (100% - var(--total-gap-width)) / var(--grid-column-count)
  );
  display: grid;
  grid-template-columns: repeat(
    auto-fill,
    minmax(max(var(--grid-item--min-width), var(--grid-item--max-width)), 1fr)
  );
  grid-gap: var(--grid-layout-gap);
}

.block {
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  height: 10em;
  background-color: red;
  font-size: 1.5rem;
  border-radius: 1em;
}
.block:nth-child(2),
.block:nth-child(6) {
  background-color: green;
}
.block:nth-child(3),
.block:nth-child(7) {
  background-color: blue;
}
.block:nth-child(4),
.block:nth-child(8) {
  background-color: black;
}
.block:nth-child(5),
.block:nth-child(10) {
  background-color: gold;
}

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.