Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center a group of wrapped flex items [duplicate]

Tags:

html

css

flexbox

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:

enter image description here

The green boxes are wrapped correctly, but they, as a whole, are not centered in the red area,

enter image description here

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

like image 206
Jeanluca Scaljeri Avatar asked Apr 04 '17 13:04

Jeanluca Scaljeri


2 Answers

Not sure what kind of centering you want do

So a few options:

Option 1: just center the elements in .container

  • add justify-content: center; to .container instead

main {   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>

Option 2: center the .container

  • add some 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>

Option 3: center both from above options

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>
like image 96
dippas Avatar answered Sep 25 '22 07:09

dippas


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>
like image 44
Blackbam Avatar answered Sep 26 '22 07:09

Blackbam