CSS grid Property

The CSS grid property is a shorthand that defines the rows, columns, named areas, and automatic track behavior of a grid container.

It combines six grid-related properties into a single declaration, allowing developers to create explicit grid layouts or configure automatically generated tracks. The shorthand is useful for page structures, dashboards, galleries, and other layouts that require coordinated rows and columns. It does not set the spacing between tracks, which is controlled separately using gap, row-gap, or column-gap.

grid Syntax

The grid shorthand accepts the syntax of grid-template or one of two automatic grid forms.

Basic Syntax

selector {
  grid: none;
}

Define Rows and Columns

.container {
  display: grid;
  grid: 100px 200px / 1fr 2fr;
}

The values before the slash define rows. The values after the slash define columns.

Define Equal Columns

.container {
  display: grid;
  grid: auto / repeat(3, 1fr);
}

Define Named Areas

.container {
  display: grid;
  grid:
    "header header" 80px
    "sidebar main" 1fr
    "footer footer" 60px
    / 200px 1fr;
}

Automatic Rows

.container {
  display: grid;
  grid: auto-flow 100px / repeat(3, 1fr);
}

Automatic Columns

.container {
  display: grid;
  grid: 100px 100px / auto-flow 150px;
}

Dense Automatic Placement

.container {
  display: grid;
  grid: auto-flow dense 100px / repeat(3, 1fr);
}

CSS-Wide Keywords

.example {
  grid: initial;
}
.example {
  grid: inherit;
}
.example {
  grid: unset;
}
.example {
  grid: revert;
}
.example {
  grid: revert-layer;
}

The grid property does not automatically establish a grid formatting context. The element must also use display: grid or display: inline-grid.

Property Values

The grid shorthand supports explicit track definitions, named grid areas, and automatic track generation.

Value or Syntax Description
none Removes explicit grid track and area definitions while resetting the other shorthand components to their initial values.
<grid-template> Defines explicit rows, columns, and optionally named grid areas.
auto-flow Configures automatic placement and implicit track generation in the selected direction.
dense Allows the auto-placement algorithm to fill earlier available spaces when possible.
<track-size> Defines a track size using a length, percentage, flexible fraction, or supported sizing function.
<line-names> Defines named grid lines using square brackets.
<string> Defines named grid areas using quoted strings.
inherit Inherits the shorthand's longhand values from the parent element.
initial Resets the shorthand's longhand properties to their initial values.
unset Resets the shorthand's non-inherited longhand properties to their initial values.

Property Characteristics

Characteristic Value
Initial value See the initial values of the individual longhand properties
Inherited No
Applies to Grid containers
Computed value See the individual longhand properties
Animation type See the individual longhand properties
Percentages Depend on the track-sizing component
Shorthand Yes, for six grid properties

Because grid is a shorthand, its computed values and animation behavior depend on the individual longhand properties it sets.

Shorthand Properties

The grid shorthand sets six grid-related properties.

Longhand Property Purpose
grid-template-rows Defines explicit grid row tracks.
grid-template-columns Defines explicit grid column tracks.
grid-template-areas Defines named grid areas.
grid-auto-rows Defines the size of implicitly generated rows.
grid-auto-columns Defines the size of implicitly generated columns.
grid-auto-flow Controls the automatic placement direction and dense packing behavior.

Longhand Example

.container {
  display: grid;
  grid-template-rows: 100px 200px;
  grid-template-columns: 1fr 2fr;
}

Equivalent Shorthand

.container {
  display: grid;
  grid: 100px 200px / 1fr 2fr;
}

Both examples define the same explicit rows and columns. The shorthand also resets the remaining grid longhands to their initial values.

Properties Not Included

The grid shorthand does not include gap, row-gap, column-gap, or grid item placement properties such as grid-area.

Important Consideration

Use individual longhand properties when changing only one part of an existing grid layout. A shorthand declaration can reset previously configured values.

How It Works

CSS Grid divides a container into rows and columns. Grid items are then positioned within the resulting cells or areas.

Establish a Grid Container

.container {
  display: grid;
}

The display: grid declaration creates a grid formatting context for the element's direct children.

Define the Grid Structure

.container {
  display: grid;
  grid: 100px 100px / 1fr 1fr 1fr;
}

This creates two explicit rows and three explicit columns.

Place Grid Items

Grid items can be placed automatically according to the grid's auto-placement algorithm or positioned explicitly using grid lines and named areas.

Explicit Tracks

Tracks declared through grid-template-rows and grid-template-columns belong to the explicit grid.

Implicit Tracks

When items require additional tracks outside the explicit grid, the browser creates implicit rows or columns.

Track Sizing

Track sizes can use fixed lengths, flexible fractions, automatic sizing, or functions such as minmax().

Important Consideration

The grid structure is applied to direct grid items. Descendants nested inside a grid item do not automatically become items of the outer grid.

Explicit Grid Layout

An explicit grid contains the rows and columns defined directly by the author.

Two-Column Grid

.container {
  display: grid;
  grid: auto / 1fr 1fr;
}

This defines one explicit row with automatic sizing and two equal-width columns. Additional rows may be generated when needed.

Three-Column Grid

.container {
  display: grid;
  grid: auto / repeat(3, 1fr);
}

Fixed and Flexible Columns

.container {
  display: grid;
  grid: auto / 220px 1fr;
}

The first column has a fixed width of 220 pixels. The second receives a flexible share of the remaining available space.

Multiple Rows

.container {
  display: grid;
  grid: 80px 1fr 60px / 200px 1fr;
}

This creates three explicit rows and two explicit columns.

Important Consideration

Fractional tracks distribute available space, but their minimum sizing behavior can prevent content from shrinking as expected. Use minmax(0, 1fr) when a flexible track must be allowed to shrink below its automatic minimum.

Defining Rows and Columns

In the explicit grid syntax, a forward slash separates row definitions from column definitions.

Rows Before the Slash

.container {
  display: grid;
  grid: 100px 150px / 1fr 1fr;
}

The grid has two rows with heights of 100 and 150 pixels, followed by two equal flexible columns.

Automatic Row Height

.container {
  display: grid;
  grid: auto auto / 1fr 2fr;
}

Both explicit rows use automatic sizing, while the columns use a 1-to-2 flexible track ratio.

Minimum and Maximum Row Size

.container {
  display: grid;
  grid: minmax(100px, auto) 1fr / 200px 1fr;
}

Named Grid Lines

.container {
  display: grid;
  grid:
    [header-start] 80px
    [header-end content-start] 1fr
    [content-end]
    / [sidebar-start] 200px [main-start] 1fr [main-end];
}

Square brackets define line names that can be referenced by grid item placement properties.

Important Consideration

The slash belongs to the grid shorthand syntax. It is not a mathematical division operator.

Named Grid Areas

Named grid areas allow a layout to be described using readable labels rather than numeric grid lines.

Page Layout

.page-layout {
  display: grid;
  grid:
    "header header" 80px
    "sidebar main" 1fr
    "footer footer" 60px
    / 220px 1fr;
}

Assign Grid Items

.page-header {
  grid-area: header;
}
.page-sidebar {
  grid-area: sidebar;
}
.page-main {
  grid-area: main;
}
.page-footer {
  grid-area: footer;
}

HTML Structure

<div class="page-layout">
  <header class="page-header">Header</header>
  <aside class="page-sidebar">Sidebar</aside>
  <main class="page-main">Main Content</main>
  <footer class="page-footer">Footer</footer>
</div>

Empty Grid Cells

.container {
  display: grid;
  grid:
    "header header" 80px
    "sidebar ." 1fr
    / 200px 1fr;
}

A period represents an unnamed grid cell that does not belong to a named area.

Area Requirements

Each row of an area template must describe the same number of cells. Every named area must form one rectangular region.

Important Consideration

Named grid areas control visual placement, not the underlying document order. Keep the HTML source order meaningful for keyboard navigation and assistive technologies.

Implicit Grid Layout

The implicit grid contains tracks that the browser creates when the explicit grid does not provide enough space for all grid items or their specified positions.

Automatic Row Generation

.container {
  display: grid;
  grid: auto-flow 100px / repeat(3, 1fr);
}

The shorthand configures row-based automatic placement, gives implicit rows a size of 100 pixels, and defines three explicit columns.

Automatic Column Generation

.container {
  display: grid;
  grid: 100px / auto-flow 150px;
}

This defines one explicit row and configures automatic column placement with implicit columns sized at 150 pixels.

Equivalent Longhand Properties

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-flow: row;
  grid-auto-rows: 100px;
}

Implicit Track Sizes

Implicit tracks can use fixed or content-based sizes. Their dimensions are controlled by grid-auto-rows and grid-auto-columns.

Important Consideration

Implicit tracks are not necessarily identical to explicitly defined tracks. Their sizes depend on the automatic sizing rules and the values configured for implicit tracks.

Automatic Placement

Automatic placement determines how grid items without explicit positions are assigned to available cells.

Row-Based Placement

.container {
  display: grid;
  grid: auto-flow / repeat(3, 1fr);
}

Items are placed in row order, creating additional rows when necessary.

Column-Based Placement

.container {
  display: grid;
  grid: 100px 100px / auto-flow;
}

Items are placed in column order, creating additional columns when necessary.

Dense Packing

.container {
  display: grid;
  grid: auto-flow dense / repeat(4, 1fr);
}

The dense keyword allows later items to occupy earlier unfilled spaces when their dimensions fit.

Sparse Placement

Without dense, the auto-placement algorithm generally progresses forward rather than revisiting earlier gaps.

Important Consideration

Dense packing can produce a visual order that differs from the source order. Avoid relying on visual placement alone to communicate the reading or interaction sequence.

The fr Unit

The fr unit represents a fraction of the available space assigned to flexible grid tracks.

Equal Columns

.container {
  display: grid;
  grid: auto / 1fr 1fr 1fr;
}

Unequal Columns

.container {
  display: grid;
  grid: auto / 1fr 2fr;
}

The second column receives twice the flexible share of the first, subject to track minimum sizing constraints.

Fixed and Flexible Tracks

.container {
  display: grid;
  grid: auto / 250px 1fr;
}

Three-Part Layout

.container {
  display: grid;
  grid: auto / 200px 2fr 1fr;
}

Allow Flexible Tracks to Shrink

.container {
  display: grid;
  grid: auto / 200px minmax(0, 1fr);
}

The minmax(0, 1fr) declaration allows the flexible track to shrink below the automatic minimum size it would otherwise use.

Important Consideration

Fractional units do not always produce equal visible column widths when content-based minimum sizes constrain the tracks.

repeat() and minmax()

The repeat() and minmax() functions help define reusable and flexible grid track sizes.

Repeat Equal Columns

.container {
  display: grid;
  grid: auto / repeat(4, 1fr);
}

Repeat Fixed Columns

.container {
  display: grid;
  grid: auto / repeat(3, 150px);
}

Minimum and Maximum Track Size

.container {
  display: grid;
  grid: auto / repeat(3, minmax(150px, 1fr));
}

Automatic Column Count

.container {
  display: grid;
  grid: auto / repeat(auto-fit, minmax(min(100%, 180px), 1fr));
}

The auto-fit keyword repeats tracks according to the available width and collapses empty repeated tracks when applicable.

auto-fill vs. auto-fit

The auto-fill keyword retains empty repeated tracks, while auto-fit can collapse them so occupied tracks use the available space.

Important Consideration

Automatic repetition is supported in explicit track definitions. It is not a general replacement for the sizing rules of implicit grid tracks.

Grid Spacing

The grid shorthand defines the grid structure but does not set the spacing between rows or columns.

Using gap

.container {
  display: grid;
  grid: auto / repeat(3, 1fr);
  gap: 20px;
}

Different Row and Column Gaps

.container {
  display: grid;
  grid: auto / repeat(3, 1fr);
  gap: 10px 24px;
}

The first value controls row gaps. The second controls column gaps.

Separate Gap Properties

.container {
  display: grid;
  grid: auto / repeat(3, 1fr);
  row-gap: 12px;
  column-gap: 20px;
}

Shorthand Independence

.container {
  display: grid;
  gap: 20px;
  grid: auto / repeat(3, 1fr);
}

The gap value remains in effect because it is not included in the grid shorthand.

Important Consideration

Grid gaps create spacing between tracks. They do not add padding around the outer edges of the grid container.

grid vs. grid-template

The grid and grid-template properties are related shorthands, but they affect different sets of longhand properties.

grid-template

.container {
  display: grid;
  grid-template: 100px 200px / 1fr 2fr;
}

The grid-template shorthand sets explicit rows, columns, and named areas.

grid

.container {
  display: grid;
  grid: 100px 200px / 1fr 2fr;
}

The grid shorthand also resets the automatic grid properties to their initial values.

Property Comparison

Longhand Property grid grid-template
grid-template-rows Yes Yes
grid-template-columns Yes Yes
grid-template-areas Yes Yes
grid-auto-rows Yes No
grid-auto-columns Yes No
grid-auto-flow Yes No

When to Use grid-template

Use grid-template when defining explicit tracks without resetting existing automatic grid settings.

When to Use grid

Use grid when defining the complete grid configuration, including resetting or configuring automatic track behavior.

Important Consideration

The broader reset behavior of grid can affect an existing layout. Choose the shorthand according to which properties need to change.

Grid Item Placement

The grid property defines the container's grid structure. Individual items can then be positioned using grid placement properties.

Grid Container

.container {
  display: grid;
  grid: repeat(3, 100px) / repeat(3, 1fr);
}

Place an Item by Grid Lines

.featured-item {
  grid-column: 1 / 3;
  grid-row: 1 / 3;
}

The item spans two columns and two rows.

Use the grid-area Shorthand

.featured-item {
  grid-area: 1 / 1 / 3 / 3;
}

The four values represent row start, column start, row end, and column end.

Named Area Placement

.container {
  display: grid;
  grid:
    "sidebar main" 1fr
    / 200px 1fr;
}
.sidebar {
  grid-area: sidebar;
}
.main {
  grid-area: main;
}

Automatic Placement

Items without explicit positions participate in the grid's auto-placement algorithm.

Important Consideration

Grid item placement properties belong on the grid items, while the grid shorthand belongs on the grid container.

Responsive Grid Layout

Responsive grid layouts adjust their track structure to fit different viewport or container widths.

Flexible Columns

.container {
  display: grid;
  grid: auto / repeat(3, 1fr);
  gap: 16px;
}

Automatic Responsive Columns

.container {
  display: grid;
  grid: auto / repeat(auto-fit, minmax(min(100%, 200px), 1fr));
  gap: 16px;
}

The number of columns adjusts to the available width while keeping each track at a practical minimum size when space allows.

Media Query Layout

.container {
  display: grid;
  grid: auto / 1fr;
  gap: 16px;
}
@media (min-width: 768px) {
  .container {
    grid: auto / repeat(2, 1fr);
  }
}
@media (min-width: 1200px) {
  .container {
    grid: auto / repeat(4, 1fr);
  }
}

Responsive Sidebar Layout

.page-layout {
  display: grid;
  grid: auto / minmax(0, 1fr);
  gap: 20px;
}
@media (min-width: 900px) {
  .page-layout {
    grid: auto / 240px minmax(0, 1fr);
  }
}

Important Consideration

Responsive track sizing does not automatically make all content fit. Images, long text, and elements with fixed dimensions may require additional sizing rules.

Shorthand Reset Behavior

The grid shorthand resets all six of its longhand properties, including those not explicitly represented in the declaration.

Existing Automatic Row Size

.container {
  display: grid;
  grid-auto-rows: 150px;
  grid: auto / repeat(3, 1fr);
}

The later grid declaration resets grid-auto-rows to its initial value of auto.

Preserve Automatic Row Size

.container {
  display: grid;
  grid: auto / repeat(3, 1fr);
  grid-auto-rows: 150px;
}

Placing the longhand declaration after the shorthand restores the desired automatic row size.

Use a More Specific Longhand

.container {
  display: grid;
  grid-auto-rows: 150px;
  grid-template-columns: repeat(3, 1fr);
}

Changing only grid-template-columns leaves the existing grid-auto-rows value unchanged.

Reset Named Areas

.container {
  display: grid;
  grid-template-areas: "sidebar main";
  grid: auto / 200px 1fr;
}

The shorthand resets grid-template-areas to none, removing the earlier named area definitions.

Important Consideration

Review the order of shorthand and longhand declarations carefully. A later grid declaration can override earlier grid configuration even when its syntax does not explicitly mention those settings.

grid Example

This example uses the grid shorthand to create a three-column layout with two explicit rows and consistent spacing.

HTML Example

<div class="grid-demo">
  <div class="grid-item">Item 1</div>
  <div class="grid-item">Item 2</div>
  <div class="grid-item">Item 3</div>
  <div class="grid-item">Item 4</div>
  <div class="grid-item">Item 5</div>
  <div class="grid-item">Item 6</div>
</div>

CSS Example

.grid-demo {
  display: grid;
  grid: repeat(2, 80px) / repeat(3, minmax(0, 1fr));
  gap: 12px;
}
.grid-demo .grid-item {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  background-color: #2456a6;
  color: #ffffff;
  font-weight: bold;
  border-radius: 4px;
}
Browser Result
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6

The grid shorthand creates two rows, each 80 pixels tall, and three equally flexible columns. The gap property adds 12 pixels of spacing between the tracks.

The six grid items are placed automatically in source order, filling the first row before continuing to the second.

Common Mistakes

Most problems with the grid shorthand involve missing grid formatting, incorrect shorthand syntax, or unexpected resets.

Forgetting display: grid

.container {
  grid: auto / repeat(3, 1fr);
}

The declaration does not establish a grid container. Add display: grid or display: inline-grid.

Reversing Rows and Columns

.container {
  display: grid;
  grid: 100px 200px / 1fr 1fr;
}

Values before the slash define rows. Values after the slash define columns.

Expecting grid to Set Gaps

.container {
  display: grid;
  grid: auto / repeat(3, 1fr);
  gap: 20px;
}

Spacing must be configured separately using gap, row-gap, or column-gap.

Resetting Existing Grid Settings

A later grid shorthand declaration resets all six longhand properties, including automatic track sizes and named areas.

Confusing grid with grid-area

The grid property configures the container. The grid-area property positions an individual grid item.

Using auto-flow in the Wrong Position

.container {
  display: grid;
  grid: auto-flow 100px / repeat(3, 1fr);
}

For row-based automatic placement, auto-flow appears before the slash. For column-based automatic placement, it appears after the slash.

Expecting Every fr Track to Shrink Equally

Content-based minimum sizes can constrain flexible tracks. Use minmax(0, 1fr) when appropriate.

Creating Invalid Named Areas

Named grid areas must form rectangular regions, and all template rows must contain the same number of cells.

Ignoring Source Order

Visual placement does not change the document order used by keyboard navigation and many assistive technologies.

Assuming Grid Items Automatically Become Grid Containers

Nested elements do not inherit grid formatting from an outer grid. Apply display: grid to nested containers when another grid layout is needed.

Important Considerations

Keep the following points in mind when using the grid shorthand:

  • Purpose: Defines grid tracks, named areas, and automatic placement behavior.
  • Shorthand: Sets six grid-related longhand properties.
  • Grid formatting: Requires display: grid or display: inline-grid.
  • Inheritance: The property is not inherited.
  • Explicit rows: Defined through grid-template-rows.
  • Explicit columns: Defined through grid-template-columns.
  • Named areas: Defined through grid-template-areas.
  • Implicit rows: Sized using grid-auto-rows.
  • Implicit columns: Sized using grid-auto-columns.
  • Automatic placement: Controlled by grid-auto-flow.
  • Syntax: A slash separates row and column definitions in explicit grid syntax.
  • Flexible sizing: The fr unit distributes available space among flexible tracks.
  • Repeated tracks: Use repeat() to simplify repeated definitions.
  • Track limits: Use minmax() to establish minimum and maximum sizes.
  • Responsive layouts: Use flexible tracks, automatic repetition, and media queries.
  • Spacing: The gap property is separate from the grid shorthand.
  • Reset behavior: Shorthand declarations reset omitted longhand components.
  • Item placement: Use grid-area, grid-row, or grid-column on grid items.
  • Dense packing: May cause visual order to differ from source order.
  • Accessibility: Maintain a logical document order for reading and keyboard navigation.
  • Browser testing: Check track sizing, overflow, and responsive behavior across different viewport widths.

The grid shorthand provides a compact way to configure complete grid layouts. For smaller changes to an existing layout, individual grid longhand properties may be easier to maintain and less likely to reset unrelated settings.

Browser Support

The CSS grid property has Widely available support in modern browsers as part of CSS Grid Layout.

Modern versions of Chrome, Edge, Firefox, Safari, and other major browsers support the standardized grid shorthand and its core layout features.

Standard Grid Example

.container {
  display: grid;
  grid: auto / repeat(3, 1fr);
  gap: 16px;
}

Legacy Browser Considerations

Internet Explorer implemented an older, prefixed grid layout model that differs from modern CSS Grid. The standardized grid shorthand should not be expected to work in that legacy implementation.

Advanced Feature Support

Some advanced grid features have different compatibility histories. Check support separately when using newer features such as subgrid or masonry-style layout.

Browser Compatibility

For current compatibility details, visit Can I Use? and search for CSS Grid.

Specifications

The grid shorthand is defined by the CSS Grid Layout specification.

CSS Grid Layout Module Level 1: grid Shorthand

The specification defines explicit and implicit grid tracks, grid placement, track sizing, and the relationship between the shorthand and its longhand properties.

CSS Grid Layout Module Level 2

For additional examples and browser documentation, consult MDN Web Docs.

MDN Web Docs: grid