30

I successfully created a responsive grid as the snippet shows.

At the moment, I see the boxes aligned to the left.

How can I put the container in the center of the page without having to use padding?

I tried using margin:auto but it did not work.

.container{
    display:flex;
    flex-wrap:wrap;
    text-align:center;
    margin:auto;
}
.box{
    width:100%;
    max-width:30%;
    border:1px solid red;
    margin:5px;
}
@media only screen and ( max-width:768px ){ 
    .box{
        width:100%;
        max-width:40%;
        border:1px solid red;
    }
}
<section class="container">
    <div class="box">
        <h1>This is the first box</h1>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci voluptates, aut totam eaque possimus molestiae atque odio vero optio porro magni, quis culpa ea. Perferendis, neque, enim. Rem, quia, quisquam.</p>
    </div>
</section>

JS Fiddle.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Sidney Sousa
  • 3,378
  • 11
  • 48
  • 99

4 Answers4

25

Use justify-content: center; instead of margin: auto;:

.container {
  display: flex;
  flex-wrap: wrap;
  text-align: center;
  justify-content: center;
}
.box {
   width: 100%;
   max-width: 30%;
   border: 1px solid red;
   margin: 5px;
}
@media only screen and (max-width: 768px) { 
  .box {
    width: 100%;
    max-width: 40%;
    border: 1px solid red;
  }
}

See justifiy-content docs on MDN.

Updated JS Fiddle.

LW001
  • 2,452
  • 6
  • 27
  • 36
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
  • 3
    It centers perfectly. But I just noticed that the fourth box does not float to the left now.The idea is to have boxes being added from left to write as more articles come. For instance if I have 7 boxes,the seventh box should flow to the left and not to the center. Hope I made some sense. – Sidney Sousa Aug 11 '16 at 12:30
  • @WosleyAlarico Then you can use `margin: 0 auto;` with a fixed width, for example `width: 800px;`. See [JS Fiddle](https://jsfiddle.net/cx1svoky/9/). – Michał Perłakowski Aug 11 '16 at 12:35
  • It doesn't really center like I mentioned in other comments previously. But well,it seems to be the only option. – Sidney Sousa Aug 11 '16 at 12:51
3

just add this line to your original code:

.container{
  width: 100%
}

and put the div box into another div that have a margin: auto;

if you need 2 boxes in the same line add display: inline-table; to the box class

Randy
  • 9,419
  • 5
  • 39
  • 56
Alaa DAIRY
  • 31
  • 2
1

Use margin:0px auto; with a width.

For example:

.container{

display:flex;
flex-wrap:wrap;
text-align:center;
margin:0px auto;
width: 400px;
}
twodee
  • 606
  • 5
  • 24
0

So, if you want to have boxes being added from left to right, and the first box on the next row to be left aligned, you can use:`

.container {
  display:flex;
  flex-flow: row wrap;
  justify-content: flex-start;
  margin: 0 auto;
  border: 1px solid red;
}
.container:after {
  content: "";
  flex: auto;
}

Fiddle

Petya Naumova
  • 727
  • 2
  • 11
  • 23