4 min read · Free · No login needed
Tables show data in rows and columns — timetables, price lists, exam schemes, comparison charts. A table is built from four tags: <table> for the whole table, <tr> for each row, <th> for a header cell, and <td> for a normal data cell.
Every row is a <tr>. Inside a row, you put cells. The first row usually uses <th> (header) cells, which browsers show in bold and centred. All other rows use <td> cells. A table with 3 rows and 3 columns has 3 <tr> elements, each holding 3 cells.
<caption> — a title for the table, placed right after <table>.colspan="2" — a cell that stretches across 2 columns.rowspan="2" — a cell that stretches across 2 rows.In the old days, websites were laid out with tables. Never do that today — use CSS for layout. Tables are only for real tabular data. A table used for layout is hard to make responsive, and screen readers read it out in a confusing order.
A table is a grid. <table> is the whole grid. <tr> is one row of the grid. <th> is a header cell — the label at the top of a column. <td> is a normal data cell. For longer tables you can also group rows with <thead> (the header rows), <tbody> (the data rows), and <tfoot> (the footer rows).
| Tag / Attribute | Value | Description |
|---|---|---|
<caption> | text | The table's title, placed right after <table>. |
<thead> | — | Groups the header rows of the table. |
<tbody> | — | Groups the main data rows. |
colspan | a number | Makes a cell stretch across that many columns. |
rowspan | a number | Makes a cell stretch across that many rows. |
scope | col or row | Tells screen readers whether a <th> labels a column or a row. |
Here is a header cell that tells a screen reader it labels a whole column:
<tr><th scope="col">Subject</th><th scope="col">Marks</th></tr>
colspan="2" counts as 2. If the numbers do not match, the browser draws the table crooked and it is hard to see why.scope="col". It costs nothing, and it lets a blind user hear "Subject" announced before each value in that column — the table suddenly makes sense to them.<td> for header cells — headers must be <th>, so they look bold and read correctly.<tr> — cells must always sit inside a row, never directly inside <table>.<style> — draws a 1px border around the table and every cell, and adds padding so the text does not touch the edges. Without it, the table would be invisible lines of plain text.<table> — starts the table. Everything about the table sits between this tag and </table>.<caption>Weekly Class Schedule</caption> — the table's title, shown above it. It must come right after <table>.<tr><th>Day</th><th>Subject</th><th>Time</th></tr> — the header row. The <th> cells are bold, centred headers that label each column.<tr><td>Monday</td><td>HTML Basics</td><td>10:00 AM</td></tr> — a data row. The <td> cells hold the actual values, one per column.<!DOCTYPE html>
<html>
<head>
<title>Tables</title>
<style>
table, th, td { border: 1px solid #999; border-collapse: collapse; }
th, td { padding: 8px 12px; }
</style>
</head>
<body>
<table>
<caption>Weekly Class Schedule</caption>
<tr><th>Day</th><th>Subject</th><th>Time</th></tr>
<tr><td>Monday</td><td>HTML Basics</td><td>10:00 AM</td></tr>
<tr><td>Tuesday</td><td>CSS Styling</td><td>10:00 AM</td></tr>
</table>
</body>
</html>
Change the code below and press Run to see the result instantly. No file to save, no login needed.
The questions learners ask most about this lesson — read the question, then open it to see the answer.
table for the whole table, tr for each row, th for a header cell, and td for a normal data cell.
colspan stretches a cell across columns, and rowspan stretches it across rows.
No, tables are only for real tabular data. Use CSS for layout, since layout tables are hard to make responsive.
AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.
Finish all 35 lessons, pass the test (80%), pay ₹10 and get your certificate.
Take the testPrefer watching? Video courses teach the same topics step by step.
Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.
Browse all coursesAsk about courses, website & more
Request human callback:
Please log in to download. You will stay on this page after logging in!
New here? Register now →