Understanding grid negative values

The grid-column and grid-row shorthand properties count grid lines.

You wrote:

For single span columns I can use grid-column: 1/1

This doesn’t make any sense. It resolves to:

grid-column-start: 1
grid-column-end: 1

So you’re defining a column that goes from the starting line to the starting line.

A column has a starting line and an ending line. So to span the first column you use:

grid-column: 1 / 2

The only reason your code works as you expect is because the Grid error handling algorithm removes the end line when the start and end lines are equal.

So 1 / 1 resolves to 1 / auto, where auto represents a default span of 1.


You wrote:

But if I want to define the last column or row, I have to write grid-column: -2/-1

Yes, that’s one way to do it. (You can also use positive integers, since you know the number of columns.)


You wrote:

Why is the syntax not the same as with 1/1 for the first column/row? Or am I making a mistake somewhere?

1 / 1

As mentioned above, 1/1 was invalid. It is fixed by the Grid system, resolving to 1 / auto. As a result, you span the first column.

-1 / -1

This combination of values is also invalid. It means span from the last line of the grid to the last line of the grid. Grid error handling changes the end value to -1 / auto. This takes you out of the explicit grid (because an implicit column is created) and negative values no longer apply. The negative values end where the implicit grid begins (demo).

-2 / -1

Correct syntax. So it works. Span one column starting from the penultimate line of the grid.

Leave a Comment