HTML <table> tags

organizing similar records of data together on a webpage

// updated 2025-04-30 14:13

Tables

Tables display groups of similar data as a grid-like structure:

  • Rows typically show data observations of individual phenomena
  • Columns typically show values that describe those individuals

For example:

  • A row could show a city
  • A column could show data that describes each city, e.g.:
    • country
    • population
    • mayor

Structure

As part of semantic HTML, <table> tags allow us to make "two-dimensional lists" with headers, rows, columns and footers:

1<table>
2  <caption>Explaining the table for accessibility</caption>
3  <thead>
4    <tr>
5      <th scope="col">Column 1 Header</th>
6      <th scope="col">Column 2 Header</th>
7      <th scope="col">Column 3 Header</th>
8    </tr>
9  </thead>
10  <tbody>
11    <tr>
12      <td>Row 1, Column 1</td>
13      <td>Row 1, Column 2</td>
14      <td>Row 1, Column 3</td>
15    </tr>
16    <tr>
17      <td>Row 2, Column 1</td>
18      <td>Row 2, Column 2</td>
19      <td>Row 2, Column 3</td>
20    </tr>
21  </tbody>
22  <tfoot>
23    <tr>
24      <td>Total, Column 1</td>
25      <td>Total, Column 2</td>
26      <td>Total, Column 3</td>
27    </tr>
28  </tfoot>
29</table>    

Elements

We can see each tag as part of a hierarchy of tags:

table
TagLevelMeaning
<table>0the table
<caption>1a write-up that explains the table (usually used for screen readers for accessibility and usually hidden from sighted users)
<thead>1table heading section (usually a row to describe each column)
<tr>2table row
<th>3table heading
<tbody>1table body (main content)
<td>3 table definition (data cell)
<tfoot>1table footer (usually a row for numerical totals)

Attributes

One attribute to note is scope for table headings; these simply denote whether the heading refers to:

  • items that appear vertically below that heading as a column (col)
  • items that follow that heading as a row (row)
⬅️ older (in textbook-html)
🛡️ HTML <div> and semantic HTML tags
newer (in textbook-html) ➡️
HTML <form> and <input> tags 🛡️
⬅️ older (in code)
🛡️ HTML <div> and semantic HTML tags
newer (in code) ➡️
HTML <form> and <input> tags 🛡️
⬅️ older (posts)
🛡️ HTML <div> and semantic HTML tags
newer (posts) ➡️
HTML <form> and <input> tags 🛡️