Reproduce flex behaviour with grid

Hello, I've just found out this pen: https://codepen.io/RamSande/pen/ebNYJP and I wanted to try to replicate it using grid, I tried
display: grid;
grid-template-columns: repeat(5, minmax(100px, auto));
display: grid;
grid-template-columns: repeat(5, minmax(100px, auto));
But this doesn't make the last elements occupy the whole content, is it possible to do that with grid? I also tried 1fr but it doesn't work as I want
13 Replies
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)β€’3d ago
You would need 10 columns, then make the last two items span 5 columns btw, the other boxes should also span 2 columns
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)β€’3d ago
Like this
No description
Chooβ™šπ•‚π•šπ•Ÿπ•˜
In addtion to using the 10 columns, you also need a media query. I don't know if you tried changing the width of the viewport. It switches to 4 columns for narrower displays, and it does this without the need for a media query. Flex is definitely the simpler solution to achieve this.
MC23
MC23OPβ€’3d ago
Thank you. I know that it's the best solution and I'll go with that. I was just curious to know if maybe I missed some grid feature but it looks like I didn't. Thanks again
MC23
MC23OPβ€’2d ago
I tried a solution with Flex, but I don't know how to avoid this (without media queries): it's ok for the element to take as much space as they need (so grow to 1), but I don't want that only one element wraps https://codepen.io/n00bCod3r/pen/EaxyvKJ
No description
Chooβ™šπ•‚π•šπ•Ÿπ•˜
The problem is your flex basis. You set a fixed pixel value instead of a percent. My original response was to the image and the the full text of what you said. The number of items on the last row depends on the total number of items.
MC23
MC23OPβ€’2d ago
This doesn't solve the problem on the possibility of only one element wrapped, and even auto doesn't. I was thinking about some emulation of grid's minmax but I don't think it's possible
MC23
MC23OPβ€’2d ago
Ohh this looks interesting, saved to my favorites and notion :D
Chooβ™šπ•‚π•šπ•Ÿπ•˜
That solution is still not ideal, because it requires specifying the number of columns in advance.
MC23
MC23OPβ€’2d ago
Oh I see
MC23
MC23OPβ€’2d ago
This is my real use case, unfortunately I don't know how many elements on each tab there will be
No description
MC23
MC23OPβ€’2d ago
But I didn't like any grid combination I tried soo

Did you find this page helpful?