In CSS Lesson 9, the items filled the grid one cell at a time. Now let's choose exactly where each item goes and how much room it takes.
flag
What you'll learn
How grid lines are numbered, and how to make an item cover several columns or rows.
How to use span and the grid-area shorthand.
How to line up one item inside its cell with justify-self and align-self.
waving_handLet's place items
Every grid has invisible grid lines between its columns and rows. You place an item by saying which line it starts on and which line it ends on. An item can cover one cell, or several cells in a row or a column.
newspaper
Think of a newspaper's front page. The editor lays it out on a grid of columns. Small stories take one box each, but the top story gets a bigger space: two columns wide and two rows tall.
An ad at the bottom runs across all the columns.
Front Page Bytes
Top storygrid-column: 1 / 3; grid-row: 2 / 4;
Weather
Games
Deals
Apps
Sports
Ad: Big sale this weekendgrid-column: 1 / 4;
Now let's see the CSS ↓
Front Page Bytes
Top storygrid-column: 1 / 3; grid-row: 2 / 4;
Weather
Games
Deals
Apps
Sports
Ad: Big sale this weekendgrid-column: 1 / 4;
The page name takes row 1, so the top story covers rows 2 and 3. The other stories fill the free cells in order.
searchA closer look
Counting grid lines
A grid with three columns has four column lines, numbered 1 to 4 from left to right. grid-column: 1 / 3; means "start at line 1, end at line 3", so the item covers the first two columns.
1234
grid-column: 1 / 3;
Rows and the span keyword
Rows work the same way with grid-row, counting lines from the top. Instead of an end line, you can say how many cells to cover: grid-column: span 2; means "cover two columns, starting wherever you land".
-1 is the last line, so grid-column: 1 / -1; covers the full width, however many columns there are.
The grid-area shorthand
grid-area sets all four lines at once, in this order: row start / column start / row end / column end. grid-area: 1 / 1 / 3 / 3; is the same as grid-row: 1 / 3; plus grid-column: 1 / 3;.
It also takes a name from grid-template-areas, as you saw in CSS Lesson 9.
Lining up one item
justify-self and align-self line up a single item inside its own cell, left to right and top to bottom. They are the one-item versions of justify-items and align-items.
constructionLet's build it
Let's lay out a Front Page Bytes front page on a three-column grid from CSS Lesson 9. Each code box opens on the CSS tab with the new lines highlighted. Click HTML to see the page being styled.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <div class="page"> <div class="top">Top story: New phones arrive</div> <div class="weather">Weather</div> <div class="games">Games</div> <div class="deals">Deals</div> <div class="apps">Apps</div> <div class="ad">Ad: Big sale this weekend</div> </div> </body></html>
What you see
description(no title)
grid-column: 1 / 3; stretches the top story from column line 1 to line 3: two columns. Weather moves to the third column, and the other items fill the next rows.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <div class="page"> <div class="top">Top story: New phones arrive</div> <div class="weather">Weather</div> <div class="games">Games</div> <div class="deals">Deals</div> <div class="apps">Apps</div> <div class="ad">Ad: Big sale this weekend</div> </div> </body></html>
What you see
description(no title)
The top story is tall, so the rows beside it are tall too, and Weather stretched to fill its cell. align-self: start; lets only Weather shrink to its text, at the top of its cell.
sticky_note_2Quick notes
grid-columnWhich columns it covers
.top { grid-column: 1 / 3; }
How to use
1 / 3Starts at column line 1 and ends at line 3, so it covers two columns.
span 2Covers two columns, starting wherever the item lands.
1 / -1From the first line to the last line: the full width.
Examples
A main story that covers two columns.
.top { grid-column: 1 / 3;}
A banner across the full width.
.banner { grid-column: 1 / -1;}
An ad that covers three columns.
.ad { grid-column: span 3;}
grid-rowWhich rows it covers
.top { grid-row: 1 / 3; }
How to use
1 / 3Starts at row line 1 and ends at line 3, so it covers two rows.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <div class="page"> <div class="top">Top story: New phones arrive</div> <div class="weather">Weather</div> <div class="games">Games</div> <div class="deals">Deals</div> <div class="apps">Apps</div> <div class="ad">Ad: Big sale this weekend</div> </div> </body></html>
What you see
description(no title)
The grid has three columns, but the ad asks for four. The browser adds a fourth column to make room, so the whole layout changes: Games moves up into the new column, and every column gets narrower.
tips_and_updatesTip: Use span 3 for three columns, or 1 / -1 to cover the full width, whatever the number of columns.
quizTest your knowledge
Edit the CSS on the left. Open the HTML tab to see the class names you can use. The mini browser updates as you type, and each goal gets a check mark when it's done.
auto_awesomeNo clicking needed. Each goal gets a check mark by itself once your code does it.
Make .hero cover two columns.not done yet
Make .hero cover two rows.not done yet
Run the .footer across all three columns.not done yet
Center the .footer in its row with justify-self.not done yet