The HTML <table> tag is used to create tabular data within an HTML document. It allows you to organize and present data in a structured format with rows and columns.
Here's the basic structure of the <table> tag:
<table>
<!-- table rows and cells -->
</table>
To define the content of the table, you use additional tags within the <table> tag:
<tr> (table row): Represents a row within the table.
<th> (table header cell): Represents a header cell within a row. It is typically used to define column headings.
<td> (table data cell): Represents a data cell within a row. It contains the actual data or content of the table.
Example usage:
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>25</td>
</tr>
<tr>
<td>Jane</td>
<td>Smith</td>
<td>30</td>
</tr>
</table>
In this example, a simple table is created with three columns: "First Name," "Last Name," and "Age." The <th> tags define the column headings, while the <td> tags contain the corresponding data.
You can further enhance the structure and appearance of the table using additional attributes and elements, such as <caption> for adding a table caption, <thead>, <tbody>, and <tfoot> for grouping table sections, and CSS styles for customizing the table's appearance.
Tables are commonly used to display data in a tabular format, such as financial data, schedules, product listings, and more. However, it's important to use tables appropriately and consider using other HTML elements and CSS for non-tabular data presentation whenever possible, as it improves accessibility and semantic structure.