The HTML Table is used to create a table using <table> tag. In table, each row is specified using <tr> tag and a table header is defined using <th> tag. A table data is defined using <td> tag.
Syntax
Following is the syntax −
<table> <tr> <th>Table Header</th> <th>Table Header</th> </tr> <tr> <td>Table data</td> <td>Table data</td> </tr> <tr> <td>Table data</td> <td>Table data</td> </tr> </table>
HTML table attributes
| Attribute | Explanation |
|---|---|
| colspan | It is used to span a table cell across column of a table. |
| rowspan | It is used to span a table cell across rows of a table. |
HTML caption element
We can also set caption for a table using HTML caption element.
Syntax
Following is the syntax −
<caption>text</caption>
Example
Let us see an example of HTML Tables:
<!DOCTYPE html>
<html>
<style>
body {
color: #000;
height: 100vh;
background-color: #8BC6EC;
background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%);
text-align: center;
}
table {
margin: 2rem auto;
width: 400px;
}
caption {
color: #fff;
font-size: 1.5rem;
}
</style>
<body>
<h1>HTML Tables</h1>
<table border="2">
<caption>Student Data</caption>
<tr>
<th>Name</th>
<th>Roll No.</th>
</tr>
<tr>
<td>John</td>
<td>031717</td>
</tr>
<tr>
<td>Elon</td>
<td>051717</td>
</tr>
</table>
</body>
</html>Output
