In this article, we’ll look at four ways to center a div horizontally and vertically using CSS grid. Of course, these centering techniques can be used for any kind of element. We’ve seen before how to center elements horizontally and vertically using flexbox, and how to position them using transforms.
set up
First, let’s create a container with a simple box element inside that we’ll use to demonstrate these centering methods. Here is the HTML:
<article>
<div></div>
</article>
And this is the first CSS.
article {
width: 100%;
min-height: 100vh;
background: black;
display: grid;
}
div {
width: 200px;
background: yellow;
height: 100px;
}
In all examples, display: grid
property. This will <article>
Use an element as a grid container and generate a block-level grid for that container. (Here’s a demo template on CodePen if you want to try it out.)
Now let’s look at different ways to center a div.
1. Center a div using CSS Grid and place-self
My favorite way to center an element in a grid is to use place-self
property. (Learn more about.)
Centering a div is as simple as:
article {
display: grid;
}
div {
place-self: center;
}
look at the pen
Centering with Grid and place-self with SitePoint (@SitePoint)
with a code pen.
of place-self
The properties are align-self
(Vertical) and justify-self
(horizontal) property (useful for centering along one axis). You can try them out in this CodePen demo.
use place-self
It’s so simple that it’s an out-of-the-box solution. But this is not the only way to center elements using Grid. So let’s look at some other methods.
Advantages to use place-self
This means it can also be used to center other elements within the same container. (Add more div elements to the CodePen demo and see what happens.)
2. Center the div using CSS Grid, justify-content, align-items
Now let’s see what’s involved in using the Grid. justify-content
and align-items
Center the div.
of justify-content
The property is used to horizontally align the items in the container when the items do not use all the available space.There are many ways to configure justify-content
property, but here we just set center
.
like justify-content
properties, align-items
The property is used to align the content inside the container, but align the content vertically instead of horizontally.
Return to your test HTML and add the following code to the parent container.
article {
display: grid;
justify-content: center;
align-items: center;
}
look at the pen
Centering with grid, align-self and justify-self with SitePoint (@SitePoint)
with a code pen.
The obvious advantage of this method is that it requires less code as the centering is handled by the container. However, targeting a div through a container is also disadvantageous in some ways, as other elements within the container are also affected.
3. Center Divs with CSS Grid and Auto Margins
As usual, target the parent container display: grid
I also use divs to assign auto margins. margin: auto
This will cause the browser to automatically calculate the available space around the child div, split it vertically and horizontally, and center the div.
article {
display: grid;
}
div {
margin: auto;
}
look at the pen
CSS Grid with SitePoint (@SitePoint), centering elements using justify-content and align-items
with a code pen.
(As an aside, there are lots of other cool things you can do with CSS margins.)
4. Center Divs with CSS Grid and place-items
of place-items
Properties are used to align items vertically and horizontally within the grid. It can be used to center a div by targeting the container like this:
article {
display: grid;
place-items: center;
}
look at the pen
Center a Div using CSS Grid and Auto Margins with SitePoint (@SitePoint)
with a code pen.
like place-self
property, place-items
In this case, it’s a shorthand for two properties. justify-items
(horizontal) and align-items
(vertical). You can try them out in this CodePen demo.
in contrast to place-self
, place-items
is applied to the container, making it slightly less flexible.
Conclusion
Each of these methods allows you to center a div horizontally and vertically within its container.As I said, my preference is place-self
This is primarily because it applies to elements that are centered rather than containers.it is the same margin: auto
Method.Of course, if you just want to center the elements in one direction, you can also use either align-self
again justify-self
.
In the demo example I used an empty div, but of course you can add content to the div and centering will still work. Also, these centering techniques work on elements other than divs.
References: