0

This may be a really stupid question but I'm having a hard time to find an answer on this: how do you center a grid itself horizontally? I can only seem to find how to center the items within the grid. Is there a "best practice" of this? Thanks

Lucas Verhoest
  • 265
  • 1
  • 5
  • 18

2 Answers2

2

you can use flex, so css would be like

display:flex 

Once you have this you can use justify contents center to align its vertically like

justify-content : center 

and if you want something to align horizontally then you can use

align-items:center

Lets say if you want to center an item vertically and horizontally then you can use

display: flex;
justify-content : center;
align-items : center;

and you will have the items , here's a link for the same https://www.w3schools.com/css/css3_flexbox.asp

let me know if this helps.

Karan Tewari
  • 498
  • 8
  • 20
0

flexbox is the easiest way to do it. You have to give the parent element:

display:flex;
justify-content: center;

In my case its the body tag which wraps my grid

body {
  margin: 40px;
  display: flex;
  justify-content: center;
}

.wrapper {
  display: grid;
  grid-template-columns: 100px 100px 100px;
  grid-gap: 10px;
  background-color: #fff;
  color: #444;
}

.box {
  background-color: #444;
  color: #fff;
  border-radius: 5px;
  padding: 20px;
  font-size: 150%;
}
<div class="wrapper">
  <div class="box a">A</div>
  <div class="box b">B</div>
  <div class="box c">C</div>
  <div class="box d">D</div>
  <div class="box e">E</div>
  <div class="box f">F</div>
</div>
d-h-e
  • 2,478
  • 1
  • 10
  • 18
  • Hi, thanks for your answer! Really appreciate it. I thought of this, but I wasn't sure if there was specifix alignment for a "grid". I was a bit afraid of combining flex and grid. I'll accept this as an answer in some minutes. thanks again – Lucas Verhoest Jan 26 '19 at 19:47