CSS Grid Layout — “Grid” or “CSS Grid” for short — is a two-dimensional layout system. Earlier approaches to page layout (tables, floats, positioning, inline-block) were workarounds that left out important capabilities, vertical centering among them. Flexbox is a strong layout tool as well, but its one-directional flow serves different use cases; the two modules work together. Grid is the first CSS module designed specifically to address the layout problems developers had been hacking around.

This guide presents Grid concepts as they exist in the latest specification, so the out-of-date Internet Explorer syntax and other historical workarounds are not covered.

Terminology

The Grid specification defines a small set of terms. They are conceptually similar and easy to mix up, so memorizing the definitions first pays off.

Grid container

The element carrying display: grid. It is the direct parent of all grid items.

<div class="container">
  <div class="item item-1"> </div>
  <div class="item item-2"> </div>
  <div class="item item-3"> </div>
</div>

Grid item

The children of the grid container — and only the direct children. In the markup above, the item elements qualify; sub-item does not.

<div class="container">
  <div class="item"> </div>
  <div class="item">
    <p class="sub-item"> </p>
  </div>
  <div class="item"> </div>
</div>

Grid line

The dividing lines that form the grid's structure. Vertical lines are column grid lines, horizontal lines are row grid lines, and each runs alongside a row or column. In the example below, the yellow line is a column grid line.

Grid track

The space between two adjacent grid lines — effectively a column or a row. The example below shows the track between the second and third row grid lines.

Grid area

The total space enclosed by four grid lines. An area can span any number of grid cells; the one shown below lies between row grid lines 1 and 3 and column grid lines 1 and 3.

Grid cell

The space between two adjacent row lines and two adjacent column lines — a single unit of the grid. The example below sits between row grid lines 1 and 2 and column grid lines 2 and 3.

Container Properties: Establishing the Grid

Declaring display: grid turns an element into a block-level grid container and gives its contents a new grid formatting context; inline-grid does the same at the inline level. Subgrid support, which lets grid parameters pass down through nested elements, was moved out of this level and lives in CSS Grid Specification Level 2.

Defining Tracks

The grid-template-columns and grid-template-rows properties take a space-separated list of track sizes — a length, a percentage, or a fraction of the grid's free space expressed with the fr unit. The spaces between those values represent grid lines, which are numbered positively from 1 automatically (with -1 as an alternate name for the final row line). Bracket syntax lets you name lines explicitly, and a single line may carry more than one name, so one line can be both row1-end and row2-start. Repetitive definitions can be collapsed with repeat(), and when several lines share a name you reference them by name plus a count.

.container {
  grid-template-columns: ...  ...;
  /* e.g. 
      1fr 1fr
      minmax(10px, 1fr) 3fr
      repeat(5, 1fr)
      50px auto 100px 1fr
  */
  grid-template-rows: ... ...;
  /* e.g. 
      min-content 1fr min-content
      100px 1fr max-content
  */
}
.container {
  grid-template-rows: [row1-start] 25% [row1-end row2-start] 25% [row2-end];
}
.container {
  grid-template-columns: repeat(3, 20px [col-start]);
}
.item {
  grid-column-start: col-start 2;
}

Because fr sizes are resolved against leftover space, free space is calculated only after any non-flexible tracks are accounted for — a total that doesn't include, for instance, a fixed 50px column.

.container {
  grid-template-columns: 1fr 1fr 1fr;
}
.container {
  grid-template-columns: 1fr 50px 1fr 1fr;
}

Named Areas and the Template Shorthands

grid-template-areas describes the grid through the area names you assign with grid-area. Repeating a name makes content span those cells, a period marks an empty cell, and none defines no areas at all. The declaration is effectively a drawing of the layout, subject to two rules: every row must contain the same number of cells, and any number of adjacent periods without spaces counts as a single empty cell. Area names are not line names, but lines at the edges of an area are named for you — an area called foo produces foo-start and foo-end lines, so one line can accumulate several names (such as header-start, main-start, and footer-start).

.item-a {
  grid-area: header;
}
.item-b {
  grid-area: main;
}
.item-c {
  grid-area: sidebar;
}
.item-d {
  grid-area: footer;
}

.container {
  display: grid;
  grid-template-columns: 50px 50px 50px 50px;
  grid-template-rows: auto;
  grid-template-areas: 
    "header header header header"
    "main main . sidebar"
    "footer footer footer footer";
}
Example of grid-template-areas

grid-template is the shorthand for rows, columns, and areas. It accepts none, a rows/columns pair (which sets areas to none), or the fuller syntax covering all three at once. Notably, it leaves the implicit-grid properties — grid-auto-columns, grid-auto-rows, and grid-auto-flow — untouched, which is usually the desired outcome but is a reason to prefer the grid shorthand instead.

.container {
  grid-template:
    [row1-start] "header header header" 25px [row1-end]
    [row2-start] "footer footer footer" 25px [row2-end]
    / auto 50px auto;
}
.container {
  grid-template-rows: [row1-start] 25px [row1-end row2-start] 25px [row2-end];
  grid-template-columns: auto 50px auto;
  grid-template-areas: 
    "header header header" 
    "footer footer footer";
}

The grid shorthand covers all six properties: the three template properties plus the three auto properties, though a single declaration can set either the explicit or the implicit family, not both. Beyond none and a template passed straight through, it supports auto-flow forms: with auto-flow right of the slash, grid-template-rows is set and grid-auto-flow becomes column; with auto-flow left of the slash, grid-template-columns is set and flow becomes row. Adding dense switches the placement algorithm to dense packing, and an omitted auto-track size defaults to auto.

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

.container {
  grid-template-rows: 100px 300px;
  grid-template-columns: 3fr 1fr;
}
.container {
  grid: auto-flow / 200px 1fr;
}

.container {
  grid-auto-flow: row;
  grid-template-columns: 200px 1fr;
}
.container {
  grid: auto-flow dense 100px / 1fr 2fr;
}

.container {
  grid-auto-flow: row dense;
  grid-auto-rows: 100px;
  grid-template-columns: 1fr 2fr;
}
.container {
  grid: 100px 300px / auto-flow 200px;
}

.container {
  grid-template-rows: 100px 300px;
  grid-auto-flow: column;
  grid-auto-columns: 200px;
}
.container {
  grid: [row1-start] "header header header" 1fr [row1-end]
        [row2-start] "footer footer footer" 25px [row2-end]
        / auto 50px auto;
}
.container {
  grid-template-areas: 
    "header header header"
    "footer footer footer";
  grid-template-rows: [row1-start] 1fr [row1-end row2-start] 25px [row2-end];
  grid-template-columns: auto 50px auto;    
}

Aligning Items and the Grid Itself

Two axes govern alignment. The inline (row) axis is handled by justify-* properties, the block (column) axis by align-* properties.

  • justify-items positions every item within its cell along the inline axis, with stretch as the default; other values are start, end, and center. The same effect per item is available through justify-self.
  • align-items does the equivalent on the block axis. Its values are stretch (default), start, end, center, and baseline; the baseline value has first baseline and last baseline modifiers for choosing the first or last line of multi-line text. Per-item control comes from align-self.
  • place-items combines both container properties in one declaration; give one value and it applies to both axes, give two and the first sets align-items, the second justify-items. A single place-items: center is a compact way to center in both directions.
  • place-self is the per-item equivalent for align-self and justify-self, and also accepts auto for the layout mode's default alignment.

align-items additionally understands the safe and unsafe modifiers, written as in align-items: safe end. safe means attempt the alignment unless doing so would push an item into an unreachable overflow region; unsafe permits content to move into such areas ("data loss"). place-self is supported in all major browsers except Edge.

.center {
  display: grid;
  place-items: center;
}
.item {
  justify-self: start | end | center | stretch;
}
.item {
  align-self: start | end | center | stretch;
}

When the grid's total size is smaller than its container — common when tracks use non-flexible units like px — the grid itself can be positioned. justify-content aligns it on the inline axis, align-content on the block axis, and both accept start, end, center, stretch, space-around, space-between, and space-evenly. The difference between the stretch values is directional: justify-content: stretch grows items to fill the container's width, while align-content: stretch grows them to fill its height. place-content sets both at once, the first value for align-content and the second for justify-content, with a single value applying to both.

.container {
  justify-content: start | end | center | stretch | space-around | space-between | space-evenly;    
}
.container {
  align-content: start | end | center | stretch | space-around | space-between | space-evenly;    
}

Implicit Tracks and Automatic Placement

Cells beyond the explicit grid produce implicit tracks — this happens when there are more items than cells, or when an item is placed outside the defined grid. grid-auto-columns and grid-auto-rows size those tracks, using the same length, percentage, or fr values as the template properties. Consider a 2×2 grid where an item is told to run from column line 5 to line 6: those lines were never defined, so zero-width implicit tracks fill the gap unless you set a size for them.

.container {
  grid-template-columns: 60px 60px;
  grid-template-rows: 90px 90px;
}
.item-a {
  grid-column: 1 / 2;
  grid-row: 2 / 3;
}
.item-b {
  grid-column: 5 / 6;
  grid-row: 2 / 3;
}
.container {
  grid-auto-columns: 60px;
}
grid-auto-columns-rows

Items without an explicit position are handled by the auto-placement algorithm, which grid-auto-flow controls. The default, row, fills each row in turn and adds new rows as needed; column fills columns and adds new columns; dense instructs the algorithm to backfill holes earlier in the grid when smaller items arrive later. Be careful with dense: it changes only the visual order and can leave items displayed out of sequence, which harms accessibility. In a five-column, two-row grid with only two items explicitly positioned, the remaining three flow across the rows under row, or down the columns if you switch to column.

<section class="container">
  <div class="item-a">item-a</div>
  <div class="item-b">item-b</div>
  <div class="item-c">item-c</div>
  <div class="item-d">item-d</div>
  <div class="item-e">item-e</div>
</section>
.container {
  display: grid;
  grid-template-columns: 60px 60px 60px 60px 60px;
  grid-template-rows: 30px 30px;
  grid-auto-flow: row;
}
.item-a {
  grid-column: 1;
  grid-row: 1 / 3;
}

.item-e {
  grid-column: 5;
  grid-row: 1 / 3;
}
Example of grid-auto-flow set to row
.container {
  display: grid;
  grid-template-columns: 60px 60px 60px 60px 60px;
  grid-template-rows: 30px 30px;
  grid-auto-flow: column;
}

gap sets the spacing between tracks and acts only between items, never at the outer edges. It is a shorthand for row-gap and column-gap, behaves as a minimum gutter — larger space from something like justify-content: space-between takes precedence — and is not specific to grid, since it also works in Flexbox and multi-column layout.

.container {
  display: flex;
  /* ... */
  gap: 10px;
  gap: 10px 20px; /* row-gap column gap */
  row-gap: 10px;
  column-gap: 20px;
}

Placing Individual Items

An item's position is set by its start and end lines: grid-column-start/grid-row-start and grid-column-end/grid-row-end. Each accepts a line, given as a number or a name; a span of a number of tracks or up to the next named line (span <number>, span <name>); or auto for automatic placement or a default span. Omit the end line and the item spans one track. Items may overlap, and z-index decides which sits on top.

.item {
  grid-column-start: <number> | <name> | span <number> | span <name> | auto;
  grid-column-end: <number> | <name> | span <number> | span <name> | auto;
  grid-row-start: <number> | <name> | span <number> | span <name> | auto;
  grid-row-end: <number> | <name> | span <number> | span <name> | auto;
}
.item-a {
  grid-column-start: 2;
  grid-column-end: five;
  grid-row-start: row1-start;
  grid-row-end: 3;
}

grid-column and grid-row are shorthands for the start/end pairs on each axis, with no end value meaning a one-track span. grid-area serves double duty: it names an item for reference by grid-template-areas, or acts as the shortest shorthand of all, setting row-start, column-start, row-end, and column-end in that order.

.item {
  grid-column: <start-line> / <end-line> | <start-line> / span <value>;
  grid-row: <start-line> / <end-line> | <start-line> / span <value>;
}
.item-c {
  grid-column: 3 / span 2;
  grid-row: third-line / 4;
}
.item-d {
  grid-area: header;
}
.item-d {
  grid-area: 1 / col4-start / last-line / 6;
}
Example of grid-area

Finally, justify-self and align-self align a single item within its own cell — inline axis and block axis respectively — with stretch as the default. Their container-wide counterparts are justify-items and align-items.

.item-a {
  justify-self: start;
}
.item-a {
  align-self: start;
}

Sizing Values in Grid Tracks

The fr unit is the value you'll reach for most often when defining grid tracks. It stands for a fraction of the space that remains after fixed sizes are accounted for, so grid-template-columns: 1fr 3fr behaves loosely like 25% 75%. The similarity ends there: percentages are rigid, and padding added to percentage-based columns under a content-box model pushes past the full width. Fractional units, by contrast, combine cleanly with other units.

Content-Aware Keywords

Rows and columns accept every familiar length — px, rem, % and the rest — alongside three keywords:

  • min-content — the smallest size the content allows. For a line of text such as “E pluribus unum”, that is likely the width of “pluribus”.
  • max-content — the largest size the content allows, i.e. the full length of that sentence.
  • auto — close to fr in behavior, except that it loses to fr units when the remaining space is handed out.

Functions That Shape Tracks

  • fit-content() — takes the available space, but is clamped between min-content and max-content.
  • max() — evaluates two or more comma-separated spacing values and returns the largest.
  • min() — takes the same arguments and returns the smallest.
  • minmax() — exclusive to CSS Grid; defines a size range for a track by taking a minimum and a maximum, so the track can grow or shrink within those bounds depending on available space.

Generating Tracks with repeat()

repeat() exists to cut down on repetition in track definitions, and it becomes more powerful when paired with the auto-fill and auto-fit keywords. auto-fill packs as many columns onto a row as will fit, leaving empty ones if needed. auto-fit fits the columns that actually exist into the space, preferring to expand them rather than keep empty tracks. This combination produces the best-known snippet in all of CSS Grid:

grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));

Subgrid

Subgrid lets a grid item carry a grid of its own that inherits the grid lines of its parent — a feature that is extremely useful when inner content should align with the outer layout.

.parent-grid {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
}
.grid-item {
  grid-column: 2 / 7;

  display: grid;
  grid-template-columns: subgrid;
}
.child-of-grid-item {
  /* gets to participate on parent grid! */
  grid-column: 3 / 6;
}

Related but distinct is display: contents;, which is not subgrid, though it can sometimes serve a similar purpose.

<div class="grid-parent">

  <div class="grid-item"></div>
  <div class="grid-item"></div>

  <ul style="display: contents;">
    <!-- These grid-items get to participate on 
         the same grid!-->
    <li class="grid-item"></li>
    <li class="grid-item"></li>
  </ul>

</div>

Animating CSS Grid

The Grid Layout Module Level 1 specification identifies exactly five animatable grid properties. Three of them are gap properties: gap, row-gap and column-gap, each animatable as a length, percentage, or calculation. The other two are grid-template-columns and grid-template-rows, which can animate as a simple list of lengths, percentages, or calculations — but only when the sole differences between the two states are the values of the length, percentage, or calculated components within that list.

In practice, the gap properties are the only ones that currently animate in the tested browsers.

Browsergap, row-gap, column-gapgrid-template-columnsgrid-template-rows
Firefoxsupported ✅ 53+supported ✅ 66+supported ✅ 66+
Safari 12.0not supported ❌not supported ❌not supported ❌
Chromesupported ✅ 66+not supported ❌not supported ❌
Chrome for Android 66+, Opera Mini 33+supported ✅not supported ❌not supported ❌
Edgesupported ✅ 16+not supported ❌not supported ❌

Specifications

Grid is defined across three levels:

  • CSS Grid Layout Module Level 1 (Candidate Recommendation): the initial definition of CSS Grid and its properties.
  • CSS Grid Layout Module Level 2 (Candidate Recommendation Draft): adds the initial definition for the Subgrid feature.
  • CSS Grid Layout Module Level 3 (Editor's Draft): contains the first working definition for Masonry layouts.

Learning Resources

Articles

  • Getting Started with CSS Grid — Robin Rendle, Mar 13, 2017
  • Does CSS Grid Replace Flexbox? — Robin Rendle, Mar 31, 2017
  • A Collection of Interesting Facts about CSS Grid Layout — Manuel Matuzovic, Jul 21, 2017
  • Things I’ve Learned About CSS Grid Layout — Ollie Williams, Nov 2, 2016
  • An Introduction to the fr CSS unit — Robin Rendle, Jun 12, 2017
  • Auto-Sizing Columns in CSS Grid: auto-fill vs auto-fit — Sara Soueidan, Dec 29, 2017
  • Exploring CSS Grid’s Implicit Grid and Auto-Placement Powers — Temani Afif, Aug 1, 2022
  • The Auto-Flowing Powers of Grid’s Dense Keyword — Geoff Graham, Jan 8, 2020
  • The Difference Between Explicit and Implicit Grids — Manuel Matuzovic, Aug 10, 2017
  • Using Grid Named Areas to Visualize (and Reference) Your Layout — Preethi Selvam, Aug 26, 2022
  • Making Zigzag CSS Layouts With a Grid + Transform Trick — Durgesh Rajubhai Pawar, May 6, 2026

Video Screencasts

  • #115: Don’t Overthink It Grids — Chris Coyier, Oct 24, 2012 (17:19)
  • #019: Building a Simple Grid — Chris Coyier, Sep 10, 2012
  • #032: Making The Grid Responsive — Chris Coyier, Sep 12, 2012
  • #153: Getting Started with CSS Grid — Geoff Graham, Feb 20, 2017 (47:26)
  • #208: A CSS Grid Layout with Pictures Down One Side Matched Up with Paragraphs on the Other — Chris Coyier, Jul 30, 2021 (21:17)
  • #179: A Grid of Squares — Chris Coyier, Apr 6, 2020 (16:34)

Browser Support

This browser support data comes from Caniuse, which has more detail. A number indicates that the browser supports the feature at that version and up.

Desktop

ChromeFirefoxIEEdgeSafari
575211*1610.1

Mobile / Tablet

Android ChromeAndroid FirefoxAndroidiOS Safari
15716015710.3