Lesson 16 of 36
HTML Tutorial

Basic Tables

Mark up rows and cells with table, tr, th and td to present real tabular data.

3 min readBeginnerWeb FundamentalsHTML Tables

A table arranges data in rows and columns so you can compare values quickly: prices, schedules, scores. The key phrase is tabular data. If your data has a natural grid shape, a table is the right element.

The core elements#

  • <table> wraps the whole table.
  • <tr> (table row) creates a horizontal row.
  • <th> (table header) is a header cell, bold and centred by default.
  • <td> (table data) is a regular data cell.
html
<table>
  <tr>
    <th>Drink</th>
    <th>Size</th>
    <th>Price (Rs.)</th>
  </tr>
  <tr>
    <td>Espresso</td>
    <td>Small</td>
    <td>150</td>
  </tr>
  <tr>
    <td>Latte</td>
    <td>Medium</td>
    <td>220</td>
  </tr>
  <tr>
    <td>Cold brew</td>
    <td>Large</td>
    <td>280</td>
  </tr>
</table>

You build a table row by row. Every row should have the same number of cells (unless you use spans, covered later), and the columns line up automatically because browsers use the cells' positions.

Reading the structure#

Picture the table as a grid with three columns. The first <tr> holds the three header cells. Each following <tr> has three data cells, one per column. There is no "column" element in the content; columns emerge from the cells in each row.

Adding borders (a taste of CSS)#

By default, tables have no visible borders, which surprises beginners. Do not use the old border attribute. Add this CSS in a stylesheet later:

html
<style>
  table, th, td { border: 1px solid #444; border-collapse: collapse; }
  th, td { padding: 0.5rem 0.75rem; text-align: left; }
</style>

border-collapse: collapse merges the double borders between neighbouring cells into single lines. You will learn where <style> belongs in the head section.

Whitespace and empty cells#

Whitespace between cells is ignored, so format your source for readability. If a cell has no data, keep the <td> and leave it empty (or write "n/a", which is clearer for a reader). Removing the cell would shift the following cells to the left and break the grid.

Tables are for data, not layout#

In the early web, developers used tables to position everything on the page. That is now considered bad practice. Layout belongs to CSS (Flexbox and Grid), while tables should present data that has a header and a grid structure. We return to this in the accessibility lesson.

Common mistakes#

  • Using <td> for header cells instead of <th>.
  • Mismatched cell counts, which produce ragged rows.
  • Forgetting to close </tr>, </td> or </th>.
  • Using a table to position page elements.
  • Relying on the deprecated border and cellpadding attributes.

Try it yourself#

Create a "Weekly opening hours" table for Brew and Byte with two columns (Day, Hours) and seven rows. Use th for the headings. Then add the CSS from this lesson, placed temporarily in a style element in the head, and notice how the grid becomes visible. Add a fourth row to your drinks table with a cell that says "n/a" and keep the grid aligned.

Recap#

A table is built from table, tr, th and td, written row by row. Use th for headers, keep each row's cell count consistent, style borders with CSS, and keep tables for tabular data only.

Check your understanding

Lesson quiz

Optional

A short quiz to check what you just learned.

Questions
4
Time limit
5 min
Pass mark
70%
Best score
-