Aral Code

Lists & Tables Lesson 22 of 42

HTML Tables — Rows, Columns and Headers

Build HTML tables with table, tr, th and td. Learn table headers, captions, borders and when tables should (and should not) be used.

Class schedules, grade sheets, jeepney fare matrices, price lists, election tallies — any information that fits into rows and columns belongs in a table. HTML tables can look intimidating at first because of all the tags, but they follow one simple pattern.

The four basic table elements

ElementMeaning
<table>The whole table
<tr>Table row
<th>Table header cell (bold and centered by default)
<td>Table data cell (a normal cell)

The key idea: a table is built row by row. You open a row, fill in its cells from left to right, close the row, then start the next one.

Example A simple fare table
<table border="1">
  <tr>
    <th>Route</th>
    <th>Regular Fare</th>
    <th>Student Fare</th>
  </tr>
  <tr>
    <td>Cubao – Quiapo</td>
    <td>₱15</td>
    <td>₱12</td>
  </tr>
  <tr>
    <td>Cubao – Divisoria</td>
    <td>₱18</td>
    <td>₱14</td>
  </tr>
</table>

Count the rows: 3 <tr> elements make 3 rows. Each row has 3 cells, so we get 3 columns.

Adding borders

The border="1" attribute in the example is an old shortcut. It works, but the modern way is CSS, which gives you much nicer results:

Example A table styled with CSS
<style>
  table { border-collapse: collapse; font-family: Arial, sans-serif; }
  th, td { border: 1px solid #999; padding: 8px 14px; }
  th { background-color: #0f5c4d; color: white; }
</style>
<table>
  <tr>
    <th>Time</th>
    <th>Monday</th>
    <th>Tuesday</th>
  </tr>
  <tr>
    <td>7:30 – 8:30</td>
    <td>Mathematics</td>
    <td>Science</td>
  </tr>
  <tr>
    <td>8:30 – 9:30</td>
    <td>English</td>
    <td>Filipino</td>
  </tr>
  <tr>
    <td>9:30 – 9:45</td>
    <td>Recess</td>
    <td>Recess</td>
  </tr>
</table>

border-collapse: collapse merges the double borders into single lines, and padding adds breathing room inside each cell.

Table captions

A <caption> gives the table a title. It must be the first thing inside <table>. Screen readers announce it, so users know what the table is about before hearing the data.

Example A captioned table
<table border="1">
  <caption>Quarterly Grades – Grade 10 Rizal</caption>
  <tr>
    <th>Subject</th>
    <th>Q1</th>
    <th>Q2</th>
  </tr>
  <tr>
    <td>Mathematics</td>
    <td>88</td>
    <td>91</td>
  </tr>
  <tr>
    <td>TLE – ICT</td>
    <td>94</td>
    <td>95</td>
  </tr>
</table>

Row headers

<th> isn't only for the top row. When the first cell of each row labels that row, make it a <th> too. Add scope="col" or scope="row" so screen readers know which direction the header applies to:

Example Headers for columns and rows
<table border="1">
  <tr>
    <th scope="col">Day</th>
    <th scope="col">Weather</th>
    <th scope="col">Temp</th>
  </tr>
  <tr>
    <th scope="row">Monday</th>
    <td>☀️ Sunny</td>
    <td>33°C</td>
  </tr>
  <tr>
    <th scope="row">Tuesday</th>
    <td>🌧️ Rainy</td>
    <td>27°C</td>
  </tr>
</table>

Empty cells

Every row should have the same number of cells. If a cell has no data, keep it but leave it empty — <td></td> — so the columns stay lined up.

When NOT to use tables

Years ago, people built whole page layouts with tables — the logo in one cell, the menu in another. Don't do this. Tables are for tabular data only: information that is naturally compared across rows and columns. For page layout, use CSS (you'll get a taste in the Layout lesson).

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which element creates a table row?
2. Which element is for a header cell?
3. Where must <caption> be placed?
4. Which is the best use of a table?
5. What does border-collapse: collapse do?

By Sir Reyel · Last updated 2026-12-04