HTML tables are used to display data in a structured, grid-like format with rows and columns. The <table> tag creates the table, with <tr> for rows, <td> for data cells, and <th> for header cells. This tutorial explains how to create tables with examples and previews, making it easy for beginners to present data effectively.
Tables organize data into rows and columns, perfect for displaying schedules, comparison charts, or datasets. They ensure content is easy to read and understand. While tables were once used for webpage layouts, modern web design uses CSS for layouts and reserves tables for tabular data.
The basic structure of a table includes a <table> tag, with rows and cells defined inside:
<table>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
<tr>
<td>Cell 3</td>
<td>Cell 4</td>
</tr>
</table>
Cell 1 | Cell 2 |
Cell 3 | Cell 4 |
Key components:
Let’s explore how to create tables with examples and their previews.
A simple table with headers for a class schedule:
<table>
<tr>
<th>Day</th>
<th>Subject</th>
</tr>
<tr>
<td>Monday</td>
<td>Math</td>
</tr>
<tr>
<td>Tuesday</td>
<td>Science</td>
</tr>
</table>
Day | Subject |
---|---|
Monday | Math |
Tuesday | Science |
A table displaying product prices:
<table>
<tr>
<th>Product</th>
<th>Price</th>
<th>Category</th>
</tr>
<tr>
<td>Laptop</td>
<td>$999</td>
<td>Electronics</td>
</tr>
<tr>
<td>Book</td>
<td>$20</td>
<td>Stationery</td>
</tr>
</table>
Product | Price | Category |
---|---|---|
Laptop | $999 | Electronics |
Book | $20 | Stationery |
Tables support attributes to enhance functionality, though many are now styled with CSS:
margin
or text-align
).To make your tables effective and user-friendly:
width: 100%
or media queries).Beginners often make these errors:
border-collapse: collapse
and padding
to create clean, professional-looking tables.
Create a simple HTML file and experiment with tables. Add headers and multiple rows to see how they work.
<!DOCTYPE html>
<html lang="en">
<body>
<h3>Weekly Schedule</h3>
<table>
<tr>
<th>Time</th>
<th>Activity</th>
</tr>
<tr>
<td>9:00 AM</td>
<td>Meeting</td>
</tr>
<tr>
<td>10:00 AM</td>
<td>Coding</td>
</tr>
</table>
</body>
</html>
Time | Activity |
---|---|
9:00 AM | Meeting |
10:00 AM | Coding |
By mastering the <table> tag and its components, you can create clear, organized data presentations that enhance your website’s usability. Practice regularly to get comfortable!