Lesson 3 of 7
Geological Wonders
Put facts in a table with HTML. Then change how the table looks with CSS.
What you will learn
- <table>
- <tr>, <th>, <td>
- Linking a stylesheet
- CSS selectors
- :hover
- :nth-child
Before you start
Postcard. You know the tags that make a page. This lesson adds a second file called a stylesheet. That stylesheet holds your first CSS.
What this page does
- It shows a table. Each row is one geological wonder.
- Every second row has a slightly different colour. This is called zebra striping. It makes long tables easier to read.
- Point at a row. The row changes colour. This tells you which row you are on.
How a table is built
A table is made of rows. Each row is made of cells. Four tags do all the work.
| Tag | What it is | Notes |
|---|---|---|
<table> | The whole table | It holds everything else. |
<tr> | One row | tr means table row. |
<th> | A header cell | The browser makes it bold and centred. Use it for column names. |
<td> | A data cell | It holds your content. |
<table>
<tr> <!-- one row -->
<th>Column A</th> <!-- a header cell -->
<th>Column B</th>
</tr>
<tr> <!-- another row -->
<td>some data</td> <!-- a data cell -->
<td>more data</td>
</tr>
</table>Build it step by step
Build the structure first. Add the style last. You can open the page in a browser after every step.
Make the page
Make a file called
index.html. Add<!DOCTYPE html>, then the<html>,<head>, and<body>tags. Put a<title>in the<head>. Also put the stylesheet link in the<head>. Open the page. It is blank, and your title is on the browser tab.<link rel="stylesheet" href="index.css">Choose your columns
Write down the facts that describe each item in your topic. For video games you might use Title, Studio, Genre, and Year. These become your header row.
Make the header row
Inside
<body>, add a<table>. Inside it add one<tr>with one<th>for each column. Open the page. You see your column names in bold.<table> <tr> <th>Title</th> <th>Studio</th> <th>Year</th> </tr> </table>Add one row of data
Under the header row, add a
<tr>with the same number of<td>cells. Fill it with one real item. Check that the cells line up under the headers. This row is your template.Add the rest of the rows
Copy that row once for each item. Change the content each time. Keep the number of cells the same in every row. Your table is now complete, but it has no style yet.
Add the style
Make a file called
index.css. Add one rule at a time and reload the page after each one. This way you see what each rule does.index.css body { background: #f4f6f9; color: #0f172a; } table { border-collapse: collapse; width: 90%; margin: 2rem auto; } th, td { border: 1px solid #cfd6e0; padding: 0.75rem; } th { background: #e8ecf2; } tr:nth-child(odd) { background-color: #f0f3f7; } tr:hover { background-color: #e4e9f0; }
border-collapse: collapseremoves the gap between cell borders. Delete this line to see what it does.margin: 2rem autoputs space above and below the table. The wordautocentres the table on the page.th, tdis one rule for two tags. The comma means and.paddingadds space inside a cell, between the border and the text.
Change the styling
Every one of these is a line in index.css. Move a control, then open that file and find the line that changed.
The space inside each cell, between the border and the text. Set it to 0 and see why it matters.
Selectors you will use
A selector says which parts of the page a CSS rule changes. This example uses six kinds.
- Element selector. Write
tableortd. It selects every tag of that type. - Group selector. Write
th, td. It applies one rule to more than one tag. - Class selector. Write
.wonder-title. It selects only tags withclass="wonder-title". Use it to style some cells and not others. - Descendant selector. Write
.wonder-title b. It selects a<b>tag inside a.wonder-titlecell. - Pseudo-class. Write
:hoveror:nth-child(odd). It selects by state or by position. - Position selector. Write
td:nth-child(4). It selects the 4th cell in every row. Use it to make one column narrower.
Try these changes
Make these changes in the files beside this guide. Each one only uses what this lesson has already covered. Tick each one when it works.
Check yourself
Three quick questions on this lesson. Get them all right to finish the lesson and earn a pack.
Loading your answers
Teacher's versionHow to run this lesson, what to check, and any answers. Students do not need this.
How to teach it
- Let the student pick the topic before they write any code. A topic the student owns keeps them working through the whole lesson.
- Build the header row, then one data row, and stop. Check that the cells line up before the student copies the row ten times.
- Add the CSS one rule at a time. Reload after each rule. The student should be able to name what each rule changed.
- Delete
border-collapse: collapseon purpose and reload. The double borders appear. Put it back. - Change
:nth-child(odd)toevenand reload. The stripes move onto the header row and the first data row. This is the fastest way to show what a pseudo-class does.