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>
First, we set the section to have configured to display: grid . This CSS property sets the element to render using CSS Grid. Now each direct child element will be a grid item placed in a column. To align the item horizontally within the grid, we use the justify-content property and set it to center .
For vertical alignment, set the parent element's width / height to 100% and add display: table . Then for the child element, change the display to table-cell and add vertical-align: middle . For horizontal centering, you could either add text-align: center to center the text and any other inline children elements.
Use margin: auto to vertically and horizontally center grid items. To center the content of grid items you need to make the item into a grid (or flex) container, wrap anonymous items in their own elements (since they cannot be directly targeted by CSS), and apply the margins to the new elements.
To center an image with CSS Grid, wrap the image in a container div element and give it a display of grid . Then set the place-items property to center. P.S.: place-items with a value of center centers anything horizontally and vertically.
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:
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>
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