I am trying to make a CSS grid that distributes cells evenly across available rows so that the last row is not mostly empty. The number of cells is variable and but the cell width is fixed. Container width is also responsive.
I can achieve a grid with variable columns/rows with:
grid-template-columns: repeat(auto-fill, Xrem);
But when a new row is created there is just one element in it and rest is empty.
See this for example: https://codepen.io/saurabh0/pen/MYgpZWZ
2
Answers
Try using the CSS function minmax() .
What about the following solution:
Since your max value is known you can write specific CSS to cover all the case. The idea is to adjust the number of columns based on the number of elements (I used my online tool to easily get the selectors: https://css-tip.com/quantity-queries/)
You can easily extend to more than 14 items following the pattern.