Learn HTML and CSS: Tables
Tables
"Where are our opening hours?" asks Hilary. Somewhere in the third paragraph, between the phone number and the bank details for donations. A table lines them up at a glance.
Rows and cells
You know tables from Excel, or as the result of an SQL query: rows and columns. In HTML you build them row by row.
<table> is the whole table. Each row is a <tr>, short for "table row". Each cell in that row is a <td>, short for "table data".
You don't type columns: the first <td> of every row together make the first column, the second ones the second column.
In the tree you'll also see a <tbody> you never typed. The browser puts that around your rows itself.
<main> <h2>Opening hours</h2> <table> <tr> <td>Wednesday</td> <td>1 pm to 5 pm</td> </tr> <tr> <td>Saturday</td> <td>10 am to 4 pm</td> </tr> </table></main>Headings in a table
The top row says what's in each column, just like the column names above an SQL result or the first row of an Excel sheet. You make those cells with <th>, short for "table header", instead of <td>.
A <th> is bold by default, and here the shelter's CSS gives it a background. More importantly: a screen reader reads out the heading with every cell, like "Age: 8 years". Without headings the listener only hears loose words.
<main> <h2>Looking for a home</h2> <table> <tr> <th>Name</th> <th>Species</th> <th>Age</th> </tr> <tr> <td>Rex</td> <td>dog</td> <td>8 years</td> </tr> <tr> <td>Misty</td> <td>cat</td> <td>2 years</td> </tr> </table></main>Try it yourself
Put the opening hours in a table with the headings Day and Hours, and under them Wednesday with 1 pm to 5 pm and Saturday with 10 am to 4 pm.
<main> <h2>Opening hours</h2></main>body { margin: 0; font-family: system-ui, sans-serif; background: #fffaf3; color: #3d2b1f;}header { padding: 20px 16px 14px; background: #f6c88f;}header h1 { margin: 4px 0 0;}.slogan { margin: 4px 0 10px;}nav ul { display: flex; flex-wrap: wrap; gap: 4px 18px; margin: 0; padding: 0; list-style: none;}nav a { color: #7a3e12; font-weight: bold;}main { padding: 8px 16px 24px;}table { border-collapse: collapse;}th,td { padding: 6px 12px; border: 1px solid #e3c9a8; text-align: left;}th { background: #fbe3c4;}.card { margin: 12px 0; padding: 12px 16px; border-radius: 16px; background: #ffffff;}.card h3 { margin: 4px 0;}.box { padding: 4px 16px; border-radius: 16px; background: #fde9d4;}.breed { color: #b5541c;}.closed { color: #b42318; font-weight: bold;}footer { padding: 12px 16px; background: #f6c88f;}Ready for more?
The full lesson has 5 exercises, from easy to challenging, with hints, points and a streak you can build.
Start the lesson