Nextgen Blog
HomeLearnAIQuantumRoboticsSecuritySpaceAbout
Nextgen Blog

By Anubhav — full stack engineer, IIT Mandi CSE. Deep dives into AI, software engineering, and the technologies shaping the next generation of tech.

Navigate

  • Home
  • Learn
  • Blog
  • Archive
  • About

Topics

  • Artificial Intelligence
  • Quantum Computing
  • Robotics
  • Cybersecurity
  • Space Tech

Connect

Built with ❤️ & By - Anubhav

© 2026 Nextgen Blog by Anubhav. All rights reserved.

Privacy PolicyTerms of Service
Phase 02 · Media, Tables & FormsChapter 8 of 18
07.
Phase 02Read time ~12 minDifficulty beginner

Tables in HTML

Tables are used to represent structured data where information has meaningful relationships between rows and columns.

In this chapter
  • 01What Is a Table?
  • 02Basic <table>
  • 03<table>
  • 04<tr> — Table Row
  • 05<td> — Table Data Cell
  • 06<th> — Table Header Cell
  • 07Basic Proper Table
  • 08Visual Structure
  • 09<thead>
  • 10<tbody>
  • 11<tfoot>
  • 12<caption>
  • 13Complete Basic Table
  • 14Understanding Table Semantics
  • 15scope
  • 16Row Headers
  • 17Why scope Matters
  • 18colspan
  • 19rowspan
  • 20Combining rowspan and colspan
  • 21Complex Tables
  • 22Tables for Data, Not Layout
  • 23❌ Old approach
  • 24Modern approach
  • 25Table Styling
  • 26border Attribute
  • 27Table Alignment
  • 28Responsive Tables
  • 29Don't Turn Every Table Into Cards
  • 30Financial Table Example
  • 31Exam Results Example
  • 32Pricing Comparison Table
  • 33Table with Footer
  • 34The Browser's Table Model
  • 35Table vs List
  • 36Table vs Description List
  • 37Accessibility Checklist
  • 38Common Mistakes
  • 39❌ Mistake 1 — No headers
  • 40❌ Mistake 2 — Table for layout
  • 41❌ Mistake 3 — Using <div> for tabular data
  • 42❌ Mistake 4 — Styling through HTML attributes
  • 43Employee Management Table
  • 44Monthly Sales
  • 45Next chapter: Chapter 7 — HTML Forms Fundamentals
In this chapter (45 sections)
01What Is a Table?02Basic <table>03<table>04<tr> — Table Row05<td> — Table Data Cell06<th> — Table Header Cell07Basic Proper Table08Visual Structure09<thead>10<tbody>11<tfoot>12<caption>13Complete Basic Table14Understanding Table Semantics15scope16Row Headers17Why scope Matters18colspan19rowspan20Combining rowspan and colspan21Complex Tables22Tables for Data, Not Layout23❌ Old approach24Modern approach25Table Styling26border Attribute27Table Alignment28Responsive Tables29Don't Turn Every Table Into Cards30Financial Table Example31Exam Results Example32Pricing Comparison Table33Table with Footer34The Browser's Table Model35Table vs List36Table vs Description List37Accessibility Checklist38Common Mistakes39❌ Mistake 1 — No headers40❌ Mistake 2 — Table for layout41❌ Mistake 3 — Using <div> for tabular data42❌ Mistake 4 — Styling through HTML attributes43Employee Management Table44Monthly Sales45Next chapter: Chapter 7 — HTML Forms Fundamentals

Examples:

  • Employee records
  • Product specifications
  • Financial data
  • Exam results
  • Pricing comparisons
  • Schedules
  • Statistics
  • Reports

The most important rule is:

Use HTML tables for tabular data, not for creating webpage layouts.


6.1 What Is a Table?#

Imagine this data:

Name Age Country
Anubhav 30 India
Rahul 28 India
John 32 USA

The data has relationships:

Name ↔ Age ↔ Country

HTML can represent those relationships semantically.


6.2 Basic <table>#

The simplest table:

<table>
 
    <tr>
        <td>Anubhav</td>
        <td>30</td>
        <td>India</td>
    </tr>
 
    <tr>
        <td>Rahul</td>
        <td>28</td>
        <td>India</td>
    </tr>
 
</table>

The result is conceptually:

Anubhav    30    India
Rahul      28    India

But this isn't a properly structured data table yet.

We need to understand the individual elements.


6.3 <table>#

The outer container is:

<table>
    ...
</table>

It tells the browser:

This content represents tabular data.


6.4 <tr> — Table Row#

tr means:

Table Row

Example:

<tr>
    ...
</tr>

A table consists of rows.

<table>
│
├── <tr>
├── <tr>
└── <tr>

6.5 <td> — Table Data Cell#

td means:

Table Data

Example:

<td>Anubhav</td>

A row can contain multiple data cells:

<tr>
 
    <td>Anubhav</td>
    <td>30</td>
    <td>India</td>
 
</tr>

Conceptually:

┌─────────┬─────┬───────┐
│ Anubhav │ 30  │ India │
└─────────┴─────┴───────┘

6.6 <th> — Table Header Cell#

th represents a header cell.

Example:

<tr>
 
    <th>Name</th>
    <th>Age</th>
    <th>Country</th>
 
</tr>

Now the browser and assistive technologies know that:

Name    → header for a column
Age     → header for a column
Country → header for a column

6.7 Basic Proper Table#

A basic table should therefore look like:

<table>
 
    <tr>
        <th>Name</th>
        <th>Age</th>
        <th>Country</th>
    </tr>
 
    <tr>
        <td>Anubhav</td>
        <td>30</td>
        <td>India</td>
    </tr>
 
    <tr>
        <td>Rahul</td>
        <td>28</td>
        <td>India</td>
    </tr>
 
</table>

6.8 Visual Structure#

Conceptually:

┌──────────┬─────┬─────────┐
│   Name   │ Age │ Country │  ← Header
├──────────┼─────┼─────────┤
│ Anubhav  │ 30  │ India   │
├──────────┼─────┼─────────┤
│ Rahul    │ 28  │ India   │
└──────────┴─────┴─────────┘

6.9 <thead>#

For better structure, group header rows inside:

<thead>

Example:

<table>
 
    <thead>
 
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Country</th>
        </tr>
 
    </thead>
 
</table>

6.10 <tbody>#

The main body of the table goes inside:

<tbody>

Example:

<table>
 
    <thead>
 
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Country</th>
        </tr>
 
    </thead>
 
    <tbody>
 
        <tr>
            <td>Anubhav</td>
            <td>30</td>
            <td>India</td>
        </tr>
 
        <tr>
            <td>Rahul</td>
            <td>28</td>
            <td>India</td>
        </tr>
 
    </tbody>
 
</table>

6.11 <tfoot>#

<tfoot> contains summary/footer rows.

For example, an invoice:

<table>
 
    <thead>
        <tr>
            <th>Product</th>
            <th>Price</th>
        </tr>
    </thead>
 
    <tbody>
 
        <tr>
            <td>Keyboard</td>
            <td>₹2,000</td>
        </tr>
 
        <tr>
            <td>Mouse</td>
            <td>₹1,000</td>
        </tr>
 
    </tbody>
 
    <tfoot>
 
        <tr>
            <th>Total</th>
            <th>₹3,000</th>
        </tr>
 
    </tfoot>
 
</table>

Structure:

<table>
│
├── <thead>
│
├── <tbody>
│
└── <tfoot>

6.12 <caption>#

A table can have a caption:

<table>
 
    <caption>
        Employee Information
    </caption>
 
    ...
 
</table>

This tells users what the table represents.

Example:

<table>
 
    <caption>
        Employee salaries for 2026
    </caption>
 
    ...
 
</table>

A caption is especially useful for accessibility and context.


6.13 Complete Basic Table#

<table>
 
    <caption>
        Employee Information
    </caption>
 
    <thead>
 
        <tr>
            <th>Name</th>
            <th>Role</th>
            <th>Experience</th>
        </tr>
 
    </thead>
 
    <tbody>
 
        <tr>
            <td>Anubhav</td>
            <td>Software Engineer</td>
            <td>5 Years</td>
        </tr>
 
        <tr>
            <td>Rahul</td>
            <td>Backend Developer</td>
            <td>4 Years</td>
        </tr>
 
    </tbody>
 
</table>

6.14 Understanding Table Semantics#

Think of the table as:

Table
│
├── Caption
│
├── Header
│   ├── Name
│   ├── Role
│   └── Experience
│
└── Data
    ├── Anubhav
    ├── Software Engineer
    └── 5 Years

The browser and assistive technology can understand these relationships.


6.15 scope#

For accessible tables, <th> can use the scope attribute.

For column headers:

<th scope="col">Name</th>
<th scope="col">Age</th>
<th scope="col">Country</th>

This communicates:

This header applies to the cells in its column.


6.16 Row Headers#

Suppose you have:

Product January February
Laptop ₹80,000 ₹75,000
Phone ₹40,000 ₹38,000

The product names can be row headers:

<table>
 
    <thead>
 
        <tr>
            <th scope="col">Product</th>
            <th scope="col">January</th>
            <th scope="col">February</th>
        </tr>
 
    </thead>
 
    <tbody>
 
        <tr>
            <th scope="row">Laptop</th>
            <td>₹80,000</td>
            <td>₹75,000</td>
        </tr>
 
        <tr>
            <th scope="row">Phone</th>
            <td>₹40,000</td>
            <td>₹38,000</td>
        </tr>
 
    </tbody>
 
</table>

Notice:

<th scope="row">

rather than:

<td>

because "Laptop" and "Phone" identify the rows.


6.17 Why scope Matters#

Imagine a screen reader navigating:

Laptop → January → ₹80,000

The semantic relationships help the user understand:

₹80,000
    ↓
Laptop
    ↓
January

Without appropriate table semantics, complex tables can become difficult to understand.


6.18 colspan#

colspan allows a cell to span multiple columns.

Example:

<tr>
 
    <th colspan="3">
        Employee Information
    </th>
 
</tr>

If the table has three columns:

┌───────────────────────────────┐
│     Employee Information      │
├──────────┬─────┬─────────────┤
│ Name     │ Age │ Department  │
└──────────┴─────┴─────────────┘

The header occupies all three columns.


6.19 rowspan#

rowspan allows a cell to span multiple rows.

Example:

<tr>
 
    <th rowspan="2">
        Frontend
    </th>
 
    <td>HTML</td>
 
</tr>
 
<tr>
 
    <td>CSS</td>
 
</tr>

Conceptually:

┌───────────┬───────┐
│           │ HTML  │
│ Frontend  ├───────┤
│           │ CSS   │
└───────────┴───────┘

6.20 Combining rowspan and colspan#

You can combine them when the data structure requires it.

Example:

<table>
 
    <tr>
 
        <th rowspan="2">Product</th>
 
        <th colspan="2">
            Sales
        </th>
 
    </tr>
 
    <tr>
 
        <th>January</th>
        <th>February</th>
 
    </tr>
 
</table>

Structure:

┌─────────┬─────────────────────┐
│ Product │       Sales         │
│         ├──────────┬──────────┤
│         │ January  │ February │
└─────────┴──────────┴──────────┘

6.21 Complex Tables#

For more complicated tables, you may need:

  • scope
  • id
  • headers

For example:

<th id="name">Name</th>
<th id="salary">Salary</th>

Then a cell can reference headers:

<td headers="name">Anubhav</td>
<td headers="salary">₹30 LPA</td>

This is useful for complex table relationships.

For ordinary tables, correct use of <th> and scope is usually sufficient.


6.22 Tables for Data, Not Layout#

This is extremely important.

❌ Old approach#

Developers historically used tables to position webpage elements:

<table>
 
    <tr>
        <td>Logo</td>
        <td>Navigation</td>
    </tr>
 
    <tr>
        <td>Main Content</td>
        <td>Sidebar</td>
    </tr>
 
</table>

Don't do this.

Modern approach#

Use semantic HTML:

<header>
    ...
</header>
 
<main>
    ...
</main>
 
<aside>
    ...
</aside>

and CSS for layout.


6.23 Table Styling#

HTML:

<table>
 
    <thead>
        <tr>
            <th>Name</th>
            <th>Age</th>
        </tr>
    </thead>
 
    <tbody>
        <tr>
            <td>Anubhav</td>
            <td>30</td>
        </tr>
    </tbody>
 
</table>

CSS controls:

  • borders
  • spacing
  • alignment
  • colors
  • typography
  • responsive behavior

For example:

table {
    border-collapse: collapse;
}

We'll learn this in the CSS course.


6.24 border Attribute#

You may see old HTML like:

<table border="1">

Avoid this in modern HTML.

Presentation belongs in CSS.

Use:

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

6.25 Table Alignment#

Don't use old attributes such as:

<td align="center">

Modern HTML separates structure and presentation.

Use CSS:

td {
    text-align: center;
}

6.26 Responsive Tables#

Tables can be difficult to display on small screens.

For example:

Desktop
┌──────┬──────┬──────┬──────┬──────┐
│ Name │ Age  │ City │ Role │ Salary│
└──────┴──────┴──────┴──────┴──────┘

On mobile, the table might be wider than the viewport.

One common approach is to place the table inside a horizontally scrollable container:

<div class="table-container">
 
    <table>
        ...
    </table>
 
</div>

CSS can then allow horizontal scrolling.

The exact responsive design depends on the data.


6.27 Don't Turn Every Table Into Cards#

Sometimes developers completely transform a table into cards on mobile.

That can work visually, but be careful not to destroy the underlying data relationships and accessibility.

For genuinely tabular information, preserving table semantics is usually important.


6.28 Financial Table Example#

Consider:

<table>
 
    <caption>
        Monthly Revenue — 2026
    </caption>
 
    <thead>
 
        <tr>
            <th scope="col">Month</th>
            <th scope="col">Revenue</th>
            <th scope="col">Expenses</th>
            <th scope="col">Profit</th>
        </tr>
 
    </thead>
 
    <tbody>
 
        <tr>
            <th scope="row">January</th>
            <td>₹10,00,000</td>
            <td>₹7,00,000</td>
            <td>₹3,00,000</td>
        </tr>
 
        <tr>
            <th scope="row">February</th>
            <td>₹12,00,000</td>
            <td>₹7,50,000</td>
            <td>₹4,50,000</td>
        </tr>
 
    </tbody>
 
</table>

This is a proper use of a table because the information has row/column relationships.


6.29 Exam Results Example#

<table>
 
    <caption>
        Student Examination Results
    </caption>
 
    <thead>
 
        <tr>
            <th scope="col">Student</th>
            <th scope="col">HTML</th>
            <th scope="col">CSS</th>
            <th scope="col">JavaScript</th>
        </tr>
 
    </thead>
 
    <tbody>
 
        <tr>
            <th scope="row">Anubhav</th>
            <td>95</td>
            <td>90</td>
            <td>92</td>
        </tr>
 
        <tr>
            <th scope="row">Rahul</th>
            <td>88</td>
            <td>91</td>
            <td>85</td>
        </tr>
 
    </tbody>
 
</table>

6.30 Pricing Comparison Table#

A pricing comparison is another common example.

<table>
 
    <caption>
        Hosting Plans
    </caption>
 
    <thead>
 
        <tr>
            <th scope="col">Feature</th>
            <th scope="col">Basic</th>
            <th scope="col">Pro</th>
            <th scope="col">Enterprise</th>
        </tr>
 
    </thead>
 
    <tbody>
 
        <tr>
            <th scope="row">Storage</th>
            <td>10 GB</td>
            <td>100 GB</td>
            <td>1 TB</td>
        </tr>
 
        <tr>
            <th scope="row">Users</th>
            <td>5</td>
            <td>50</td>
            <td>Unlimited</td>
        </tr>
 
    </tbody>
 
</table>

6.31 Table with Footer#

Let's create a sales report:

<table>
 
    <caption>
        Sales Report
    </caption>
 
    <thead>
 
        <tr>
            <th scope="col">Product</th>
            <th scope="col">Units</th>
            <th scope="col">Revenue</th>
        </tr>
 
    </thead>
 
    <tbody>
 
        <tr>
            <th scope="row">Laptop</th>
            <td>20</td>
            <td>₹16,00,000</td>
        </tr>
 
        <tr>
            <th scope="row">Phone</th>
            <td>30</td>
            <td>₹12,00,000</td>
        </tr>
 
        <tr>
            <th scope="row">Tablet</th>
            <td>10</td>
            <td>₹3,00,000</td>
        </tr>
 
    </tbody>
 
    <tfoot>
 
        <tr>
            <th scope="row">Total</th>
            <td>60</td>
            <td>₹31,00,000</td>
        </tr>
 
    </tfoot>
 
</table>

6.32 The Browser's Table Model#

Think about a table like this:

                TABLE
                  │
        ┌─────────┼─────────┐
        ↓         ↓         ↓
      THEAD     TBODY     TFOOT
        │         │         │
        ↓         ↓         ↓
      Rows       Rows      Rows
        │         │         │
        ↓         ↓         ↓
      Cells      Cells     Cells

And:

Header cell → <th>
Data cell   → <td>

6.33 Table vs List#

Suppose you have:

HTML
CSS
JavaScript

That's a list:

<ul>
    <li>HTML</li>
    <li>CSS</li>
    <li>JavaScript</li>
</ul>

But:

Technology     Years Experience
HTML           5
CSS            5
JavaScript     4

has meaningful columns.

Use:

<table>

6.34 Table vs Description List#

Consider:

Processor
Intel i9
 
RAM
32 GB
 
Storage
2 TB

A description list can be appropriate:

<dl>
 
    <dt>Processor</dt>
    <dd>Intel i9</dd>
 
    <dt>RAM</dt>
    <dd>32 GB</dd>
 
    <dt>Storage</dt>
    <dd>2 TB</dd>
 
</dl>

But if you're comparing multiple products:

             Laptop A    Laptop B
Processor    i7          i9
RAM          16 GB       32 GB
Storage      1 TB        2 TB

a table is much more appropriate.


6.35 Accessibility Checklist#

For a table, consider:

✓ Caption when useful
✓ <th> for headers
✓ scope="col" for column headers
✓ scope="row" for row headers
✓ <thead>
✓ <tbody>
✓ <tfoot> where appropriate
✓ Proper relationships in complex tables
✓ Don't use tables for layout

6.36 Common Mistakes#

❌ Mistake 1 — No headers#

<table>
 
    <tr>
        <td>Name</td>
        <td>Age</td>
    </tr>
 
</table>

If these are headers, use <th>.


❌ Mistake 2 — Table for layout#

<table>
    <tr>
        <td>Header</td>
    </tr>
</table>

Use semantic layout elements and CSS.


❌ Mistake 3 — Using <div> for tabular data#

<div>
    Anubhav | 30 | India
</div>

If the data has real row/column relationships, use a table.


❌ Mistake 4 — Styling through HTML attributes#

Avoid:

<table border="1">

Use CSS.


🧠 Table Cheat Sheet#

Element Purpose
<table> Table
<caption> Table title/caption
<thead> Header section
<tbody> Main data
<tfoot> Footer/summary
<tr> Table row
<th> Header cell
<td> Data cell
scope="col" Column header relationship
scope="row" Row header relationship
colspan Span columns
rowspan Span rows

🧪 Practice Project#

Create:

Employee Management Table#

Your page should contain:

Employee Management
 
┌──────────┬──────────────┬──────────┬────────────┐
│ Name     │ Department   │ Exp.     │ Salary     │
├──────────┼──────────────┼──────────┼────────────┤
│ Anubhav  │ Engineering  │ 5 years  │ ₹26.5 LPA  │
│ Rahul    │ Engineering  │ 4 years  │ ₹20 LPA    │
│ Priya    │ Marketing    │ 3 years  │ ₹15 LPA    │
└──────────┴──────────────┴──────────┴────────────┘

Requirements:

  • <table>
  • <caption>
  • <thead>
  • <tbody>
  • <tfoot>
  • <th>
  • <td>
  • scope="col"
  • scope="row"

Then create another table:

Monthly Sales#

Use:

Month
Product
Units
Revenue

Create at least five rows.

Add a total in <tfoot>.


🎯 Chapter 6 Interview Questions#

  1. What is an HTML table?
  2. What is <table>?
  3. What is <tr>?
  4. What is <td>?
  5. What is <th>?
  6. Difference between <th> and <td>?
  7. What is <thead>?
  8. What is <tbody>?
  9. What is <tfoot>?
  10. What is <caption>?
  11. What is scope="col"?
  12. What is scope="row"?
  13. What is colspan?
  14. What is rowspan?
  15. How do you create a row header?
  16. How do you create a column header?
  17. Why are tables not recommended for webpage layouts?
  18. How do you make tables accessible?
  19. How do you make a wide table usable on mobile?
  20. When should you use a table instead of a list?
  21. When should you use a description list instead of a table?
  22. Why should table styling be done with CSS?
  23. How do you represent a table with multiple levels of headers?
  24. What are complex table header relationships?

🔥 The Core Decision#

When you have structured information, ask:

What is the relationship between the data?
                 │
        ┌────────┼─────────┐
        ↓        ↓         ↓
    Collection  Term +    Rows &
                meaning   Columns
        │        │         │
       <ul>     <dl>     <table>
       <ol>

The most important rule:

If the data makes sense because of its rows and columns, use a table.

Don't use a table simply because you want something to look like a grid.


Next chapter: Chapter 7 — HTML Forms Fundamentals#

We'll enter one of the most important parts of HTML: forms and user input. You'll learn <form>, <input>, <label>, name, value, action, method, GET vs POST, text fields, passwords, email, numbers, dates, URLs, telephone inputs, buttons, and how browsers actually submit form data.

Finished this chapter?

Mark it as complete to track your mastery.

Previous ChapterChapter 5 — Lists in HTMLNext ChapterChapter 7 — HTML Forms Fundamentals
PrevNext