Tables in HTML
HTML में Tables
Table Anatomy — rows first, then cells
<table>
<tr> ← tr = table ROW (horizontal line)
<th>Roll No</th> ← th = HEADER cell (bold + centered)
<th>Name</th>
<th>Marks</th>
</tr>
<tr>
<td>101</td> ← td = DATA cell
<td>Aman</td>
<td>92</td>
</tr>
<tr>
<td>102</td><td>Priya</td><td>88</td>
</tr>
</table>
<tr> is one horizontal line; inside it, each <td>/<th> is one box. Column count = number of cells you put in each row — keep it equal in every row or the table looks broken.Note: modern tables have no visible border by default — borders come from CSS (border: 1px solid). The old border="1" attribute still works for quick tests but is not used in production.
th vs td — the exam favorite
| th | td | |
|---|---|---|
| Stands for | Table Header | Table Data |
| Look | Bold + centered (default) | Normal + left |
| Meaning | Label of a column/row | The actual value |
| Screen readers | Announce it with each related cell ("Marks: 92") | Just the value |
th is not "bold td" — it semantically LABELS the column, and screen readers pair it with every data cell below. Using td with manual bold loses that meaning.
colspan and rowspan — merging cells, visually
<table>
<tr><th colspan="3">Class 10-A Result</th></tr> ← 1 cell stretches across 3 columns
<tr><th>Roll</th><th>Name</th><th>Marks</th></tr>
<tr><td rowspan="2">House A</td><td>Aman</td><td>92</td></tr>
<tr><td>Priya</td><td>88</td></tr> ← only 2 cells! House A occupies the first
</table>
caption, thead, tbody, tfoot — the professional shape
<table>
<caption>Term 1 Result – Class 10-A</caption>
<thead>
<tr><th>Name</th><th>Marks</th></tr>
</thead>
<tbody>
<tr><td>Aman</td><td>92</td></tr>
<tr><td>Priya</td><td>88</td></tr>
</tbody>
<tfoot>
<tr><td>Average</td><td>90</td></tr>
</tfoot>
</table>
Why bother? caption gives the table an official title (read by screen readers, good for SEO). thead/tbody/tfoot let CSS style each zone separately, let long tables print the header on every page, and enable sticky headers while scrolling. Small effort now, big powers later.
When NOT to Use Tables
Table की बनावट — पहले rows, फिर cells
<table>
<tr> ← tr = table ROW (aadi line)
<th>Roll No</th> ← th = HEADER cell (bold + center)
<th>Name</th>
<th>Marks</th>
</tr>
<tr>
<td>101</td> ← td = DATA cell
<td>Aman</td>
<td>92</td>
</tr>
<tr>
<td>102</td><td>Priya</td><td>88</td>
</tr>
</table>
<tr> एक आड़ी line; उसके अंदर हर <td>/<th> एक खाना. Columns की गिनती = हर row में डाले cells — हर row में बराबर रखिए वरना table टूटी दिखती है.Note: modern tables में default border नहीं दिखता — borders CSS से आते हैं (border: 1px solid). पुराना border="1" attribute quick tests में अब भी चलता है पर production में use नहीं होता.
th vs td — exam favorite
| th | td | |
|---|---|---|
| मतलब | Table Header | Table Data |
| दिखावट | Bold + center (default) | Normal + left |
| अर्थ | Column/row का label | असली value |
| Screen readers | हर related cell के साथ बोलते हैं ("Marks: 92") | सिर्फ value |
th "bold वाला td" नहीं है — वह column को semantically LABEL करता है, और screen readers उसे नीचे की हर data cell से जोड़कर बोलते हैं. td पर manual bold से यह meaning खो जाती है.
colspan और rowspan — cells merge करना, visual के साथ
<table>
<tr><th colspan="3">Class 10-A Result</th></tr> ← 1 cell 3 columns तक फैला
<tr><th>Roll</th><th>Name</th><th>Marks</th></tr>
<tr><td rowspan="2">House A</td><td>Aman</td><td>92</td></tr>
<tr><td>Priya</td><td>88</td></tr> ← sirf 2 cells! पहली जगह House A ने घेरी है
</table>
caption, thead, tbody, tfoot — professional shape
<table>
<caption>Term 1 Result – Class 10-A</caption>
<thead>
<tr><th>Name</th><th>Marks</th></tr>
</thead>
<tbody>
<tr><td>Aman</td><td>92</td></tr>
<tr><td>Priya</td><td>88</td></tr>
</tbody>
<tfoot>
<tr><td>Average</td><td>90</td></tr>
</tfoot>
</table>
क्यों? caption table को official title देता है (screen readers पढ़ते हैं, SEO को अच्छा). thead/tbody/tfoot से CSS हर zone को अलग style कर सकती है, लंबी tables print में हर page पर header दोहरा सकती हैं, और scroll पर sticky header बन सकता है. अभी छोटी मेहनत, बाद में बड़ी powers.
Tables कब NAHI use करें
💻 Live Code Editor
इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.console.log देखने के लिए F12 दबाइए.