Skip to main content

CSS Grid Items

Inside a grid container sit one or more grid items. Any element that is a direct child of the container becomes one automatically.

Example

Five grid items inside one grid container:

Prompt
Create a CSS grid container with five grid items numbered 1 through 5. The container should have three columns of equal width. Style each item with a background color, padding, and centered text.
Output: complete HTML page

CSS Sizing/Spanning Grid Items

A single grid item is not limited to one cell: it can stretch across several columns or rows.

Where it starts and ends is controlled by these properties:

  • grid-column-start — the column-line where the item begins
  • grid-column-end — the column-line where the item finishes
  • grid-column — shorthand combining grid-column-start and grid-column-end
  • grid-row-start — the row-line where the item begins
  • grid-row-end — the row-line where the item finishes
  • grid-row — shorthand combining grid-row-start and grid-row-end

When placing a grid item, you refer to line numbers: column-lines run between columns, and row-lines run between rows.


CSS grid-column-start and grid-column-end

A grid item begins at the column-line named by grid-column-start and finishes at the one named by grid-column-end.

Example

The first grid item here runs from column-line 1 to column-line 3:

Prompt
Create a CSS grid layout with five items. The grid has three columns. Make the first item start at column-line 1 and end at column-line 3, so it spans two columns. Style items with background colors, padding, and centered text.
Output: complete HTML page

The CSS grid-column Property

Writing grid-column saves you from setting grid-column-start and grid-column-end separately, since it is their shorthand.

Example

Starting at column-line 1, the first grid item here spans 2 columns:

Prompt
Create a CSS grid layout with five items in a three-column grid. Use the grid-column shorthand property on the first item to start at column-line 1 and span 2 columns. Style items with background colors, padding, and centered text.
Output: complete HTML page

CSS grid-row-start and grid-row-end

Rows work the same way: grid-row-start names the row-line where a grid item begins, and grid-row-end the one where it finishes.

Example

The first grid item here runs from row-line 1 to row-line 3:

Prompt
Create a CSS grid layout with five items in a three-column grid. Make the first item start at row-line 1 and end at row-line 3, so it spans two rows. Style items with background colors, padding, and centered text.
Output: complete HTML page

The CSS grid-row Property

grid-row is the shorthand that replaces grid-row-start and grid-row-end.

Example

Starting at row-line 1, the first grid item here spans 2 rows:

Prompt
Create a CSS grid layout with five items in a three-column grid. Use the grid-row shorthand property on the first item to start at row-line 1 and span 2 rows. Style items with background colors, padding, and centered text.
Output: complete HTML page

Combine grid-column and grid-row

To span columns and rows at the same time, give a grid item both grid-column and grid-row.

Example

The first grid item here starts at column-line 1 and row-line 1, spanning 2 columns and 2 rows:

Prompt
Create a CSS grid layout with six items in a three-column grid. Use grid-column and grid-row on the first item so it starts at column-line 1 and spans 2 columns, and starts at row-line 1 and spans 2 rows. Style items with background colors, padding, and centered text.
Output: complete HTML page

All CSS Grid Item Properties

This table collects every CSS property that grid items can use:

Grid item properties table

index.html
<table>
  <tr>
    <th>Property</th>
    <th>Description</th>
  </tr>
  <tr>
    <td>align-self</td>
    <td>Aligns the content for a specific grid item along the column axis</td>
  </tr>
  <tr>
    <td>grid-area</td>
    <td>A shorthand property for the grid-row-start, grid-column-start, grid-row-end and the grid-column-end properties</td>
  </tr>
  <tr>
    <td>grid-column</td>
    <td>A shorthand property for the grid-column-start and the grid-column-end properties</td>
  </tr>
  <tr>
    <td>grid-column-end</td>
    <td>Specifies where to end the grid item</td>
  </tr>
  <tr>
    <td>grid-column-start</td>
    <td>Specifies where to start the grid item</td>
  </tr>
  <tr>
    <td>grid-row</td>
    <td>A shorthand property for the grid-row-start and the grid-row-end properties</td>
  </tr>
  <tr>
    <td>grid-row-end</td>
    <td>Specifies where to end the grid item</td>
  </tr>
  <tr>
    <td>grid-row-start</td>
    <td>Specifies where to start the grid item</td>
  </tr>
  <tr>
    <td>justify-self</td>
    <td>Aligns the content for a specific grid item along the row axis</td>
  </tr>
  <tr>
    <td>place-self</td>
    <td>A shorthand property for the align-self and the justify-self properties</td>
  </tr>
</table>