HTML <table> Tag โ Data Tables
๐ Introduction
The <table> tag is used to display data in rows and columns.
It is commonly used for:
- Reports
- Schedules
- Pricing tables
- Data comparison
- Admin dashboards
๐งฑ Table Structure
A basic table uses these tags:
<table>โ main container<tr>โ table row<th>โ table header<td>โ table data
๐ก Basic Syntax
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</table>
๐ก Example
<!DOCTYPE html>
<html>
<head>
<title>Table Example</title>
</head>
<body>
<h2>Students Table</h2>
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
<th>Country</th>
</tr>
<tr>
<td>yourname</td>
<td>20</td>
<td>Algeria</td>
</tr>
<tr>
<td>Sara</td>
<td>22</td>
<td>Morocco</td>
</tr>
</table>
</body>
</html>
๐ฅ๏ธ Output Explanation
<th>= column headers (bold by default)<td>= actual data<tr>= each row
๐ฏ Important Structure Rule
๐ Tables always follow this structure:
table
โโโ tr (row)
โ โโโ th or td
โ โโโ td
โ๏ธ Important Tags
1. <tr> (Table Row)
<tr>
<td>Row data</td>
</tr>
โ Creates a horizontal row
2. <th> (Header Cell)
<th>Name</th>
โ Used for column titles โ Usually bold and centered
3. <td> (Data Cell)
<td>yourname</td>
โ Used for normal data
๐จ Styling Example
<table style="width:100%; border-collapse: collapse;">
<tr>
<th style="border:1px solid black;">Name</th>
<th style="border:1px solid black;">Age</th>
</tr>
</table>
๐ซ Common Mistakes
- Forgetting
<tr>โ - Using
<td>outside table โ - Not structuring rows properly โ
๐งช Mini Exercise
Create a table with:
3 columns:
- Name
- Age
- Job
At least 3 rows of data
Add borders to the table
Make headers bold