Tutorials HTML HTML Tables
Ch 9 / 40
Chapter 9 Beginner 9 min read HTML

HTML Tables — Structure, Spanning & Styling

Tables organise data into rows and columns — course schedules, pricing grids, comparison charts, and more. In this chapter you'll master every table tag, learn how to merge cells with colspan and rowspan, group rows semantically, and make tables fully responsive with CSS.

What you'll learn
Every HTML table tag — <table>, <tr>, <th>, <td>
Semantic grouping with <thead>, <tbody>, <tfoot>
Adding a table title with <caption>
Merging columns with colspan and rows with rowspan
Styling tables with CSS — borders, striped rows, hover
Making tables responsive on mobile

What is an HTML Table?

An HTML table displays data in a grid of rows and columns. Every table is built from three core elements: <table> (the container), <tr> (table row), and either <th> (table header cell) or <td> (table data cell). Here is a fully labelled example of a real table rendered live:

📋 K2infocom — Course Schedule (Live Table Preview)
<th> Course Name <th> Duration <th> Level <th> Fee
Web Development With AI 40 hrs Beginner–Advanced ₹8,999
Python Masterclass 30 hrs Beginner–Intermediate ₹6,999
Data Analysis 25 hrs Intermediate ₹7,499
<tfoot> — Total courses available at K2infocom 15+
Use Tables for Data Only
HTML tables are for tabular data — data that naturally belongs in rows and columns. Never use tables for page layout. That was a 1990s practice. Use CSS Flexbox or Grid for layout instead.

All HTML Table Tags Explained

Here is a complete reference of every element used to build an HTML table:

<table> Table Container
The outer wrapper for the entire table. All other table elements must be nested inside it. Browsers render it as a block-level element.
<tr> Table Row
Defines a horizontal row inside the table. Must contain one or more <th> or <td> cells. Each <tr> represents one row of data.
<th> Table Header Cell
A header cell — rendered bold and centred by default. Use for column or row headings. The scope attribute (col or row) improves accessibility for screen readers.
<td> Table Data Cell
A data cell — the standard cell for body content. Rendered left-aligned with normal font weight by default. Supports colspan and rowspan for spanning.
<thead> Table Head Group
Semantically groups the header rows of the table. Helps browsers, screen readers, and print engines identify the column labels. When printed, <thead> repeats on every page.
<tbody> Table Body Group
Groups the body rows — all the actual data rows. You can have multiple <tbody> elements in one table to visually group related rows of data.
<tfoot> Table Foot Group
Groups the footer rows — typically totals, summaries, or notes. Rendered at the bottom of the table even if placed before <tbody> in the HTML source.
<caption> Table Caption
Provides a visible title for the table — rendered above it by default. Important for accessibility: screen readers read it first. Like an image's alt, it describes what the table contains.

Basic Table Syntax

Here is the minimal, correct structure for an HTML table with <thead>, <tbody>, and <tfoot>:

HTML — Complete Table Structure
<table>

  <!-- Optional title for the table -->
  <caption>K2infocom Course Overview</caption>

  <!-- Header rows -->
  <thead>
    <tr>
      <th scope="col">Course</th>
      <th scope="col">Duration</th>
      <th scope="col">Fee</th>
    </tr>
  </thead>

  <!-- Data rows -->
  <tbody>
    <tr>
      <td>Web Development With AI</td>
      <td>40 hrs</td>
      <td>₹8,999</td>
    </tr>
    <tr>
      <td>Python Masterclass</td>
      <td>30 hrs</td>
      <td>₹6,999</td>
    </tr>
  </tbody>

  <!-- Footer / summary row -->
  <tfoot>
    <tr>
      <td colspan="2">Total Courses Available</td>
      <td>15+</td>
    </tr>
  </tfoot>

</table>

Merging Cells — colspan & rowspan

Two attributes let you merge cells across multiple columns or rows, creating complex layouts:

colspan="N"
Makes a cell span N columns horizontally. The cell takes up the space of N cells in the same row. Remove the equivalent number of <td> cells from that row to keep the grid valid.
rowspan="N"
Makes a cell span N rows vertically. The cell stretches downward across N rows. Remove the equivalent <td> cells from the rows below to keep the grid valid.
colspan — spans columns
colspan="3" — spans 3 columns
Col 1 Col 2 Col 3
Data A Data B Data C
rowspan — spans rows
rowspan="3"
spans 3 rows
Row 1 Data
Row 2 Data
Row 3 Data
HTML — colspan and rowspan in action
<table>
  <thead>
    <tr>
      <!-- Spans across 2 columns -->
      <th colspan="2">Student Info</th>
      <th>Score</th>
    </tr>
    <tr>
      <th>Name</th>
      <th>City</th>
      <th>Marks</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <!-- rowspan spans this cell across 2 rows -->
      <td rowspan="2">Rahul Shah</td>
      <td>Vadodara</td>
      <td>92</td>
    </tr>
    <tr>
      <!-- No first cell needed — covered by rowspan above -->
      <td>Surat</td>
      <td>88</td>
    </tr>
  </tbody>
</table>
Keep the Grid Valid
When using colspan="3", remove 2 cells from that row. When using rowspan="2", remove 1 cell from the row below in the same column. The total number of cells in every row must always equal the number of columns — otherwise the table renders broken.

Table Attribute Reference

Here is a complete reference of the most important HTML table attributes:

Attribute Applied To Purpose
colspan <td>, <th> Merges the cell across the specified number of columns horizontally
rowspan <td>, <th> Merges the cell across the specified number of rows vertically
scope <th> Declares whether the header applies to a col or a row — critical for screen reader accessibility
headers <td> Associates a data cell with one or more header cells by their id — for complex table accessibility
caption-side CSS on <caption> CSS property — top (default) or bottom — positions the caption above or below the table

Styling Tables with CSS

Plain HTML tables look unstyled by default. Here is the CSS used to create clean, professional-looking tables — and the result rendered live below:

CSS — Professional Table Styles
/* Responsive wrapper — allows horizontal scroll on mobile */
.table-wrap {
  width: 100%;
  overflow-x: auto;
  border-radius: 12px;
  border: 1px solid #e5e7eb;
}

/* Base table reset */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 15px;
  min-width: 480px;
}

/* Caption — visible title */
caption {
  padding: 12px 16px;
  font-weight: 700;
  text-align: left;
  background: #f9fafb;
  border-bottom: 1px solid #e5e7eb;
}

/* Header cells */
thead th {
  background: #f3f4f6;
  padding: 12px 16px;
  font-weight: 700;
  text-align: left;
  border-bottom: 2px solid #e44d26;
  white-space: nowrap;
}

/* Data cells */
tbody td {
  padding: 12px 16px;
  border-bottom: 1px solid #e5e7eb;
  vertical-align: middle;
}

/* Striped rows — alternating background */
tbody tr:nth-child(even) {
  background: #f9fafb;
}

/* Hover highlight */
tbody tr:hover {
  background: rgba(228, 77, 38, 0.04);
}

/* Footer row */
tfoot td {
  padding: 10px 16px;
  font-weight: 700;
  background: #f3f4f6;
  border-top: 2px solid #e44d26;
}

Here is that CSS applied to a real table — rendered live:

🎓 K2infocom — Web Development Batch Schedule
Course Batch Start Duration Mode Seats Status
Web Dev With AI 1 Jun 2025 40 hrs Online + Offline 30 Open
Python Masterclass 15 Jun 2025 30 hrs Online 40 Open
Data Analysis 1 Jul 2025 25 hrs Online 35 Full
MERN Stack 10 Jul 2025 50 hrs Offline 25 Open
Total seats across all open batches 95 —

Making Tables Responsive

Wide tables overflow on small screens. The standard solution is to wrap the table in a <div> with overflow-x: auto — this adds a horizontal scroll bar on mobile without breaking the table structure.

HTML + CSS — Responsive Table Pattern
<!-- Wrap your table in a scrollable div -->
<div class="table-wrap" role="region" aria-label="Course schedule table">
  <table>
    ...your table content...
  </table>
</div>
CSS — Responsive wrapper
.table-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch; /* smooth scroll on iOS */
  border-radius: 12px;
  border: 1px solid var(--border);
}

table {
  min-width: 480px; /* prevents columns from collapsing too narrow */
  width: 100%;
  border-collapse: collapse;
}
Accessibility
Add role="region" and aria-label="Table description" to the wrapper <div>. This tells screen readers that the scrollable region contains important content, making the horizontal scroll accessible to keyboard and screen reader users.

Common Mistakes — Do's & Don'ts

These are the most frequent table mistakes beginners make:

Do — Correct
<!-- Use scope for accessibility -->
<thead>
  <tr>
    <th scope="col">Name</th>
    <th scope="col">Score</th>
  </tr>
</thead>

<!-- colspan: remove matching cells -->
<tr>
  <td colspan="2">Total</td>
  <td>100</td>
</tr>
Don't — Wrong
<!-- Tables for layout — never do this -->
<table>
  <tr>
    <td>Sidebar</td>
    <td>Main Content</td>
  </tr>
</table>

<!-- colspan without removing cells (broken) -->
<tr>
  <td colspan="2">Total</td>
  <td>Extra</td>
  <td>100</td>
</tr>

Try It Yourself

Edit the table code below and click Open in Editor to see your table live in full screen.

Live Editor
✎ Editor
👁 Preview
Try This
Change colspan="2" on the first header to colspan="3" — notice how it spans further. Then try adding a rowspan="2" on a <td> to see vertical merging in action.
Free Resource
HTML5 Cheat Sheet — All Tags & Attributes
One printable PDF covering every HTML table tag, colspan, rowspan, scope, and responsive table patterns.
Download PDF
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React, and Node.js in one industry-ready curriculum with real projects and placement support.
120+
Chapters
40 hrs
Content
10
Projects
4.9 ⭐
Rating
HTML5 CSS3 JavaScript React.js Node.js MongoDB AI Tools

Quick Check — Chapter 9

Expand each question to check your understanding before moving on to the multiple choice quiz below.

Questions & Answers
<th> is a table header cell — it is semantically marked as a label for a column or row. Browsers render it bold and centre-aligned by default, and screen readers announce it as a header. <td> is a table data cell — it holds the actual content of the table. It is rendered left-aligned with normal font weight. Always use <th> for column and row headings (with scope="col" or scope="row") and <td> for all other data.
These three elements semantically group rows within a table. <thead> groups the header rows, <tbody> groups the data rows, and <tfoot> groups the footer rows (totals, summaries). They are not strictly required — a table can work with just <tr> and <td> — but using them is strongly recommended for accessibility, CSS styling flexibility, and because <thead> repeats on every printed page.
colspan="N" on a <td> or <th> makes that cell span N columns horizontally. To use it correctly, you must remove the equivalent number of cells from that row — otherwise the table has more cells than columns and renders broken. For example, colspan="3" in a 4-column table means that row only needs 2 more cells (1 spanning + 2 regular = 4 total column width). Always count that every row's total column span equals the number of columns.
Using tables for layout was common in the 1990s when CSS layout tools did not exist — but it is now considered very bad practice for several reasons. Tables are semantically wrong for layout — they tell browsers and screen readers "this is tabular data" which is false for a navigation sidebar or two-column page. They are inflexible — very hard to make responsive. They are slower — browsers must load the entire table before rendering any of it. Modern alternatives — CSS Flexbox and CSS Grid — handle all layout needs with far less code, full responsiveness, and correct semantics.
The standard technique is to wrap the <table> inside a <div> with CSS overflow-x: auto. This keeps the table at its natural width but adds a horizontal scrollbar on narrow screens, allowing users to scroll to see all columns without the table being squished or broken. Set a min-width on the table itself to prevent columns from collapsing too narrow. Also add role="region" and aria-label on the wrapper div so screen reader users know the scrollable region contains data.
The scope attribute tells screen readers which cells a header applies to. scope="col" means the header describes all cells in its column below it. scope="row" means the header describes all cells in its row to the right. Without scope, screen readers may struggle to associate data cells with their correct headers — especially in tables with merged cells or complex structures. Adding scope to every <th> is a simple one-attribute fix that significantly improves table accessibility.
Multiple Choice Questions
MCQ 1 of 4
Which tag defines a header cell in an HTML table — rendered bold and centred by default?
MCQ 2 of 4
Which attribute makes a table cell span across 3 columns horizontally?
MCQ 3 of 4
Which element provides a visible title or description for an HTML table?
MCQ 4 of 4
What CSS property on a wrapper <div> makes a wide table scrollable on mobile?
Chapter Summary
  • HTML tables use <table>, <tr>, <th>, and <td> as their core elements. Use tables only for tabular data — never for page layout.
  • <th> is a header cell (bold, centred, semantic). <td> is a data cell. Always add scope="col" or scope="row" to <th> for accessibility.
  • Group rows semantically: <thead> for headers, <tbody> for data, <tfoot> for summaries. Add <caption> for a visible table title.
  • colspan="N" merges N columns horizontally. rowspan="N" merges N rows vertically. Always remove the equivalent cells to keep the grid valid.
  • Style tables with border-collapse: collapse, zebra-striped rows (:nth-child(even)), and hover highlight for clean professional tables.
  • Make tables responsive by wrapping in a <div> with overflow-x: auto and setting a min-width on the table itself.
~9 min read
Beginner level
Chapter 9 of 40

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
If this tutorial helped you, a 5-star Google review means a lot. It helps other learners discover our free content and motivates us to keep improving.
500+ Reviews (Target)
Write a Google Review
Join WhatsApp Channel