- Home
- /
- Tutorials
- /
- HTML Tutorial
- /
- HTML <td> Tag
HTML Tags
HTML <td> Tag
<td>is one data cell in a table row. Every value in a table lives in a<td>, except the labels, which live in<th>. Getting that distinction right is most of what there is to know; the rest is spanning cells across columns and rows.
Try it
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>td</title>
<style>
table { border-collapse: collapse; }
th, td { border: 1px solid #cbd5e1; padding: 8px 12px; text-align: left; }
th { background: #f1f5f9; }
caption { caption-side: bottom; color: #64748b; font-size: 14px; padding-top: 8px; }
</style>
</head>
<body>
<table>
<caption>Monthly plan comparison</caption>
<thead>
<tr><th scope="col">Plan</th><th scope="col">Price</th><th scope="col">Exercises</th></tr>
</thead>
<tbody>
<tr><th scope="row">Free</th><td>₹0</td><td>2 per lesson</td></tr>
<tr><th scope="row">Practice</th><td>₹99 / month</td><td>All</td></tr>
</tbody>
</table>
</body>
</html>The plan names are <th>, not <td>: they label their row. The prices and counts are the data. A screen reader on the "₹99 / month" cell says "Practice, Price, ₹99 / month": it reads the row header and column header first, because the <th>s and scope told it which is which.
Structure
<td> must be inside a <tr>, which must be inside a <table> (or its <thead>, <tbody>, <tfoot>). Each <tr> normally has the same number of cells; a cell can hold text, links, images, lists, anything, including another table, though you will regret that.
Attributes
| Attribute | What it does |
|---|---|
| colspan="n" | The cell is n columns wide. The row then needs n − 1 fewer cells. |
| rowspan="n" | The cell is n rows tall. The next n − 1 rows each need one fewer cell in that position. |
| headers="id id" | Space-separated ids of the s that label this cell. Only needed for complex tables where scope cannot express the relationship. |
| align, valign, bgcolor, width, height, nowrap | Obsolete. Use CSS: text-align, vertical-align, background, width, white-space. |
A realistic example: a timetable with spans
Spans are where tables get fiddly. This timetable has a two-hour lab (rowspan) and a lunch break across every day (colspan).
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Timetable</title>
<style>
table { border-collapse: collapse; font-family: system-ui, sans-serif; }
th, td { border: 1px solid #cbd5e1; padding: 8px 14px; text-align: center; }
th { background: #f1f5f9; }
.break { background: #fef9c3; }
</style>
</head>
<body>
<table>
<tr><th></th><th scope="col">Mon</th><th scope="col">Tue</th><th scope="col">Wed</th></tr>
<tr><th scope="row">9:00</th><td>HTML</td><td rowspan="2">CSS lab</td><td>JavaScript</td></tr>
<tr><th scope="row">10:00</th><td>CSS</td> <td>DSA</td></tr>
<tr><th scope="row">11:00</th><td colspan="3" class="break">Break</td></tr>
<tr><th scope="row">12:00</th><td>Project</td><td>Project</td><td>Review</td></tr>
</table>
</body>
</html>Look at the 10:00 row: it has only two <td>s, because the "CSS lab" cell from the row above is occupying the Tuesday slot. Miscounting that is the most common table bug there is.
Common mistakes
1. Using td for headers
<td><strong>Name</strong></td> looks like a header and is not one. Screen readers cannot associate it with the column, and you cannot select all headers in CSS. Use <th> with scope="col" or scope="row". Bold is the default anyway.
2. Miscounting after a span
A cell with colspan="2" takes two slots, so its row needs one cell fewer. A rowspan="2" takes a slot in the next row too. When a table renders with a cell hanging off the right edge or a gap in the middle, count slots per row: they must all add up to the same number.
3. Tables for layout
Before CSS Grid, pages were built as one giant table. Do not: it reads as a table to assistive technology ("table with 3 rows and 4 columns…"), it cannot reflow for phones, and every modern layout tool is better. If the content is not tabular data, values with row and column labels, it is not a table. Grid and Flexbox are what you want.
4. Empty cells
<td></td> is valid, but in older browsers and with border-collapse: separate an empty cell may lose its border (see empty-cells). If the cell is genuinely empty, leave it empty and set empty-cells: show; do not fill it with a non-breaking space (the nbsp entity), which screen readers may announce as "blank" or skip unpredictably.
5. Putting td straight inside table
<table><td>…</td></table> without a <tr> is invalid. Browsers repair it by inventing a row, which can produce a different structure from the one you intended, and different repairs in different browsers.
6. Sizing cells with attributes
width, height, align and bgcolor on <td> are obsolete. They still work, but CSS is more capable: th:nth-child(2) { width: 30% } sizes a whole column, which the attribute cannot do cleanly.
Browser support
Universal, including colspan, rowspan and headers.
Related chapters
<th>: header cells andscope<tr>: rows<table>: the element and its sections- Styling table borders
- CSS for tables
HTML <td> Tag exercises
Add data cells
Make a table with one row of two data cells: Delhi and Hot.
Result
