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>
DEMO
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
Not sure what kind of centering you want do
So a few options:
.container
justify-content: center;
to .container
insteadmain { background-color: blue; width: 390px; display: flex; } .container { background-color: red; display: flex; flex-wrap: wrap; justify-content:center; } .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>
.container
width
+ margin:auto
to .container
main { background-color: blue; width: 390px; display: flex; justify-content: center; } .container { background-color: red; display: inline-flex; flex-wrap: wrap; width: 300px; margin: auto; } .a1 { color: grey; width: 100px; height: 200px; background-color: green; border: 1px solid black; box-sizing: border-box }
<main> <div class="container"> <div class="a1"></div> <div class="a1"></div> <div class="a1"></div> <div class="a1"></div> </div> </main>
main { background-color: blue; width: 390px; display: flex; } .container { background-color: red; display: inline-flex; flex-wrap: wrap; width: 90%; margin: auto; justify-content: center; } .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>
If you just want to center the container give a fixed width to the container or make sure that the width of the container has the expected width.
main {
background-color: blue;
width: 390px;
display: flex;
justify-content: center;
}
.container {
background-color: red;
display: inline-flex;
flex-wrap: wrap;
width:306px;
}
.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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With