š A table is used in HTML to display data in rows and columns, just like in MS Excel or a notebook. It helps organize information in a structured way.
The basic tag for a table is <table>.
An HTML table has several important parts:
<table> ā defines the table.<tr> (Table Row) ā defines a row in the table.<th> (Table Header) ā defines a heading cell (bold and centered by default).<td> (Table Data) ā defines a normal cell (data cell).<caption> ā gives a title/caption for the table.<thead> ā groups the header part of the table.<tbody> ā groups the main body of the table.<tfoot> ā groups the footer part of the table.border, cellpadding, cellspacing, colspan, rowspan are used to style and merge cells.<table border="1">
<tr>
<th>Roll No</th>
<th>Name</th>
<th>Marks</th>
</tr>
<tr>
<td>1</td>
<td>Ravi</td>
<td>85</td>
</tr>
<tr>
<td>2</td>
<td>Sita</td>
<td>92</td>
</tr>
</table>
š Here:
<tr> = rows<th> = headings<td> = data<table border="1" cellpadding="5" cellspacing="0">
<caption>
<b>Class 10 Exam Results</b>
</caption>
<thead>
<tr>
<th>Roll No</th>
<th>Name</th>
<th>Marks</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Ravi</td>
<td>85</td>
</tr>
<tr>
<td>2</td>
<td>Sita</td>
<td>92</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="2">Total Students</td>
<td>2</td>
</tr>
</tfoot>
</table>
š Explanation:
<caption> = Table title.<thead> = Heading row (Roll No, Name, Marks).<tbody> = Main data (Ravi, Sita).<tfoot> = Footer (summary row).colspan="2" merges two cells into one.<table border="1" cellpadding="5">
<tr>
<th rowspan="2">Roll No</th>
<th colspan="2">Student Info</th>
</tr>
<tr>
<th>Name</th>
<th>Marks</th>
</tr>
<tr>
<td>1</td>
<td>Ravi</td>
<td>85</td>
</tr>
<tr>
<td>2</td>
<td>Sita</td>
<td>92</td>
</tr>
</table>
š Explanation:
rowspan="2" ā merges 2 rows vertically.colspan="2" ā merges 2 columns horizontally.| Tag | Meaning |
|---|---|
<table> | Defines a table |
<tr> | Table row |
<th> | Table header (bold + centered) |
<td> | Table data cell |
<caption> | Title of the table |
<thead> | Header section |
<tbody> | Body section |
<tfoot> | Footer section |
rowspan | Merge cells vertically |
colspan | Merge cells horizontally |
š So, tables are like grids in HTML ā they give us rows and columns to neatly arrange data.