Center a grid container vertically and horizontally
Asked Answered
S

6

27

Surprisingly, I can't find anything about this online. Maybe I've missed something. How do you horizontally and vertically center your entire main wrapper with CSS Grid, and obviously, maintain responsiveness?

.wrapper {
  display: grid;
  grid-template-columns: minmax(100vw, 1fr);
  grid-template-rows: minmax(100vh, 1fr);
  align-items: center;
  justify-content: center;
}
<div class="wrapper">
  <div class="centerthis">loremloremlorem</div>
</div>

Using the CSS above, the div is centered vertically, but not horizontally.

The following CSS centers the div horizontally, but not vertically:

.maingrid {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
  align-items: center;
  justify-content: center;
}

.centerthis {
  align-self: center;
  justify-self: center;
}
<div class="maingrid">
  <div class="centerthis">loremloremlorem</div>
</div>
Stanfill answered 28/9, 2017 at 12:25 Comment(0)
R
33

Instead of justify-content: center use justify-items: center.

.wrapper {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 100vh;
  align-items: center;
  justify-items: center; /* adjusted */
}
<div class="wrapper">
  <div class="centerthis">loremloremlorem</div>
</div>

In your second example, you say:

The following CSS centers the div horizontally but not vertically.

That's because the container has no extra height. The row is the height of the content. Instead of grid-template-rows: 1fr try something like grid-template-rows: 100vh.

.maingrid {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 100vh;
  align-items: center;
  justify-content: center;
}

.centerthis {
  align-self: center;
  justify-self: center;
}

body {
  margin: 0;
}
<div class="maingrid">
  <div class="centerthis">loremloremlorem</div>
</div>

More here:

Reidreidar answered 28/9, 2017 at 12:35 Comment(0)
L
7

Instead of using

  align-items: center;
  justify-items: center;

you can use

  place-items: center;

.wrapper {
        display: grid;
        grid-template-columns: 1fr;
        grid-template-rows: 100vh;
        place-items: center;
        border: 2px solid;
       }
<div class="wrapper">
  <div class="centerthis">loremloremlorem</div>
</div>
Lampoon answered 17/11, 2021 at 11:26 Comment(0)
C
0

Example for 4 items in a 2x2 grid.

If you want to make your items smaller within the outer container; change width: 100% to whatever you like and add margin-top: and margin-left: in your CSS (for example: width: 70%, margin-top: 15%, margin-left: 15%. Margins being half of your left-over space, to keep things centered)

#wrapper {
  position: absolute; 
  display: grid;
  width: 100%;
  grid-template-columns: 1fr 1fr;
}
<div id="wrapper">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>
Carmen answered 20/5, 2021 at 9:34 Comment(0)
P
0

Only add margin auto in class "wrapper", like margin: 0 auto;

With this, you center your container (wrapper) with respect to its direct parent, usually "body"

Hope it helps.

Prurigo answered 30/4, 2023 at 12:22 Comment(0)
T
-4

it's better if you do

.wrapper {
  display: flex;
  flex-direction: row; // Not really necassary though
  justify-content: center; // Or you could use "space-around"
  align-items: center;
}
Teodora answered 28/9, 2017 at 12:40 Comment(0)
S
-7

I suggest you use flex-box, it is a new layout mode in CSS3.

In this case we just give .maingrid display: flex, a height and center the content.

Then let .centerthis align itself to the middle of his father element like below.

Hope it help,

.maingrid {
  display: flex;
  height: 100px;
  justify-content: center;
}

.centerthis {
  align-self: center;
}
<div class="maingrid">
  <div class="centerthis">loremloremlorem</div>
</div>
Shaunshauna answered 28/9, 2017 at 16:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.