I am trying to have the grid cells stay centred when I resize the window, but they end up just staying where they are rather than moving closer together. I am trying to replicate the https://rickandmortyapi.com/. I thought it might be better to share the code to my project through codepen: https://codepen.io/Alessandro-Garcia/pen/RwqNaPq
This is the code responsible for taking care of the grid:
main {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(589.19px, auto));
gap: 0;
overflow-x: hidden;
justify-items: center;
align-items: center;
}
And this is the code responsible for the individual cells:
article{
margin: auto;
}
2
Answers
You can achieve what you want using
flexbox
instead ofgrid
. Try and add this on yourmain
element.Here is also the full example if you want to look around the code too. Rick and Morty codepen. Hope that helps! 🙂
For further information on
flexbox
since you mentioned you are new toCSS
have a look here.You can use
justify-content: center
on your grid container. CSS tricks has a good cheat sheet for grid.I’ve edited your codepen and literally just added
justify-content: center;
tomain
. Looks to work ok.Example below: