To create tables in HTML, you'll use the <table>, <tr>, <th>, and <td> elements:
-
<table>:
Defines the table itself. -
<tr>:
Stands for table row and defines each row within the table. -
<th>:
Represents table header cells (typically the headings of columns or rows). -
<td>:
Represents standard cells in the table, containing data.
Here's an example of a simple table with two rows and two columns:
<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</table>
This code will generate a basic table with a border attribute to make the table borders visible. Replace the content within <th> and <td> tags with your actual data.
To add more rows or columns, simply replicate the <tr>, <th>, or <td> elements within the <table> element.
Here's an example of a table with multiple rows and columns:
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
<th>Country</th>
</tr>
<tr>
<td>John</td>
<td>25</td>
<td>USA</td>
</tr>
<tr>
<td>Alice</td>
<td>30</td>
<td>Canada</td>
</tr>
</table>
This would create a table with three columns and two rows, displaying information about individuals in each row.
Additionally, you can use attributes such as colspan and rowspan
to define cells that span multiple columns or rows, respectively.
Tables can become complex, and they might need styling for better presentation. CSS can be used to enhance the appearance, alignment, and other visual aspects of the table. But this basic structure will help you get started with creating tables in HTML.