CSS Grid: Span items on multiple columns and/or rows

Every cell item has the option to occupy more than just one box in the row, and expand horizontally or vertically to get more space, while respecting the grid proportions set in the container.

Those are the properties we’ll use for that:

  • grid-column-start
  • grid-column-end
  • grid-row-start
  • grid-row-end

Example:

.container {
  display: grid;
  grid-template-columns: 200px 200px 200px 200px;
  grid-template-rows: 300px 300px;
}
.item1 {
  grid-column-start: 2;
  grid-column-end: 4;
}
.item6 {
  grid-column-start: 3;
  grid-column-end: 5;
}

Editing the dimensions of some cells

The numbers correspond to the vertical line that separates each column, starting from 1:

The numbers correspond to the vertical line that separates each column

The same principle applies to grid-row-start and grid-row-end, except this time instead of taking more columns, a cell takes more rows.

Shorthands: grid-column, grid-row and grid-area

Those properties have a shorthand syntax provided by:

  • grid-column
  • grid-row

The usage is simple, here’s how to replicate the above layout:

.container {
  display: grid;
  grid-template-columns: 200px 200px 200px 200px;
  grid-template-rows: 300px 300px;
}
.item1 {
  grid-column: 2 / 4;
}
.item6 {
  grid-column: 3 / 5;
}

The grid-area property can be used as a shorthand for the grid-column and grid-row shorthands, when you need to apply both to a single element. Instead of having:

.item1 {
  grid-row: 1 / 4;
  grid-column: 3 / 5;
}

You can use

.item1 {
  grid-area: 1 / 3 / 4 / 5;
}

(grid-row-start / grid-column-start / grid-row-end / grid-column-end)

Using span

Another approach is to set the starting column/row, and set how many it should occupy using span:

.container {
  display: grid;
  grid-template-columns: 200px 200px 200px 200px;
  grid-template-rows: 300px 300px;
}
.item1 {
  grid-column: 2 / span 2;
}
.item6 {
  grid-column: 3 / span 2;
}

span works also with the non-shorthand syntax:

.item1 {
  grid-column-start: 2;
  grid-column-end: span 2;
}

and you can also use on the start property. In this case, the end position will be used as a reference, and span will count “back”:

.item1 {
  grid-column-start: span 2;
  grid-column-end: 3;
}

Lessons in this unit:

0: Introduction
1: grid-template-columns and grid-template-rows
2: Setting columns and rows dimensions
3: Gap between the cells
4: ▶︎ Span items on multiple columns and/or rows
5: Minimum row width
6: Layout using grid-template-areas
7: DEMO Example: simple grid layout
8: DEMO Example: grid layout with header, sidebar, content and footer
Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. Launching May 21, 2024. Join the waiting list!