I use css grid to resize the columns and move them to new rows when they don't fit. Here is the code that explains everything:
.tiles-container { display: grid; grid-gap: 6rem; grid-template-columns: repeat(auto-fill, minmax(min(220px, 100%), 2fr)); } a { background: red; height: 100px; }
<div class="tiles-container"> <a></a> <a></a> <a></a> <a></a> </div>
grid-template-columns: repeat(auto-fill, minmax(min(220px, 100%), 2fr));
Now, what I want to avoid is moving only one (single) column to a new row. Instead, it collapses earlier and moves the 2 columns together.
To explain it more intuitively, this is acceptable:
█ █ █ █
Also:
█ █ █
██
Also:
██
██
This is unacceptable:
█ █ █
█
I want to avoid unnecessary media queries here. This is my code: https://jsfiddle.net/tucado/0czokyxa/3/
P粉1564156962024-03-29 00:11:31
If anyone encounters the same problem, here is the solution: https://jsfiddle.net/tucado/0czokyxa/5/
and CSS:
.tiles-container { /* first breakpoint*/ --w1:1200px; --n:6; /* second breakpoint*/ --w2:800px; --m:4; /* third breakpoint*/ --w3:400px; --p:2; display:grid; grid-template-columns: repeat(auto-fill, minmax(clamp(clamp(clamp( 100%/(var(--n) + 1) + 0.1%, (var(--w1) - 100%)*1000, 100%/(var(--m) + 1) + 0.1%), (var(--w2) - 100%)*1000, 100%/(var(--p) + 1) + 0.1%), (var(--w3) - 100%)*1000, 100%), 1fr)); gap:10px; border:1px solid; overflow:hidden; margin:5px; resize:horizontal; } .tiles-container > a { height:100px; background:red; }
Thank you @Temani-Afif