Log inSign up free
Getting Started, HTML
  1. Getting started

    1. Lesson 1: What Is HTML?
    2. Lesson 2: HTML Document Structure
    3. Lesson 3: HTML Tags
    4. Lesson 4: HTML Links
    5. Lesson 5: HTML Images
    6. Lesson 6: HTML Lists
    7. Lesson 7: HTML Tables
    8. Lesson 8: HTML Forms
    9. Lesson 9: Semantic HTML
Getting Started, HTML

Getting started · Lesson 7 of 9 · 3 min read · By Jack

Lesson 7: HTML Tables

By the end, you'll know how to build a table with headers, rows and cells, merge cells, add captions, and group table sections.

Estimated time: 20 minutes (about 8 minutes reading, 12 minutes practice)

Learning goals: By the end, you'll know how to build a table with headers, rows and cells, merge cells, add captions, and group table sections.

When to use tables
Tables are for tabular data: information that naturally fits rows and columns, like timetables, prices or scores. Don't use tables to lay out a page. That's a job for CSS.


Part 1: A basic table (4 min)

<table>  
  <tr>  
    <th>Name</th>  
    <th>Age</th>  
  </tr>  
  <tr>  
    <td>Amira</td>  
    <td>28</td>  
  </tr>  
  <tr>  
    <td>Tom</td>  
    <td>34</td>  
  </tr>  
</table>
ElementMeaning
<table>The whole table
<tr>A table row
<th>A header cell (bold and centred by default)
<td>A standard data cell

Every row should have the same number of cells (unless you merge cells, see Part 3). Browsers show tables without borders by default. CSS adds them.

Part 2: Captions and sections (4 min)

Add structure so tables are easier to read and more accessible.

<table>  
  <caption>Monthly Sales (£)</caption>  
  <thead>  
    <tr>  
      <th scope="col">Month</th>  
      <th scope="col">Sales</th>  
    </tr>  
  </thead>  
  <tbody>  
    <tr>  
      <th scope="row">January</th>  
      <td>1,200</td>  
    </tr>  
    <tr>  
      <th scope="row">February</th>  
      <td>1,450</td>  
    </tr>  
  </tbody>  
  <tfoot>  
    <tr>  
      <th scope="row">Total</th>  
      <td>2,650</td>  
    </tr>  
  </tfoot>  
</table>
  • <caption> is the table's title. It must come first inside <table>.
  • <thead>, <tbody> and <tfoot> group the header, main rows and footer.
  • scope="col" or scope="row" tells screen readers whether a header applies to a column or a row.

Part 3: Merging cells (4 min)

Use colspan to stretch a cell across columns and rowspan to stretch it down rows.

<table>  
  <tr>  
    <th colspan="2">Contact Details</th>  
  </tr>  
  <tr>  
    <td>Email</td>  
    <td>hello@example.com</td>  
  </tr>  
  <tr>  
    <td>Phone</td>  
    <td>01234 567890</td>  
  </tr>  
</table>
<table>  
  <tr>  
    <th rowspan="2">Day</th>  
    <th>Morning</th>  
    <th>Afternoon</th>  
  </tr>  
  <tr>  
    <td>Maths</td>  
    <td>Art</td>  
  </tr>  
</table>

When a cell spans several columns or rows, leave out the cells it covers, so the row totals still add up.


Quick reference

AttributeUsed onEffect
colspan<th>, <td>Spans multiple columns
rowspan<th>, <td>Spans multiple rows
scope<th>Says whether the header is for a column or row

Common mistakes

  • Using tables for page layout
  • Rows with different numbers of cells
  • Using <td> for headings instead of <th>
  • Putting <tr> directly inside <table> while also using <thead> or <tbody> (use sections consistently)
  • Forgetting to close </tr>, </td> or </table>
  • Adding borders with old attributes like border="1" instead of CSS

Key takeaways

  • <table> contains <tr> rows, which contain <th> and <td> cells.
  • Use <th> for headings and add scope for accessibility.
  • <caption>, <thead>, <tbody> and <tfoot> add meaning and structure.
  • colspan and rowspan merge cells.
  • Tables are for data, not layout.

Exercises (12 min)

  1. Identify (1 min). Which element is used for each?
    • a) A column heading
    • b) A row
    • c) The table's title

    Answers: a) <th>, b) <tr>, c) <caption>.
  2. Fix the errors (3 min).
    <table>  
      <tr>  
        <td>Fruit</td>  
        <td>Colour  
      </tr>  
      <td>Apple</td>  
      <td>Red</td>  
      <caption>Fruit Colours</caption>  
    </table>

    Hints: use <th> for headings, close the second </td>, wrap "Apple" and "Red" in a <tr>, and move <caption> to the top of the table.
  3. Build it (6 min). Create tables.html with a weekly timetable:
    • a <caption>
    • a <thead> with the days Monday to Friday as column headers (plus an empty first cell)
    • a <tbody> with at least three time slots, using <th scope="row"> for each time
    • one colspan cell, such as a lunch break stretching across all days
  4. Challenge (2 min). Add a <tfoot> row, and use rowspan to make a double-length lesson run across two time slots.

Lesson 7 checkpoint

  • I can build a table with <tr>, <th> and <td>.
  • I can add a caption and group rows with <thead>, <tbody> and <tfoot>.
  • I can merge cells with colspan and rowspan.
  • I know tables are for data, not layout.

Keep track of your learning

Create a free account to unlock them and track your progress.

  • Track your progress through every course
  • Take the quizzes and see your score
  • Download the worksheets and resources
  • Get a certificate when you finish

Related reading on our blog

Rather have it done for you?

We build websites for small businesses

Everything in this course is what we do every day for local businesses: fast, mobile-friendly sites that are built to be found on Google. See our web design services or our website packages.