Centering a Div Just Got Easier with display: grid;
Centering a But now, thanks to the simplicity and power of CSS Grid, centering an element is incredibly easy. In fact, with just two lines of code, you can align your content perfectly in the middle of the page—both horizontally and vertically. Previously, centering a Now, with CSS Grid, centering is as simple as it gets! Here’s the magic formula:
Why Was Centering a Div Hard?
position: absolute
and transform
, flexbox with a few properties, or margin tricks. These methods worked, but they often involved a bit more CSS than needed, especially when aiming for simple layouts.
The Simplest Way to Center a Div with CSS Grid
.centered-div-container {
display: grid;
place-items: center;
height: 100vh; /* Full viewport height */
}