Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Place Items by Line Number

In this lesson, you placed two of the Coming Up cards on the events page’s grid by its line numbers, reading the numbers from DevTools’ grid overlay: the Spring Seed Swap card across the top row, and the Plant the Potatoes card two rows tall.

Key Points

  • Grid lines are the edges of the tracks, and the grid overlay numbers them: across the top for the column lines, down the side for the row lines. Three columns have four column lines, one at each edge and one at each gap.
  • grid-column places an item between two column lines: the number before the slash is the line it starts on, and the number after it is the line it ends on. grid-column: 1 / 3 runs from line 1 to line 3, so it covers two columns.
  • Watch out: the numbers count lines, not columns. 1 / 3 doesn’t mean columns one to three. To cover a three-column row, write 1 / 4.
  • Negative numbers count back from the end, and -1 is the last line of the tracks you defined: here the columns from grid-template-columns. grid-column: 1 / -1 covers the whole row, however many columns you define.
  • grid-row does the same with row lines. With one number, as in grid-row: 1, the item starts on that line and covers one track.
  • The browser places the cards in source order and never goes back to a cell it has passed. A card with only its column lines given is placed in that order too, so when it can’t fit in the next free cell, it drops to the next row and leaves that cell empty.
  • Once a card has both a row and a column, as with grid-column: 1 / -1 and grid-row: 1, the browser gives it that spot first, then places the rest in order.
  • Like the order property, line numbers move a card only on screen. Tab and screen readers still follow the HTML, so the Spring Seed Swap’s link is still reached third, after the first two work days. That’s date order, which still makes sense here, so the HTML keeps its order.
  • span counts tracks instead of naming a line. grid-row: span 2 makes an item two rows tall from wherever the browser places it, so the Plant the Potatoes card stays in date order.

Final Code

In events/upcoming.html, two cards get a second class. The Spring Seed Swap card gets featured:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your Coming Up cards sit in three equal columns, and the browser has put each card in the next free cell. Rosa wants the Spring Seed Swap, the season’s only Sunday event, to stand out as one wide card across the top of the grid, with the work days below it.

00:16 To get there, you tell the browser which grid lines the card runs between. With the grid overlay on, look at the numbers across the top. They count the column lines, 1 to 4: one at each edge, and one at each gap.

00:30 Down the side, the overlay numbers the row lines the same way.

00:35 First, give the card a class to select. In upcoming.html, add a second class, featured, to the Spring Seed Swap card. Then add a rule at the end of layout.css.

00:49 As a first try, give featured cards grid-column: 1 / 3, which looks like columns one to three. The number before the slash is the line the card starts on.

00:59 The number after it is the line the card ends on. Save. The card now runs from line 1 to line 3, so it covers two columns.

01:10 After the first two cards, the next free cell is in column 3, but your card starts at line 1, so it drops to the second row. The browser places the cards in order and never goes back to a cell it has passed, so that empty cell stays empty.

01:26 Watch out: these numbers count lines, not columns.

01:32 Three columns have four lines, so change the end to 4, and save. The swap card now covers the whole row.

01:40 Lines also count back from the end, with negative numbers. -1 is the last line of the columns you defined, here line 4. Change 4 to -1, and save. Nothing moves.

01:53 Now the card reaches the last column line, however many columns you define.

01:59 The swap card is still in the second row. Grid-row places it between row lines the same way. Add grid-row: 1 to the rule. With one number, the card starts on line 1 and covers one row, the first.

02:14 Save. The swap card rises to the first row, and Clear the Beds and Spread the Compost move down to the second. Now the card has both a row and a column, so the browser gives it that spot first.

02:26 Then it places the rest in order, and the empty cell is gone. Like the order property, line numbers move the card only on screen. Tab and screen readers still reach its link third, after the first two work days.

02:40 That’s date order, which still makes sense here, so keep the HTML as it is.

02:47 Rosa would like the family day, Plant the Potatoes, to stand out too: taller, but still in date order. Line numbers would pin it to one place. The keyword span counts tracks instead, from wherever the card lands.

03:01 In upcoming.html, add a second class, family-friendly, to the potatoes card. Then add a rule for it in layout.css, with grid-row: span 2, and save.

03:14 The card stays where the browser put it, in the third column, and now covers two rows, from row line 2 to row line 4. Bean Poles and Tomatoes fill the two cells beside it, and Weed and Mulch starts a fourth row.

03:30 So grid-column and grid-row place an item by the lines it starts and ends on, read from the overlay’s numbers. -1 is the last line of the tracks you defined.

03:41 And span counts tracks, not lines. Next, in Name Grid Areas, you’ll give the parts of a grid names, so you can place items without counting lines at all.

Become a Member to join the conversation.