skip to content

What does the CSS grid shorthand property set, and which longhands does it reset that you cannot even specify in it?

level: middleimportance: nice to knowfreq 24%

answer

  1. it is a shorthand for six properties, not three
  2. shorthands reset what they do not mention
  3. the implicit half is the part that gets wiped
  4. auto-flow keyword marks the growing axis
  5. grid-template is the safer narrower one

basics

~20 s

The grid shorthand sets grid-template-rows, grid-template-columns and grid-template-areas plus grid-auto-rows, grid-auto-columns and grid-auto-flow. Any of those six it does not mention are reset to their initial values, including the three implicit-grid longhands the template form cannot express.

solid answer

~40 s

`grid` is the shorthand for six longhands: the three explicit ones (`grid-template-rows`, `grid-template-columns`, `grid-template-areas`) and the three implicit ones (`grid-auto-rows`, `grid-auto-columns`, `grid-auto-flow`). Like any shorthand, it resets every longhand in its set, so writing `grid: 100px 100px / 200px 200px` also wipes `grid-auto-flow` back to `row` and both auto-track properties back to `auto` — even though that form of the syntax cannot set them. That is the trap: a `grid-auto-rows: 120px` rule earlier in the block is silently undone. The shorthand does have an implicit form — `grid: auto-flow 120px / 200px 200px` sets `grid-auto-flow: row` with `grid-auto-rows: 120px` — but it is either/or, so you cannot define a full template and the auto-track sizing in one declaration. For that reason most codebases prefer `grid-template` or the longhands.

code

css · 5 lines
css
.board {
  display: grid;
  grid: 80px 80px / auto-flow 280px;
  gap: 12px;
}

go deeper

for a junior

Know that grid is a shorthand covering both the template properties and the auto-* ones, and that grid-template is the narrower version limited to the explicit tracks.

for a middle

Explain that a shorthand resets every longhand in its set, so the template form of grid silently returns grid-auto-flow, grid-auto-rows and grid-auto-columns to their initial values.

for a senior

Recognise this as a real regression source when declarations are refactored, and be able to state the house rule you would set for shorthand use in a shared stylesheet.

for a principal

Weigh terseness against reviewability: a shorthand whose main effect is on longhands it never names is exactly the kind of implicit behaviour a large codebase should lint or forbid outright.

## What is in the set `grid` is a shorthand covering six properties: - explicit: `grid-template-rows`, `grid-template-columns`, `grid-template-areas` - implicit: `grid-auto-rows`, `grid-auto-columns`, `grid-auto-flow` CSS shorthands always reset every longhand they cover, whether or not the particular value you wrote mentions it. That rule is what makes `grid` sharper-edged than most shorthands, because half its set is unreachable from the syntax form people actually use. ## The explicit form ```css .layout { display: grid; grid: 80px 1fr 60px / 200px 1fr; /* rows / columns */ } ``` This is the `grid-template` syntax embedded in `grid`: rows before the slash, columns after, and the `grid-template-areas` string form is accepted here too. What it silently also does is set `grid-auto-flow: row`, `grid-auto-rows: auto` and `grid-auto-columns: auto`. ```css .layout { grid-auto-rows: 120px; /* wiped out by the next line */ grid: 80px 1fr / 200px 1fr; } ``` The cascade order inside the block is the whole story: the shorthand comes later, so it wins, and the implicit rows go back to `auto`. If a grid mysteriously reverts to content-sized implicit rows after someone "tidied up" the declarations into a shorthand, this is the cause. ## The implicit form The shorthand can also express the auto-track side, using the `auto-flow` keyword to mark which axis is the flowing one: ```css .rows { grid: auto-flow 120px / 200px 200px; } /* grid-auto-flow: row; grid-auto-rows: 120px; grid-template-columns: 200px 200px */ .cols { grid: 80px 80px / auto-flow 280px; } /* grid-template-rows: 80px 80px; grid-auto-flow: column; grid-auto-columns: 280px */ ``` Read the position of `auto-flow`: on the row side it means rows are the generated axis, on the column side it means columns are. The value after it is the auto track size, and `dense` may accompany the keyword. Crucially this is an either/or grammar. The side carrying `auto-flow` is the implicit axis, so that side cannot also be a template. You cannot say "three explicit rows, and any further rows are 120px" in one `grid` declaration — that genuinely needs `grid-template-rows` and `grid-auto-rows` written separately. ## grid-template versus grid `grid-template` is the narrower shorthand: the same rows/columns/areas syntax, but it covers only the three explicit longhands and therefore only resets those. If your intent is "define the explicit structure and leave the implicit settings alone", `grid-template` says exactly that and `grid` does not. ```css .safe { grid-auto-rows: 120px; grid-template: 80px 1fr / 200px 1fr; /* grid-auto-rows survives */ } ``` ## What is not in the set `gap` (and `row-gap` / `column-gap`) are not part of `grid`, so the shorthand neither sets nor resets them. Neither are the alignment properties. Older material sometimes claims `grid` resets gaps; it does not. ## The practical guidance Use `grid-template` or plain longhands for everyday layout code and reserve `grid` for the compact `auto-flow` form when it genuinely reads better. The reasoning is the same one that applies to every resetting shorthand: a declaration whose main effect is on properties it does not name is hard to review, and here the properties it silently resets are precisely the ones that control the implicit grid — the part of the layout that only misbehaves once real data arrives.

  • How does grid-template differ from grid?
    `grid-template` covers only `grid-template-rows`, `grid-template-columns` and `grid-template-areas`, so it resets just those three and leaves `grid-auto-rows`, `grid-auto-columns` and `grid-auto-flow` untouched. `grid` covers all six. When you only mean to declare the explicit structure, `grid-template` states that intent and avoids the silent reset.
  • Can one grid declaration define explicit rows and also size the implicit ones?
    No. The side of the slash carrying `auto-flow` is the implicit axis, so it cannot also carry a template list. Declaring three fixed rows and a size for any further rows requires `grid-template-rows` plus `grid-auto-rows` as separate declarations.

saying these in an interview costs you the question

  • Thinks grid is just an alias for grid-template
  • Believes the shorthand resets gap as well
  • Assumes unspecified longhands keep their previous values
  • Thinks auto-flow in the shorthand only sets the flow direction
  • Expects one grid declaration to set both a template and auto sizes on the same axis

context