Please enable JavaScript to use CodeHS

Web Design Tutorial

HTML Tables

Game schedules, league standings, meal plans: whenever information lines up in rows and columns, a table is the tool for the job. Learn the four tags behind every HTML table, then build your own, cell by cell.

html ~15 min Beginner friendly
The Big Idea

What tables are for

Tables are commonly used to organize and display information on web pages. A table shows information in a grid: rows run horizontally (side to side) and columns run vertically (up and down). Each little box where a row and a column meet is called a cell.

Think about where you already see grids like this every day:

  • A class schedule: one row per class period, columns for time, subject, and room.
  • Sports standings: one row per team, columns for wins, losses, and points.
  • A meal plan: one row per day, columns for breakfast, lunch, and dinner.

Whenever every row describes one thing and every column describes one fact about it, that data belongs in a table.

Tables are for data, not for page layout. A long time ago, people used tables to position everything on a page: menus, pictures, whole designs. Today that job belongs to CSS. Save the <table> tag for actual rows and columns of related information, like scores and schedules.


The Roster

The four table tags

There are four tags that you will need to create a table in HTML. Think of them as the team roster: each one has exactly one job.

<table> The containerWraps around ALL the table data. One per table.
<tr> Table rowHolds a single horizontal row of cells.
<th> Table headerOne header cell, usually a column title. Bold and centered.
<td> Table dataOne regular cell of data.

Let's start building! First, the <table> tag encloses the whole table. Tables are easier to see when they have a border, and the default HTML table does not show one. So we add an attribute (a characteristic) called border to the opening tag:

index.html
<table border="1">
    All table information will go inside here
</table>

On its own, this table displays nothing at all. A table only shows up once it has at least one row with a header or data cell inside. That is what the next section is about.


How It Reads

Rows first, then cells

Here is the secret to every HTML table: you never write a column. You build the table one row at a time, and inside each row you write the cells from left to right. The columns appear on their own, because every row lines its cells up in the same order.

Step 1 Open the table

The browser sees <table> and gets a grid ready.

Step 2 Read a row

Each <tr> starts a new horizontal row.

Step 3 Fill in cells

Each <th> or <td> fills one cell, left to right.

Let's try the smallest useful table: one row with two header cells. Notice how every opening tag has a matching closing tag, and how the indentation shows what is inside what. Press Run to see how the browser draws it:

index.html
<h1>Contact List</h1>
<table border="1">
    <tr>
        <th>Name</th>
        <th>Address</th>
    </tr>
</table>
Browser
rendered page

Contact List

Name Address

It is not much of a table with just one row, right? Rows of real data use <td> cells instead of <th>. Add a second <tr> with two <td> tags and the grid starts to grow:

index.html
<table border="1">
    <tr>
        <th>Name</th>
        <th>Address</th>
    </tr>
    <tr>
        <td>Maya Torres</td>
        <td>12 Oak Street</td>
    </tr>
</table>
Browser
rendered page
Name Address
Maya Torres 12 Oak Street

Indentation is your best friend here. Tables use a lot of tags, and it is easy to get lost or forget a closing tag. Indent each level (table, then tr, then th/td) a little further, and check that every opening tag has a matching closing tag before you move on.

Your Turn

Practice: meal prep

1

Meal Prep Table

Start with this table and finish the week: it has a header row plus Monday and Tuesday. Add 3 more rows (Wednesday, Thursday, Friday) using a <tr> tag and <td> tags for each. Up for a challenge? Add a whole new Snack column, and see if you can make the days of the week bold.

index.html (starter)
<table border="1">
    <tr>
        <th>Day</th>
        <th>Breakfast</th>
        <th>Lunch</th>
        <th>Dinner</th>
    </tr>
    <tr>
        <td>Monday</td>
        <td>scrambled eggs</td>
        <td>turkey sandwich</td>
        <td>chicken salad</td>
    </tr>
    <tr>
        <td>Tuesday</td>
        <td>oatmeal</td>
        <td>salad</td>
        <td>salmon</td>
    </tr>
</table>
index.html
<table border="1">
    <tr>
        <th>Day</th>
        <th>Breakfast</th>
        <th>Lunch</th>
        <th>Dinner</th>
    </tr>
    <tr>
        <th>Monday</th>
        <td>scrambled eggs</td>
        <td>turkey sandwich</td>
        <td>chicken salad</td>
    </tr>
    <tr>
        <th>Tuesday</th>
        <td>oatmeal</td>
        <td>salad</td>
        <td>salmon</td>
    </tr>
    <tr>
        <th>Wednesday</th>
        <td>yogurt</td>
        <td>pizza</td>
        <td>tacos</td>
    </tr>
    <tr>
        <th>Thursday</th>
        <td>pancakes</td>
        <td>soup</td>
        <td>stir fry</td>
    </tr>
    <tr>
        <th>Friday</th>
        <td>smoothie</td>
        <td>burrito</td>
        <td>spaghetti</td>
    </tr>
</table>
Browser
rendered page
DayBreakfastLunchDinner
Mondayscrambled eggsturkey sandwichchicken salad
Tuesdayoatmealsaladsalmon
Wednesdayyogurtpizzatacos
Thursdaypancakessoupstir fry
Fridaysmoothieburritospaghetti

Spot the bold-days trick? Each day uses a <th> instead of a <td>. Header cells are automatically bold, and they work at the start of a row, not just in the top row. For the Snack challenge, add one more <th> to the top row and one more <td> to every other row.


Label It

Headers and captions

A grid full of numbers is useless if nobody knows what the numbers mean. HTML gives you two labeling tools:

  • The header row: the first <tr>, filled with <th> cells that hold each column's title. The browser makes them bold and centered so they stand out from the data rows.
  • A caption: the <caption> tag gives the whole table a title. It goes directly after the opening <table> tag.

You can also add scope="col" to a header cell. It does not change how anything looks, but it tells screen readers "this header labels the column below me," which makes your table easier to use for everyone:

index.html
<table border="1">
    <caption>League Standings</caption>
    <tr>
        <th scope="col">Team</th>
        <th scope="col">Wins</th>
        <th scope="col">Losses</th>
    </tr>
    <tr>
        <td>Hawks</td>
        <td>9</td>
        <td>3</td>
    </tr>
    <tr>
        <td>Lions</td>
        <td>8</td>
        <td>4</td>
    </tr>
</table>
Browser
rendered page
League Standings
Team Wins Losses
Hawks 9 3
Lions 8 4

What happens if you swap <th> for <td>? The table still works, but the titles lose their bold, centered look, and screen readers can no longer tell titles from data. Try it in the table builder below and watch the difference.


Game Day Look

Styling with CSS

The border="1" attribute is the quick, old-school way to see your table. For a table that actually looks good, use a few lines of CSS instead:

  • border draws a line around the table and each cell.
  • border-collapse: collapse merges the doubled-up lines into one clean grid.
  • padding gives the text inside each cell some breathing room.
index.html
<style>
    table, th, td {
        border: 2px solid darkgreen;
        border-collapse: collapse;
    }
    th, td {
        padding: 8px 14px;
    }
    th {
        background-color: lightgreen;
    }
</style>

<table>
    <tr>
        <th>Team</th>
        <th>Points</th>
    </tr>
    <tr>
        <td>Hawks</td>
        <td>412</td>
    </tr>
    <tr>
        <td>Lions</td>
        <td>398</td>
    </tr>
</table>
Browser
rendered page
Team Points
Hawks 412
Lions 398

Same tags, new uniform. Notice the HTML part did not change at all: still <table>, <tr>, <th>, and <td>. The structure lives in HTML; the style lives in CSS.

Interactive

Build your own table

Time to run some drills. Set the number of rows and columns, switch the header row on or off, and give your table a caption. Then click any cell in the rendered table to edit its text. The HTML writes itself on the left while the browser view updates on the right. Flip on Highlight structure to see every <tr>, <th>, and <td> color-coded.

Table builder Rows 4 Columns 4

Click any cell in the rendered table to change what it says. Leave the caption box empty and the <caption> tag disappears from the HTML too.

Your HTML

            
Rendered page (click a cell to edit)
<tr> (odd rows) <tr> (even rows) <th> header cell <td> data cell

Your Turn

Practice: family tree

2

My Family Tree

Now it's your turn to create a table completely from scratch. Build a table about your family tree: relatives, friends, pets, or anyone you'd like to include. Use at least 5 rows and at least 5 header cells. Some ideas for your headers: Name, Relationship, Age, Best Quality, Fun Memory. Don't forget every closing tag, and keep your indentation tidy!

index.html
<table border="1">
    <caption>My Family Tree</caption>
    <tr>
        <th>Name</th>
        <th>Relationship</th>
        <th>Age</th>
        <th>Best Quality</th>
        <th>Fun Memory</th>
    </tr>
    <tr>
        <td>Sam</td>
        <td>brother</td>
        <td>16</td>
        <td>funny</td>
        <td>beach trip</td>
    </tr>
    <tr>
        <td>Priya</td>
        <td>best friend</td>
        <td>13</td>
        <td>caring</td>
        <td>science fair</td>
    </tr>
    <tr>
        <td>Biscuit</td>
        <td>dog</td>
        <td>4</td>
        <td>spontaneous</td>
        <td>ate my sock</td>
    </tr>
    <tr>
        <td>Grandma Rosa</td>
        <td>grandmother</td>
        <td>68</td>
        <td>wise</td>
        <td>baking cookies</td>
    </tr>
</table>
Browser
rendered page
My Family Tree
NameRelationshipAgeBest QualityFun Memory
Sambrother16funnybeach trip
Priyabest friend13caringscience fair
Biscuitdog4spontaneousate my sock
Grandma Rosagrandmother68wisebaking cookies

Count check: that is 5 rows (1 header + 4 data) and 5 header cells across the top. Every row has exactly 5 cells, which is what keeps the columns lined up.


Check Yourself

Quick quiz

1. Match the job to the tag: which one creates a single horizontal ROW of the table?

2. You change every <th> in the top row to <td>. What happens?

3. Which of these is a job HTML tables SHOULD be used for?

Keep building web pages

Congratulations on learning how to create and format tables using HTML! Keep the streak going: organize a page with lists, or bring your new tables to life with color.