Cory Rylan

My name is , Google Developer Expert, Speaker, Software Developer. Building Design Systems and Web Components.

Follow @coryrylan
CSS

How to Center in CSS with CSS Grid

Cory Rylan

- 4 minutes

Using CSS Grid, we can layout content in a two-dimensional grid with columns and rows. CSS grid is a fantastic CSS API to layout content on the web. Often we need to center content within a page or container. We can accomplish this in many different ways in CSS. In a previous post, we learned how to center using Flexbox; in this post, we will learn how to center content using CSS Grid.

In our example, we have two elements: a parent element and the child element. We want to center the child element within the width of the parent element container.

section {
  width: 200px;
  border: 1px solid #2d2d2d;
}

div {
  color: #fff;
  background: #2d2d2d;
  padding: 12px;
  display: inline-block;
}
Inline block item to be centered with CSS Grid

To center our item, we need to set the parent element to render as a CSS Grid.

section {
  width: 200px;
  border: 1px solid #2d2d2d;
  display: grid;
  justify-content: center;
}

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.

Inline block item centered with CSS Grid

With justify-content we can align the columns start, end, stretch or center.

Centering Vertically

Now that the element is centered horizontally, we can center the element vertically. Let's increase our parent container to be 200px high.

Inline block item to be vertically centered with CSS Grid

Similar to Flexbox, Grid columns will fill the space where they are defined. To center the item vertically we set align-content: center;.

section {
  display: grid;
  justify-content: center;
  align-content: center;
}
Inline block item vertically centered with CSS Grid

To center multiple items, we need to add a couple more CSS properties to the parent grid element.

<section>
  <div>1</div>
  <div>2</div>
  <div>3</div>
</section>
section {
  display: grid;
  justify-content: center;
  align-content: center;

  gap: 4px;
  grid-auto-flow: column;
}

The first property gap allows us to define space between grid items. The second property grid-auto-flow, will enable us to define the direction we want items to layout, such as from left to right or vertically stacked as rows.

Inline block item horizontally centered with CSS Grid

When using justify-content and align-content, we are aligning the columns relative to the parent Grid container. In our next example, we will see how we can align items relative to the column instead of the parent grid container.

Centering Items within a CSS Grid Column

CSS Grid is very powerful and gives us excellent grain control of our layouts. We learned how to center columns in a grid but now lets center items within a column.

For our example, we will create a grid layout with three columns and three items.

.item-center {
  display: grid;
  grid-auto-flow: column;
  gap: 4px;
  align-items: center;
  justify-items: center;
}

To center the items within a column, we use align-items and justify-items instead of the align-content and justify-content in our previous example. Using the item properties, we can align our items just like we did with our columns.

Inline block item horizontally centered with CSS Grid

If we use the dev tools and inspect the grid, we can see the outlined columns and our items centered relative to the assigned column.

Inline block item horizontally centered with CSS Grid

CSS Grid is a fantastic tool to use for layouts in our Web pages and Web Applications. Check out the full working demo below!

View Demo Code   
Twitter Facebook LinkedIn Email
 

No spam. Short occasional updates on Web Development articles, videos, and new courses in your inbox.

Related Posts

CSS

Design System Architecture - Managing CSS Themes

Learn how to use leverage CSS themes to create a flexible and efficient theming system in your design system architecture.

Read Article
CSS

Flow Charts with CSS Anchor Positioning

Learn how to use CSS Anchor Positioning to create flow charts and diagram with just CSS.

Read Article
CSS

Dynamic Contrast Layers with CSS Style Queries

Learn how to create contrasting layers with CSS style queries ensuring your UI is always the right contrast ratio.

Read Article