Spanning Several Cells

The Maine Coon is one of the largest cat breeds, so let's give it a bigger card too: two columns wide and two rows tall.

An item can take up more than one cell. With the grid-column property, we say which columns it takes up, and with grid-row, which rows. The easiest way is the word span and a number:

.large {
	grid-column: span 2;
}

The item starts where it would be anyway, but it's two columns wide. The items after it move on to make room. If the item doesn't fit at the end of a row, it moves to the next one.

We can also say exactly where an item starts and where it ends, with the numbers of the grid lines. The lines are numbered from 1: a grid with three columns has four vertical lines, from the left edge to the right edge (see the picture). The value is then two numbers separated by a slash: the line where the item starts and the line where it ends.

A grid container with three columns and two rows of grid items. The vertical grid lines are numbered from 1 to 4 and the horizontal ones from 1 to 3.
.large {
	grid-column: 1 / 3;
}

This item lies between lines 1 and 3, so it takes up the first and the second column. grid-column: 1 / -1 stretches an item across the whole width of the grid: negative numbers count the lines from the end, so -1 is the last line.

The grid-row property works the same way, just for rows.

Instructions

In index.html, add the class large to the Maine Coon card, so that the value of its class attribute is card large.

In style.css, create a rule for .large with the declaration grid-column: span 2. The card is now two columns wide, and the Ragdoll card has moved to a new row.

In the rule for .large, add the declaration grid-row: span 2. The card is now two rows tall, and the Ragdoll card has moved into the free cell next to it.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

4/7