CSS Grid Span Keyword: Column and Row Placement
In CSS Grid layouts, the span keyword allows you to
specify how many tracks—columns or rows—a grid item should expand
across, rather than manually calculating absolute start and end grid
lines. This article provides a concise guide to how the
span keyword works with grid-column and
grid-row, demonstrates its syntax, and explains how it
simplifies dynamic responsive designs.
What the span Keyword
Does
By default, CSS Grid items occupy a single cell bounded by grid
lines. Placing items using line numbers often requires specifying both a
starting line and an ending line (for example,
grid-column: 1 / 3;). The span keyword changes
this paradigm by introducing relative sizing. When you use
span, you tell the browser: "Make this item \(N\) tracks wide or tall from its starting
point."
Using span with
grid-column
The span keyword is most commonly used within the
grid-column shorthand property
(grid-column-start / grid-column-end).
Relative Span from Auto-Placement:
.item { grid-column: span 2; }The item will place itself according to the grid auto-placement algorithm and stretch across two columns.
Starting Line with a Span:
.item { grid-column: 2 / span 3; }The item explicitly starts at vertical grid line 2 and spans across 3 columns, automatically ending at grid line 5.
Span Ending at a Specific Line:
.item { grid-column: span 2 / 5; }The item ends at grid line 5 and spans backward across 2 columns, starting at line 3.
Using span with
grid-row
The span keyword behaves identically on the vertical
axis when applied to grid-row:
.card-featured {
grid-row: 1 / span 2;
}In this case, the element begins at horizontal grid line 1 and spans downward across two rows, terminating at grid line 3.
Spanning to Named Lines
Beyond spanning a specific integer count of tracks, span
can also target named grid lines. When combined with named lines, the
syntax looks like:
.item {
grid-column: span main-end;
}This instructs the item to expand from its placement position until
it reaches the next line labeled main-end.
Why Use the span
Keyword?
- Maintainability: If your grid layout changes (such
as adding or removing columns), items configured with
spanadapt automatically without needing updated end-line calculations. - Component Reusability: Items styled with
grid-column: span 2can be moved anywhere inside the grid without breaking explicit line-based alignments. - Readability: Expressing intent as "span 3 columns"
is often clearer and faster to interpret than calculating line
coordinates like
4 / 7.