I'm trying to center a group of wrapped flex items. The HTML looks like this:
main {
background-color: blue;
width: 390px;
display: flex;
justify-content: center;
}
.container {
background-color: red;
display: inline-flex;
flex-wrap: wrap;
}
.a1 {
color: grey;
width: 100px;
height: 200px;
background-color: green;
border: 1px solid black;
}
<main>
<div class="container">
<div class="a1"></div>
<div class="a1"></div>
<div class="a1"></div>
<div class="a1"></div>
</div>
</main>
The above looks like this:
The green boxes are wrapped correctly, but they, as a whole, are not centered in the red area,
without defining a width on the .container
because the red block can have any size and I want to fit as many blocks next to each other as possible.
Any suggestions how to center the wrapped green blocks?
UPDATE: According to this 2 year old post it is not possible. So in my case I have to use javascript to fix this. But maybe I can use CSS Grid Layout