Aligning with Precision: Alignment Properties in CSS Grid

Position your grid items exactly where you need them by mastering justify-items, align-items, and the gap property.

1
2
3
4

Welcome! Let's explore how to align items in a CSS Grid. First, we need a grid container.

.container {
display: grid;
grid-template-columns: 1fr 1fr;

}

Grid Gap: Defining Space

The grid-gap property (or the modern gap) is a shorthand for setting the space between grid rows and columns. For example, gap: 20px 10px; sets a 20px gap between rows and a 10px gap between columns.

Justify Items: Row Axis Alignment

justify-items aligns content inside a grid item along the **row (inline) axis**. Common values are start, end, center, and stretch (the default), which makes the item fill the entire width of its cell.

Align Items: Column Axis Alignment

align-items aligns content inside a grid item along the **column (block) axis**. It accepts the same values as justify-items: start, end, center, and stretch (the default).

The 'place-items' Shorthand

The place-items property is a convenient shorthand for setting both align-items and justify-items at once. The syntax is place-items: <align-items> <justify-items>;. If you provide only one value, it applies to both. For perfect centering, just use place-items: center;.

Practice Zone


Interactive Test 1: Drag & Drop

Arrastra en el orden correspondiente.


Arrastra las opciones:

gap
justify
align

Completa el código:

Defines the space between grid cells: grid-______: 10px;
Aligns items along the row axis: ______-items: start;
Aligns items along the column axis: ______-items: center;
Unlock with Premium

Interactive Test 2: Fill in the Blanks

Rellena los huecos en cada casilla.

.container {
  display: grid;
  gap: ;
  justify-items: ;
  align-items: ;
}
Unlock with Premium

Practice Example: Code Editor

Use the editor to make the grid items align to the end of the row axis and the start of the column axis. Give them a 20px gap.

Grid Element Alignment

PropertyDescription
grid-gapDefines the space between rows and columns in the grid.
justify-itemsAligns items inside their cells horizontally.
align-itemsAligns items inside their cells vertically.
justify-contentAligns the grid inside its container horizontally.
align-contentAligns the grid inside its container vertically.

* Write your CSS code and apply it to see the results.

Results:

Unlock with Premium

Knowledge Check

Which property controls the horizontal (row axis) alignment of items in a grid?


Unlock with Premium

Practical Grid Alignment Techniques

Grid alignment properties are the key to creating clean, professional, and responsive layouts. Let's move beyond theory and see how these tools solve common design problems with live examples.


1. The Holy Grail: Perfect Centering

Centering an element both vertically and horizontally used to be a challenge. With Grid, it's trivial. The combination of display: grid and place-items: center on a parent container is all you need.

.parent {
    display: grid;
    place-items: center;
    height: 200px; /* Needs a defined height */
}
Centered!

2. Card Layouts with Consistent Alignment

When creating a grid of cards, you might want the content inside each card to align differently. Use align-items to ensure text or buttons are consistently positioned, regardless of content height.

.card-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    align-items: end; /* Aligns all items to the bottom */
}
Short card
This is a much taller card

3. Full-Bleed vs. Centered Content

By default, grid items stretch to fill their cell. You can override this with justify-items. This is useful for creating sections where some elements span the full width while others are centered within their column.

.container {
    display: grid;
    justify-items: center; /* Center all items by default */
}
.full-width {
    justify-self: stretch; /* Override for this specific item */
}
Full-Width Header
Centered Content

Practical Takeaway: Master justify-items, align-items, and place-items to move from simply creating grids to artfully directing the flow and position of content within them. These properties are your most powerful tools for creating intentional, robust layouts.