HTML tables are used to organize data into rows and columns. The basic structure of an HTML table includes the <table>, <tr> (table row), <th> (table header), and <td> (table data) elements.
A basic HTML table is created using the <table> tag along with <tr>, <th>, and <td> tags.
Example
<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
<td>Row 1, Cell 3</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
<td>Row 2, Cell 3</td>
</tr>
</table>
In this example, the table contains three columns and three rows. The first row contains table headers defined by the <th> tag, and the remaining rows contain table data defined by the <td> tag.
To add borders to an HTML table, you can use the border attribute or CSS styling.
Example
<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
<td>Row 1, Cell 3</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
<td>Row 2, Cell 3</td>
</tr>
</table>
This example shows how to add borders to a table using the border attribute.
You can add a caption to your table using the <caption> tag. The caption tag is used to provide a title for the table.
Example
<div class="table-caption">
Table caption content
</div>
In this example, the table has a caption describing its contents.
The colspan attribute allows a cell to span multiple columns, while the rowspan attribute allows a cell to span multiple rows.
Example
<table border="1">
<tr>
<th colspan="2">Header spanning 2 columns</th>
</tr>
<tr>
<td rowspan="2">Rowspan 2 rows</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td>Row 2, Cell 2</td>
</tr>
</table>
This example demonstrates how to use the colspan and rowspan attributes to create cells that span multiple columns and rows.
You can use CSS to style your tables and make them more visually appealing. This includes adding borders, padding, background colors, and more.
Example
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 10px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
In this example, CSS is used to style the table, including adding borders, padding, and background colors.
To make tables responsive, you can use CSS to allow horizontal scrolling on smaller screens.
Example
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 10px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
@media screen and (max-width: 600px) {
table, thead, tbody, th, td, tr {
display: block;
width: 100%;
}
thead {
display: none;
}
tr {
margin-bottom: 15px;
}
td {
text-align: right;
padding-left: 50%;
position: relative;
}
td:before {
content: attr(data-label);
position: absolute;
left: 0;
width: 50%;
padding-left: 10px;
font-weight: bold;
text-align: left;
}
}
This example demonstrates how to make a table responsive using CSS.
Here are some important notes and best practices when using tables in HTML: