home ~ projects ~ socials

Make A CSS Grid With An Arbitrary Number Of Columns

This is how I'm making the grid do the thing

Output

alfa
bravo
charlie
delta

HTML

<div class="example-wrapper">
  <div>alfa</div>
  <div>bravo</div>
  <div>charlie</div>
  <div>delta</div>
</div>

CSS

.example-wrapper {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 1rem;
}

.example-wrapper div {
  background-color: var(--bw-reverse-40);
}
-- end of line --