I would love some eyes and opinions in this problem that I am facing with flexbox and a grid. I will have a variable number of items in a list (like in an image gallery), but the size of each item would be fixed. Now, I need to horizontally-center the list items, and have them always be aligned.
html {
box-sizing: border-box;
}
p {
padding: 1em;
}
.wrapper {
max-width: 100%;
min-height: 500px;
padding: 2em 0;
background: papayawhip;
border: 1px solid orange;
overflow: hidden;
display: flex;
justify-content: center;
}
ul {
display: flex;
background: mistyrose;
width: 100%;
justify-content: flex-start;
flex-wrap: wrap;
margin: 1em;
border: 1px solid blue;
padding: 0;
}
li {
display: block;
background: indianred;
width: 200px;
height: 200px;
margin: .1em;
/* padding: 1em; */
color: white;
font-size: 2rem;
text-align: center;
display: flex;
justify-content: center;
align-items: center;
align-self: stretch;
border: 1px solid azure;
}
li div {
display: inline-block;
}
<div class="wrapper">
<ul>
<li>
<div>
First
</div>
</li>
<li>
<div>
Second
</div>
</li>
<li>
<div>
Third
</div>
</li>
<li>
<div>
Fourth
</div>
</li>
<li>
<div>
Fifth
</div>
</li>
</ul>
</div>
I've been struggling to get it to work like that with Flexbox and was wondering if this would be a case where I need to use CSS Grid instead.
The desired behavior can be seen in the image below. Another way to think about the desired behavior is justify-content: flex-start
but the whole thing (from left-edge of left most list item to right edge of right-most list item) centered.
If this can't be achieved with Flexbox, is CSS grid then the way to go? Or can the same effect be achieved in a different way?