CSS Grid - auto generate new rows breaks on auto-flow-column

Viewed 1234

I have a css grid containing some links. It is a three col grid, with any number of rows (depending on the number of items).
I would want even distribution for the number of items across each column preferably.

The num of cols and rows are set by:
grid-template-columns: repeat(3, 1fr); // make it three columns wide
grid-template-rows: repeat(auto-fill); // auto generate new rows

This works perfectly, except when I try to make the grid flow by columns, not rows:
grid-auto-flow: column; // this breaks it

Example:

.container {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(auto-fill);
  /*grid-auto-flow: column;*/ 
}

a {
  height: 50px;
}
<div class="container">
  <a href="#">item one</a>
  <a href="#">item two</a>
  <a href="#">item three</a>
  <a href="#">item four</a>
  <a href="#">item fuve</a>
  <a href="#">item six</a>
  <a href="#">item seven</a>
  <a href="#">item eight</a>
  <a href="#">item nine</a>
  <a href="#">item ten</a>
  <a href="#">item eleven</a>
  <a href="#">item twelve</a>
  <a href="#">item thirteen</a>
  <a href="#">item fourteen</a>
  <a href="#">item fifteen</a>
</div>

How can I keep three columns in my grid, make it flow via columns instead of rows and let it auto generate those rows as and when needed?

1 Answers

You don't need CSS grid but columns:

.container {
  column-count:3;
}

a {
  height: 50px;
  display:block;
}
<div class="container">
  <a href="#">item one</a>
  <a href="#">item two</a>
  <a href="#">item three</a>
  <a href="#">item four</a>
  <a href="#">item fuve</a>
  <a href="#">item six</a>
  <a href="#">item seven</a>
  <a href="#">item eight</a>
  <a href="#">item nine</a>
  <a href="#">item ten</a>
  <a href="#">item eleven</a>
  <a href="#">item twelve</a>
  <a href="#">item thirteen</a>
  <a href="#">item fourteen</a>
  <a href="#">item fifteen</a>
</div>

Related