Can I make a CSS grid with dynamic number of rows or columns?

前端 未结 5 2083
猫巷女王i
猫巷女王i 2020-12-13 08:51

What I wanna do is to make a CSS grid with a dynamic number of cells. For the sake of simplicity, let\'s assume there will always be four cells per row. Can I specify a grid

5条回答
  •  难免孤独
    2020-12-13 09:01

    For those landing here looking for a way to have a sort of dynamic table, with items wrapping to new rows, and still being aligned across rows, a pretty good solution is to use flex with flex-wrap and flex: 1 for all elements:

    .container {
      width: 100%;
      display: flex;
      flex-wrap: wrap;
      align-items: center;
    }
    
    .container .item {
      flex: 1;
    }
    

提交回复
热议问题