how to center grid item horizontally

I'm using grid to create 3 columns. I want to center grid items of last row horrizontally if there is no enough items for the row. How can I solve this problem? I tried justify-content:center but it not work
No description
10 Replies
Chris Bolson
Chris Bolson2mo ago
The whole point of grid is that the cells are aligned. To be able to do this using grid you will need to double the number of grid columns, place each "card" in 2 columns and then, somehow, detect if there are less than 3 items on the last row, update their column start position accordingly to offset them. This of course will change depending on if there are 1 or 2 items. The key thing here here is just how to detect if there are less than 3 columns. Maybe with a combination of :not(), :has(), :nth-child() and :empty 🤔 (pops off to Codepen) Alternatively you could use flex...
empty
empty2mo ago
Sorry, Im not good at css. If I use flex how can I divide the column?
Chris Bolson
Chris Bolson2mo ago
OK, that was actually simpler than I expected. You have got to love the power of :has() (though this case might also have been possible using sibling selectors) Note, this solution as is will only work if you have a known number of rows (ie 2 in this case):
.grid:not(:has(:nth-child(6))) > div:nth-child(4){
grid-column: 2 / span 2;
}
.grid:not(:has(:nth-child(5))) > div:nth-child(4){
grid-column: 3 / span 2;
}
.grid:not(:has(:nth-child(6))) > div:nth-child(4){
grid-column: 2 / span 2;
}
.grid:not(:has(:nth-child(5))) > div:nth-child(4){
grid-column: 3 / span 2;
}
Here is a working example: https://codepen.io/cbolson/pen/MWMzLxa
empty
empty2mo ago
Ok I will look into it
Chris Bolson
Chris Bolson2mo ago
I have added some comments to help explain what is happening. I have also added a flex version. The advantage of using flex is that it will work for any number of rows whereas the grid method requires knowing how many rows we have (at least in my current version). Bear in mind that flex and grid have other differences which might influence which is better for your use case.
ἔρως
ἔρως2mo ago
wouldnt subgrid help here?
empty
empty2mo ago
It seems like using flex in this case is more easier
Alex
Alex2mo ago
Some great articles about achieving this via grid: - https://css-irl.info/controlling-leftover-grid-items/ - https://ryanmulligan.dev/blog/grid-stacks/ But yes, I agree flex is probably the cleaner solution, especially for a generalised use case where you don't know the count
CSS { In Real Life } | Controlling Leftover Grid Items with Pseudo-selectors
CSS { In Real Life } | Controlling Leftover Grid Items with Pseudo-...
Center Items in First Row with CSS Grid
Stacking grid items so that an odd number of items appears horizontally centered in the first row instead of the last.
empty
empty2mo ago
@Alex @Chris Thanks guys
Chris Bolson
Chris Bolson2mo ago
Thanks for that. Using the last-child selector makes more sense 🤦‍♂️ Also defining the column end makes things easier. I have updated my pen to use this method. With this method there is no need to know the number of rows, even when using grid 👍
Want results from more Discord servers?
Add your server