HTML Tables

HTML Tables: Displaying Data with Structure

When you need to organize data into rows and columns — think price lists, schedules, or reports — HTML tables are your go-to tool. They offer structure, clarity, and a way to present complex info with precision.

The Basics: table, tr, th, and td

Here are the essential tags:

  • <table> — starts the table
  • <tr> — defines a row (table row)
  • <th> — defines a header cell (bold + centered by default)
  • <td> — defines a standard data cell
<table>
  <tr>
    <th>Fruit</th>
    <th>Color</th>
  </tr>
  <tr>
    <td>Apple</td>
    <td>Red</td>
  </tr>
  <tr>
    <td>Banana</td>
    <td>Yellow</td>
  </tr>
</table>

And a basic css for table borders

table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
HTML Table Basic Example

This structure is the heart of any HTML table.

Using colspan and rowspan

Sometimes you need a cell to stretch across multiple columns or rows. That’s where colspan and rowspan come in.

Example: colspan

<table>
  <tr>
    <th colspan="2">Fruits</th>
  </tr>
  <tr>
    <td>Apple</td>
    <td>Banana</td>
  </tr>
</table>
Table colspan

Example: rowspan

<table>
  <tr>
    <th rowspan="2">Fruit</th>
    <td>Apple</td>
  </tr>
  <tr>
    <td>Banana</td>
  </tr>
</table>
Table rowspan

colspan and rowspan help you group and align data intuitively.

Adding Borders and Basic Styling

By default, HTML tables are borderless. Add a border attribute for simple outlines:

<table border="1">
  <tr>
    <th>Item</th>
    <th>Price</th>
  </tr>
  <tr>
    <td>Item 1</td>
    <td>$5</td>
  </tr>
</table>
Adding Borders and Basic Styling

For cleaner designs, it’s better to style tables with CSS — but this gives you quick control while learning.

Table Captions and Summary

Use <caption> to describe what the table is about. It helps accessibility and comprehension.

<table border="1">
  <caption>Fruit Color Table</caption>
  <tr>
    <th>Fruit</th>
    <th>Color</th>
  </tr>
  <tr>
    <td>Cherry</td>
    <td>Red</td>
  </tr>
</table>
Table Captions and Summary

The summary attribute is obsolete in HTML5, so modern accessibility prefers clear <caption> tags and semantic layout.

Full Example: Styled Table with Caption

<table border="1">
  <caption>Fruit Information</caption>
  <tr>
    <th>Name</th>
    <th>Color</th>
    <th>Taste</th>
  </tr>
  <tr>
    <td>Apple</td>
    <td>Red</td>
    <td>Sweet</td>
  </tr>
  <tr>
    <td>Banana</td>
    <td>Yellow</td>
    <td>Mild</td>
  </tr>
</table>
Full Example: Styled Table with Caption

Best Practices for Tables

  • Use <th> for headings — it improves accessibility
  • Keep layout simple for readability on smaller screens
  • Add captions when data needs context
  • Use CSS for advanced styling (covered in later lessons)

Summary

HTML tables are essential for displaying structured data — whether for product lists, schedules, or stats. You now know how to:

  • Create rows and cells with <tr>, <th>, and <td>
  • Merge cells using colspan and rowspan
  • Add borders for visibility
  • Describe your table with <caption>

Check your understanding 1

Which tag is used to define a table row in HTML?
Choose one answer. You can try again.

Answer and explanation

<code><tr></code>

<tr> stands for 'table row' and is used to create a horizontal row of cells inside a table. Each row contains header cells (<th>) or data cells (<td>).

Check your understanding 2

The <td> tag is used to define a cell in the table that contains data, not a heading.
Choose one answer. You can try again.

Answer and explanation

True

<td> (table data) holds regular content like student names or scores. If you want a heading cell instead, you use <th>.

Check your understanding 3

Choose the option that lists all and only the correct statements.

  1. <table>
  2. <tr>
  3. <th>
  4. <ul>
Which tags are used in combination to construct a basic HTML table?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 2 and 3

A basic table is built using <table> (container), <tr> (row), and <th> or <td> (cells). <ul> is used for unordered lists, not tables.

Check your understanding 4

What does the colspan attribute do in a table cell?
Choose one answer. You can try again.

Answer and explanation

It combines cells horizontally

The colspan attribute lets a <td> or <th> span across multiple columns, effectively merging them horizontally. This is useful in layouts like marksheets or timetables.

Check your understanding 5

The <caption> tag can be used inside a table to provide a title for the table.
Choose one answer. You can try again.

Answer and explanation

True

Yes, the <caption> tag is placed directly after the <table> tag and serves as the title or description for the table. It's useful for context, especially in printed or accessible content.

Check your understanding 6

Choose the option that lists all and only the correct statements.

  1. <table border="1">
  2. <table style="width: 100%; border-collapse: collapse;">
  3. <summary>Exam result table</summary>
  4. <caption>Class 12 - Attendance</caption>
Which of the following are valid ways to style or describe a table?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 2 and 4

You can add borders using the border attribute or CSS styles. <caption> provides a label for the table. <summary> is not valid inside <table> — it's used with <details>.

Table Styling

To lean how to style the tables using CSS, refer Table Styling using CSS.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: HTML Forms →