HTML Tables

HTML tables are used to present tabular data—information that naturally fits into rows and columns. Common examples include pricing tables, schedules, comparison charts, and contact lists.

In this lesson, you’ll learn how to create HTML tables using the core table elements: <table>, <tr>, <th>, and <td>. You’ll also see how basic CSS can be used to improve table readability and appearance.

By the end of this lesson, you’ll understand how tables are structured and when it’s appropriate to use them.

Desired Outcomes:

By the end of this lecture, you should be able to:

  • Understand when tables should and should not be used
  • Create tables using <table>, <tr>, <th>, and <td>
  • Define table headers, rows, and data cells correctly
  • Understand the relationship between rows, headers, and cells
  • Apply basic CSS styles to improve table readability

Basic Table Structure

Tables in HTML are created using the <table> element. Inside the <table> element, we have rows defined by the <tr> (table row) element. Within each row, we define table headers using the <th> (table header) element or table cells using the <td> (table data) element.

  • <tr> defines a table row
  • <th> defines a header cell
  • <td> defines a data cell

A table is always built row by row, even when it visually looks like columns.

Example:

<table>
    <tr>
       <th>Header 1</th>
       <th>Header 2</th>
    </tr>
    <tr>
       <td>Data 1</td>
       <td>Data 2</td>
    </tr>
    <tr>
       <td>Data 3</td>
       <td>Data 4</td>
    </tr>
 </table>
Basic Table Structure
Basic Table Structure

How this table works

  • The table has two columns
  • The first row defines column headers
  • The remaining rows contain data

Each row must contain the same number of cells to keep the table aligned correctly.

Table Headers

Table headers (<th>) are used to define the headings of each column in a table. They help provide context and improve the readability of the table.

By default:

  • Header text is bold
  • Header text is centered

Example:

<table>
    <tr>
       <th>Name</th>
       <th>Email</th>
       <th>Phone</th>
    </tr>
    <tr>
       <td>John Doe</td>
       <td>johndoe@example.com</td>
       <td>123-456-7890</td>
    </tr>
    <!-- More rows... -->
 </table>
Table headers
Table headers

In this example, the first row contains table headers for the Name, Email, and Phone columns.

Why table headers matter

  • Improve readability
  • Provide context for data
  • Help users understand table structure
  • Prepare tables for accessibility improvements later

Table Cells

Table cells (<td>) hold the actual data within a table. Each cell corresponds to a specific row and column intersection. Table cells provide the content for each column in the table.

Example:

<table>
    <tr>
       <th>Name</th>
       <th>Email</th>
       <th>Phone</th>
    </tr>
    <tr>
       <td>John Doe</td>
       <td>johndoe@example.com</td>
       <td>123-456-7890</td>
    </tr>
    <!-- More rows... -->
 </table>
Table cells
Table cells

In this example, the second row contains table cells with data for the Name, Email, and Phone columns.

Important concepts

  • Each <td> belongs to a specific row
  • Cells align automatically under their headers
  • The browser handles layout, spacing, and alignment by default

Understanding this relationship between rows, headers, and cells is key to building clean tables.

Styling Tables

You can customize the appearance of tables using CSS. By applying CSS styles to the <table>, <th>, and <td> elements, you can change the table's layout, font, color, spacing, and more.

Example CSS:

table {
   width: 100%;
   border-collapse: collapse;
}

th, td {
   padding: 8px;
   border: 1px solid black;
}

th {
   background-color: #f2f2f2;
}

td {
   text-align: center;
}

This example shows the most common table styling techniques you’ll use as a beginner.

What this styling does

  • Makes the table span the full width of its container
  • Removes double borders between cells
  • Adds spacing inside cells
  • Highlights header rows
  • Centers text in data cells

This example demonstrates how CSS enhances tables without changing their HTML structure.

Styling tables
Styling tables

By customizing the styles of your tables, you can match them to the design and aesthetics of your webpages.

Tables are a versatile tool for displaying tabular data on your webpages. 

Conclusion

HTML tables are a powerful way to present structured data clearly and efficiently. By understanding table structure, headers, rows, and cells, you can create tables that are easy to read and easy to maintain.

As you continue learning HTML, you’ll build on this foundation by adding forms and interactive elements that allow users to input and submit data.

In the next lesson, you’ll learn about HTML forms, which introduce user interaction into your webpages.

This lesson is part of our complete Learn HTML guide.

When to Use Tables (Important Best Practice)

Tables should be used only for tabular data — information that logically fits into rows and columns.

✔️ Good use cases:

  • Schedules
  • Price comparisons
  • Contact lists
  • Data reports

❌ Avoid using tables for:

  • Page layout
  • Positioning elements
  • General page structure

Layout should be handled using CSS, not tables.

Updated on:

Part of our complete: Learn to Code HTML guide

-33%

Time remaining:
days 00: 00: 00

Learn to code HTML & CSS

From Zero to Hero

Check out this course on Udemy, now at 75% off! Inside this interactive course, I will teach you how to develop websites from scratch moving from beginner to advanced concepts. I take time to explain every detail and finish off by building some real-world websites.

HTML & CSS: From Zero to Hero
Learn to code HTML & CSS

-33%

Time remaining:
days 00: 00: 00

Learn to code JavaScript & jQuery

From Zero to Hero

Check out this course on Udemy, now at 75% off! Inside this interactive course, I will teach you how to develop components from scratch moving from beginner to advanced concepts. I take time to explain every detail and finish off by building some real-world reusable components.

JavaScript & jQuery: From Zero to Hero
Learn to code JavaScript & jQuery

-25%

Time remaining:
days 00: 00: 00

Learn Frontend Web Development

From Zero to Hero

Check out this course on Udemy, now at 75% off! A bundle of the previous two courses. Inside this interactive course, I will teach you how to develop websites from scratch moving from beginner to advanced concepts. I take time to explain every detail and finish off by building some real-world websites.

Frontend Web Development: From Zero to Hero
Learn Frontend Web Development