Back Home

The Layout Lab

Understanding CSS Grid Items

In CSS Grid Layout, the direct children of a grid container are called grid items. These items are the fundamental building blocks that you arrange and position within the grid. Mastering how to control these items is key to creating complex and flexible web page layouts.

Placement and Sizing

Grid items are automatically placed into grid cells based on their source order. However, you have significant control over their placement and how they span across cells. This is achieved using properties like grid-column, grid-row, grid-area, and their associated shorthand properties.

Let's look at a simple example. Imagine you have a grid container set up, and its direct children become grid items. We can influence their behavior within the grid structure.

Item 1 (spans 2)
Item 2
Item 3 (spans 2 rows)
Item 4
Item 5 (spans 3)
Item 6

Key Properties for Grid Items

  • grid-column-start / grid-column-end: Define the starting and ending grid lines for an item.
  • grid-row-start / grid-row-end: Define the starting and ending grid lines for an item.
  • grid-column: Shorthand for grid-column-start and grid-column-end.
  • grid-row: Shorthand for grid-row-start and grid-row-end.
  • grid-area: A shorthand to place an item in a specific grid area, defined by grid-row-start / grid-column-start / grid-row-end / grid-column-end.
  • justify-self: Aligns an item along the row axis within its grid area.
  • align-self: Aligns an item along the column axis within its grid area.

Auto-Placement Behavior

When you don't explicitly place an item, the grid container will automatically position it into the next available cell. This behavior can be influenced by grid-auto-flow properties on the container, but the individual item's order in the HTML source is crucial for this default placement.

Understanding these properties empowers you to create precise and dynamic layouts where each element has a defined place and size within the overall grid structure.