Skip to main content

CSS Grid Container

A grid container holds one or more grid items arranged in columns and rows.

All direct child elements of a grid container automatically become grid items.

An element becomes a grid container when its display property is set to grid or inline-grid.

Example

Prompt
Create an HTML page with a grid container holding 5 numbered items (1 through 5). Use display: grid on the container.
Output: complete HTML page

Display Grid Property

A <div> element becomes a grid container when its display property is set to grid or inline-grid.

Block-level grid

Use display: grid to make a block-level grid container.

Prompt
Create an HTML page with a div container using display: grid that holds 5 numbered child items (1 through 5). Style each item with a background color and padding to show the block-level grid behavior.
Output: complete HTML page

Inline-level grid

Use display: inline-grid to make an inline-level grid container.

Prompt
Create an HTML page with a div container using display: inline-grid that holds 5 numbered child items (1 through 5). Style each item with a background color and padding to show the inline-level grid behavior.
Output: complete HTML page

CSS Grid Container Properties

Here is a list of all CSS properties that can be used on a grid container:

  • align-content — vertically aligns the grid inside the container.
  • align-items — aligns items in the grid.
  • column-gap — sets the gap between columns.
  • display — sets the element as a grid container.
  • gap — a shorthand for row-gap and column-gap.
  • grid — a shorthand for multiple grid properties.
  • grid-auto-columns — sets the size of auto-generated columns.
  • grid-auto-flow — controls how auto-placed items are inserted.
  • grid-auto-rows — sets the size of auto-generated rows.
  • grid-template — a shorthand for defining rows, columns, and areas.
  • grid-template-areas — defines named grid areas.
  • grid-template-columns — sets the number and size of columns.
  • grid-template-rows — sets the number and size of rows.
  • justify-content — horizontally aligns the grid inside the container.
  • justify-items — aligns items along the row axis.
  • place-content — a shorthand for align-content and justify-content.
  • place-items — a shorthand for align-items and justify-items.
  • row-gap — sets the gap between rows.