HTML Tables0%

HTML Tables

Beginner8 min readβ€’Updated: 2026-09-10
Study Materials

HTML Tables (Rows, Columns & Data Grids) πŸ“Š

Welcome to Chapter 8: Tables in HTML!

Look at the classroom wall or your school diary. What is pinned on the softboard?

  • Your Weekly Class Timetable (Monday to Saturday periods).
  • Your Term Examination Date Sheet (Dates, Timing, and Subject names).
  • Your Annual School Report Card / Marksheet (Subject, Maximum Marks, Marks Obtained, Grade).
  • An IPL Cricket Points Table (Matches Played, Won, Lost, Points, Net Run Rate).

All of these are real-world examples of Tabular Data!

When information is arranged into clean horizontal rows and vertical columns, our eyes can compare numbers and read facts in a split second.

In this lesson, you will learn how to build clean, professional data tables using HTML!


The Grid Concept: Rows, Columns & Cells πŸ“

Before writing code, let's understand how a table is constructed:

Output
Column 1 Column 2 Column 3
β”‚ β”‚ β”‚
β–Ό β–Ό β–Ό
Row 1 ──► β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Subject β”‚ Max Marks β”‚ Marks Scored β”‚ <── Header Row
Row 2 ──► β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Mathematics β”‚ 100 β”‚ 95 β”‚ <── Data Row
Row 3 ──► β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Science β”‚ 100 β”‚ 92 β”‚ <── Data Row
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β–²
β”‚
A single box inside is called a CELL
  • Row (Horizontal ➑️): Goes from left to right.
  • Column (Vertical ⬇️): Goes from top to bottom.
  • Cell (Single Box πŸ“¦): Every small box where a row and column intersect.

The 4 Fundamental Table Tags 🧱

To build any basic table in HTML, you only need four core tags:

TagFull NameWhat It Does
<table>Table ContainerThe outer box that holds the entire table together.
<tr>Table RowCreates a new horizontal row from left to right.
<th>Table HeaderA special cell for column titles. Text is automatically bold and centered!
<td>Table DataA normal cell holding regular information. Text is regular and left-aligned.

Writing Your Very First HTML Table

Let's build a simple student report card table:

HTML
<table border="1" style="border-collapse: collapse; width: 100%; text-align: left;">
<!-- Row 1: Column Headings -->
<tr>
<th style="padding: 8px; background-color: #F1F5F9;">Subject</th>
<th style="padding: 8px; background-color: #F1F5F9;">Max Marks</th>
<th style="padding: 8px; background-color: #F1F5F9;">Marks Obtained</th>
<th style="padding: 8px; background-color: #F1F5F9;">Grade</th>
</tr>
Β 
<!-- Row 2: Mathematics -->
<tr>
<td style="padding: 8px;">Mathematics</td>
<td style="padding: 8px;">100</td>
<td style="padding: 8px;">98</td>
<td style="padding: 8px;">A1</td>
</tr>
Β 
<!-- Row 3: Science -->
<tr>
<td style="padding: 8px;">Science</td>
<td style="padding: 8px;">100</td>
<td style="padding: 8px;">94</td>
<td style="padding: 8px;">A1</td>
</tr>
Β 
<!-- Row 4: English -->
<tr>
<td style="padding: 8px;">English</td>
<td style="padding: 8px;">100</td>
<td style="padding: 8px;">89</td>
<td style="padding: 8px;">A2</td>
</tr>
</table>

Notice the 3 Golden Rules Here:

  1. 1
    Every row starts with <tr> and ends with </tr>.
  2. 2
    The top row uses <th> tags because they are column headings.
  3. 3
    The data rows underneath use <td> tags for regular numbers and text.

Why Did We Add border-collapse: collapse? πŸͺ„

By default, old HTML tables put a tiny annoying double gap between cell borders (it looks like two parallel lines around every box).

To fix this and make your table look clean and modern like a real school marksheet, developers always add this magic CSS line:

CSS
table {
border-collapse: collapse; /* Collapses double borders into single neat lines! */
}

Semantic Structure Tags: <thead>, <tbody>, <tfoot> & <caption> πŸ›οΈ

When professional developers and school examination portals build large tables, they divide them into three logical sectionsβ€”just like a human body has a Head, a Body, and Feet:

Output
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ <caption> Table Title / Description </caption>β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ <thead> Column Headings (th) </thead>β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ <tbody> Actual Data Rows (td) </tbody>β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ <tfoot> Totals, Averages, Summary </tfoot>β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

1. <caption> (The Table Title)

A caption is the official title of the table. It must be placed immediately after the opening <table> tag. Screen readers for visually impaired students read the caption first so they know what the table is about!

2. <thead> (Table Head)

Encloses the header row containing your <th> elements. If your table is printed on paper across 5 pages, modern browsers repeat <thead> at the top of every printed page!

3. <tbody> (Table Body)

Wraps all the main rows of your data.

Wraps the bottom row showing totals, averages, overall percentages, or remarks.


Complete Semantic Table Example:

HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>School Fee Structure</title>
<style>
table {
width: 100%;
border-collapse: collapse;
font-family: Arial, sans-serif;
}
caption {
font-size: 1.2rem;
font-weight: bold;
margin-bottom: 10px;
color: #0A2540;
}
th, td {
border: 1px solid #CBD5E1;
padding: 10px;
text-align: left;
}
th {
background-color: #0A2540;
color: #FFFFFF;
}
tfoot {
background-color: #F8FAFC;
font-weight: bold;
}
</style>
</head>
<body>
Β 
<table>
<!-- Caption: Table Title -->
<caption>MSK School Annual Fee Breakdown (Class 10th)</caption>
Β 
<!-- Table Header -->
<thead>
<tr>
<th>Fee Category</th>
<th>Frequency</th>
<th>Amount (β‚Ή)</th>
</tr>
</thead>
Β 
<!-- Table Body -->
<tbody>
<tr>
<td>Tuition & Lab Fee</td>
<td>Quarterly</td>
<td>β‚Ή4,500</td>
</tr>
<tr>
<td>Computer Science & Robotics Lab</td>
<td>Annual</td>
<td>β‚Ή1,500</td>
</tr>
<tr>
<td>Library & Sports Development</td>
<td>Annual</td>
<td>β‚Ή1,000</td>
</tr>
</tbody>
Β 
<!-- Table Footer -->
<tfoot>
<tr>
<td>Total First Quarter Payable</td>
<td>One Time</td>
<td>β‚Ή7,000</td>
</tr>
</tfoot>
</table>
Β 
</body>
</html>

An Important Warning: Tables Are NOT for Webpage Layouts! ⚠️

In the late 1990s (when web design was brand new), developers used <table> tags to arrange headers, sidebars, and footers on websites.

🚫 Never use tables for page layouts today! - Tables are heavy and slow to load for layout purposes. - They break on mobile phones and small smartphone screens. - Page layouts must always be built using modern CSS tools like Flexbox and CSS Grid. - Only use <table> when you have real tabular data (numbers, schedules, timetables, comparison lists)!

Common Beginner Mistakes (And How to Avoid Them) ⚠️

1. ⚠️ Writing <td> directly inside <table> without a <tr>

You cannot drop data cells floating inside a table! They must always live inside a row (<tr>).

HTML
<!-- ❌ WRONG: Missing <tr> wrapper -->
<table>
<td>Item 1</td>
<td>Item 2</td>
</table>
Β 
<!-- βœ… CORRECT: Wrapped in <tr> -->
<table>
<tr>
<td>Item 1</td>
<td>Item 2</td>
</tr>
</table>

2. ⚠️ Confusing <th> with <thead>

  • <th> is a single cell tag (replaces <td> for headers).
  • <thead> is a section wrapper tag that wraps an entire header <tr>.

3. ⚠️ Unbalanced Columns

If your top header row has 4 columns (<th>), every data row underneath should also have 4 cells (<td>). If one row has only 2 cells, your table will look broken and misaligned!


Quick Summary

  • HTML tables are created using the <table> container tag.
  • Tables are organized in horizontal rows using <tr> (Table Row) tags.
  • Use <th> (Table Header) for column titles (automatically bold and centered).
  • Use <td> (Table Data) for regular data cells (regular left-aligned text).
  • Add border-collapse: collapse; in CSS to merge double borders into neat single lines.
  • Semantic tables use <caption> for the title, <thead> for headings, <tbody> for data rows, and <tfoot> for summary/totals.
  • Tables should only be used for tabular data, never for webpage layouts.

Practice Quiz

Test your understanding with these multiple-choice questions:

1. Which tag creates a new horizontal row inside an HTML table?

A. <row> B. <td> C. <tr> D. <th> Answer: C Explanation: <tr> stands for Table Row and defines a single horizontal line of cells.


2. What is the difference between <th> and <td>?

A. <th> is for images, <td> is for text B. <th> text is bold and centered by default, while <td> text is normal and left-aligned C. <th> can only be used once per table D. <td> creates rows, <th> creates columns Answer: B Explanation: <th> (Table Header) renders bold, centered text for column titles, whereas <td> (Table Data) renders regular text for data cells.


3. Which tag should be used as the very first child of a <table> to provide an accessible title?

A. <title> B. <header> C. <caption> D. <heading> Answer: C Explanation: The <caption> tag defines the official title or summary of a table and is announced first by screen readers.


4. Which CSS property eliminates the gap between table cell borders to create single clean lines?

A. border-style: single; B. border-collapse: collapse; C. border-spacing: none; D. border-merge: true; Answer: B Explanation: border-collapse: collapse; merges adjacent double cell borders into crisp, single borderlines.


5. Why should you NOT use <table> tags to design a website's overall page layout?

A. Tables are prohibited by law B. Browsers will refuse to open the page C. Tables are not responsive for mobile devices and harm web accessibility D. Tables turn all text blue Answer: C Explanation: Using tables for page layouts damages responsiveness on smartphones and makes navigation confusing for screen reader users. Page layouts should be designed with CSS Flexbox or Grid.


Hands-on Practice Challenge 🎯

Open VS Code and create a file named my-report-card.html.

Your Challenge:

Create your own professional School Annual Examination Marksheet:

  1. 1
    Add a <caption> with: "Class 10 Annual Examination Marksheet 2026".
  2. 2
    Wrap column headings inside <thead> with columns: Subject, Max Marks, Marks Scored, and Result.
  3. 3
    Add 4 subjects in <tbody> (e.g. Maths, Science, English, Social Science).
  4. 4
    Add a <tfoot> row calculating the Total Marks (e.g. 400) and Grand Total Scored.
  5. 5
    Style it with border-collapse: collapse; and add nice padding so the marksheet looks crisp and clean!
  6. 6
    Open your marksheet in your browser and check your score!

Next Up: In Topic 8.2, we will explore Colspan & Rowspan &mdash; how to merge cells horizontally and vertically like a spreadsheet master!

Next Lesson

Colspan & Rowspan

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Description ListsColspan & Rowspan

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext