CSS Grid Alignment

Discover the properties that give you total control over layout: `justify-items`, `align-items`, `justify-content`, and `gap`.

Lesson ProgressStep 1 of 8
1
2
3
4
0 EXP

Welcome! Let's build a CSS Grid. We start by setting 'display: grid' on a container.

.container {
  display: grid;
}

Spacing: `gap`, `row-gap`, `column-gap`

The `gap` property is the simplest way to add space, or "gutters", between your grid rows and columns.

  • `gap: 20px;` - Sets both `row-gap` and `column-gap` to `20px`.
  • `gap: 20px 10px;` - Sets `row-gap` to `20px` and `column-gap` to `10px`.
  • You can also use the individual `row-gap` and `column-gap` properties.
.container {
  display: grid;
  gap: 1rem;
}

System Check

How would you set a 20px gap between rows and a 10px gap between columns using the shorthand?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🚀
Space Navigator

Use the `gap` property to space out a grid.

🎯
Alignment Whiz

Correctly align items on both the block and inline axes.

✍️
Shorthand Expert

Prove your mastery of the `place-items` shorthand.

Mission: Build a Centered Grid

Create a grid container that uses `gap` and aligns its items to the `center` (both horizontally and vertically). Our AI assistant will provide real-time feedback.

A.D.A. Feedback:

> Awaiting input...

Challenge: Order the Properties

While CSS order doesn't always matter, a logical flow is best practice. Drag these properties into a logical order for creating a centered grid.

grid-template-columns: 1fr 1fr;
display: grid;
place-items: center;

Challenge: Complete the Syntax

Fill in the missing property names to create a centered grid with 1rem of space.

.container {display:;: 1rem;: center;}

Consult A.D.A.

Community Holo-Net

Beyond the Cells: The Complete Guide to CSS Grid Alignment

Creating a grid with `display: grid` is only the first step. The true power of CSS Grid lies in its sophisticated alignment capabilities. Mastering alignment allows you to control not only the space *between* items but also the position of items *within* their cells and the position of the *entire grid* within its container.

The most crucial concept to understand is the difference between aligning **items** and aligning **content (tracks)**.

  • Item Alignment: Controls where an item sits *inside* its designated grid cell. (e.g., `justify-items`, `align-items`).
  • Track Alignment (Content): Controls the alignment of the grid tracks themselves *within* the grid container, which is only relevant if the total size of the tracks is less than the size of the container. (e.g., `justify-content`, `align-content`).

1. Spacing the Grid: `gap`, `row-gap`, `column-gap`

Before aligning, let's set the space *between* our tracks. The `gap` property is the modern, simple way to do this.

  • gap: 20px;: Sets a `20px` gap for both rows and columns.
  • gap: 30px 10px;: Sets a `30px` `row-gap` and a `10px` `column-gap`.
  • row-gap & column-gap: You can use these longhand properties individually. (These were formerly `grid-row-gap` and `grid-column-gap`, but those are now deprecated).

2. Aligning *Items* (Inside their Cells)

These properties are set on the **grid container** and define the *default* alignment for all items within it.

  • justify-items: Aligns items along the **inline (row) axis**. Values include `start`, `end`, `center`, and `stretch` (default).
  • align-items: Aligns items along the **block (column) axis**. Values include `start`, `end`, `center`, `stretch` (default), and `baseline`.

What if you want one *specific* item to be different? You can override these defaults on an individual **grid item** using:

  • justify-self: Overrides `justify-items` for a single item.
  • align-self: Overrides `align-items` for a single item.

3. Aligning the *Grid Itself* (Track Alignment)

This is what trips many people up. These properties do *nothing* if your grid tracks (`1fr 1fr`, etc.) already fill up 100% of the container.

But, if your container is `500px` wide and your columns are `100px 100px`, you have `300px` of empty space. Where does the grid sit? **This** is what `-content` properties control.

Example: Tracks fill the space

.container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  /* justify-content has no effect */
}

Example: Tracks have free space

.container {
  display: grid;
  height: 300px;
  grid-template-rows: 50px 50px;
  /* align-content now works! */
  align-content: center;
}
  • justify-content: Aligns the entire grid of tracks along the **inline (row) axis**. Values: `start`, `end`, `center`, `space-between`, `space-around`, `space-evenly`.
  • align-content: Aligns the entire grid of tracks along the **block (column) axis**. Values: `start`, `end`, `center`, `space-between`, `space-around`, `space-evenly`.

4. The Almighty Shorthands

Writing all these properties is tedious. Use these shorthands to save time.

  • place-items: Shorthand for `align-items` and `justify-items`.
    place-items: center start; (aligns center, justifies start)
    place-items: center; (aligns center, justifies center)
  • place-content: Shorthand for `align-content` and `justify-content`.
    place-content: center space-between;
  • place-self: Shorthand for `align-self` and `justify-self`.
    place-self: end center;
Key Takeaway: To center a single item perfectly in its cell, use `place-items: center` on the container. To center the *entire grid* in the middle of a larger space (like a webpage), use `place-content: center` (assuming your tracks don't fill 100% of the space).

CSS Grid Alignment Glossary

Grid Container
The HTML element on which `display: grid` is applied. It's the parent of all grid items.
Grid Item
A direct child element of a grid container.
Grid Cell
The smallest unit of a grid, defined by the intersection of a row track and a column track. An item is placed *within* a cell.
Grid Track
The space between two adjacent grid lines. A track is essentially a row or a column.
Inline Axis (Row Axis)
The horizontal axis (in a standard left-to-right document). Properties with `justify-` control alignment on this axis.
Block Axis (Column Axis)
The vertical axis (in a standard top-to-bottom document). Properties with `align-` control alignment on this axis.
`gap`
A shorthand property for `row-gap` and `column-gap`. Defines the size of the "gutters" between grid tracks.
`justify-items`
Set on the grid container. Controls the default alignment of all *items* on the **inline (row) axis** *within their grid cells*.
`align-items`
Set on the grid container. Controls the default alignment of all *items* on the **block (column) axis** *within their grid cells*.
`place-items`
A shorthand for `align-items` and `justify-items`. Example: `place-items: center;`
`justify-self`
Set on a grid item. Overrides the `justify-items` property for that single item.
`align-self`
Set on a grid item. Overrides the `align-items` property for that single item.
`justify-content`
Set on the grid container. Aligns the **entire grid of tracks** on the **inline (row) axis** when there is extra space in the container.
`align-content`
Set on the grid container. Aligns the **entire grid of tracks** on the **block (column) axis** when there is extra space in the container.
`place-content`
A shorthand for `align-content` and `justify-content`.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching CSS and building robust and accessible web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest CSS Grid specifications and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!