On this page9 sections
  1. Learning Objectives
  2. 1. Rows and Cells
  3. 2. Caption, Head, Body and Foot
  4. 3. Merging Cells
  5. 4. Row Headers With scope
  6. Common Beginner Mistakes
  7. Practice Questions
  8. Practice Answers
  9. Key Takeaways

Lecture 013: Tables

Category3: Links, Images, Tables & Lists
Lecture013 of 048
Estimated video length14 min
TargetHTML5 · any modern browser
LevelBeginner

Timetables, price lists, exam results: when data has rows and columns, it belongs in a table. A table is built row by row, and each row is made of cells.


Learning Objectives

  • Build a table from <table>, <tr>, <th> and <td>
  • Group rows with <thead>, <tbody> and <tfoot>, and add a <caption>
  • Merge cells with colspan and rowspan

1. Rows and Cells

ElementMeaning
<table>The whole table
<tr>One table row
<th>A header cell (bold and centred by default)
<td>A normal data cell

Example

html
<table border="1">
  <tr>
    <th>Name</th>
    <th>Marks</th>
  </tr>
  <tr>
    <td>Ayesha</td>
    <td>92</td>
  </tr>
  <tr>
    <td>Bilal</td>
    <td>85</td>
  </tr>
</table>

border="1" is only there so you can see the cells. Real borders come from CSS.


2. Caption, Head, Body and Foot

These elements group the rows and give the table a title. Screen readers use them to read tables aloud sensibly.

Example

html
<table border="1">
  <caption>Monthly expenses</caption>
  <thead>
    <tr><th>Item</th><th>Cost (Rs)</th></tr>
  </thead>
  <tbody>
    <tr><td>Rent</td><td>30,000</td></tr>
    <tr><td>Food</td><td>18,000</td></tr>
    <tr><td>Transport</td><td>6,000</td></tr>
  </tbody>
  <tfoot>
    <tr><th>Total</th><td>54,000</td></tr>
  </tfoot>
</table>

3. Merging Cells

  • colspan="2": a cell stretches across 2 columns
  • rowspan="2": a cell stretches down 2 rows

Example: a class timetable:

html
<table border="1">
  <tr>
    <th>Time</th><th>Monday</th><th>Tuesday</th>
  </tr>
  <tr>
    <td>9:00</td><td>Maths</td><td rowspan="2">Science lab</td>
  </tr>
  <tr>
    <td>10:00</td><td>English</td>
  </tr>
  <tr>
    <td>11:00</td><td colspan="2">Sports day</td>
  </tr>
</table>

When a cell spans, leave out the cells it covers. "Science lab" covers Tuesday 10:00, so that row has one cell fewer.


4. Row Headers With scope

A <th> can label a row as well as a column. scope says which one.

Example

html
<table border="1">
  <tr><th scope="col">Plan</th><th scope="col">Price</th></tr>
  <tr><th scope="row">Basic</th><td>Free</td></tr>
  <tr><th scope="row">Pro</th><td>Rs 999 / month</td></tr>
</table>

Common Beginner Mistakes

1. Tables for page layout. Old sites used tables to place columns. Use CSS Flexbox and Grid (CSS Lectures 049–052). Tables are for data.

2. Rows with different cell counts. Every row should add up to the same number of columns, counting colspans.

3. Forgetting to drop covered cells. A rowspan or colspan cell takes the place of others. Remove them, or the table gets an extra column.

4. No header cells. Without <th>, nobody knows what each column means.


Practice Questions

  1. Which element makes a row?
  2. What is the difference between <th> and <td>?
  3. Which element gives a table a visible title?
  4. A cell should cover three columns. Which attribute and value do you use?
  5. Why shouldn't you use tables to lay out a whole page?
  6. Build a 2-column table with headers "Day" and "Weather", and two rows of data.

Practice Answers

  1. <tr>
  2. <th> is a header cell (a label); <td> is a normal data cell.
  3. <caption>
  4. colspan="3"
  5. Tables are meant for data. Layout tables are hard to make responsive and confuse screen readers. Use CSS layout instead.
  6. html
    <table border="1">
      <tr><th>Day</th><th>Weather</th></tr>
      <tr><td>Monday</td><td>Sunny</td></tr>
      <tr><td>Tuesday</td><td>Rain</td></tr>
    </table>

Key Takeaways

  • <table> → <tr> rows → <th> header cells and <td> data cells.
  • <caption>, <thead>, <tbody> and <tfoot> organise and label the table.
  • colspan and rowspan merge cells; drop the cells they cover.
  • Tables are for data, never for page layout.