HTML Table
An HTML table is defined with the table tag.
It is used to display data in a tabular form on a web page.
Data is shown in various rows and columns as required.
It uses the below tags for rows and column:-
<th> : Used to define table column headings </th>
<tr> : Defines the row.</tr>
<td> : Defines the column.</td>
Let's understand this by the following example:
<table>
<tr>
<th> Firstname </th>
<th> Lastname </th>
<th> Age </th>
</tr>
<tr>
<td>Ranbir </td>
<td>Kapoor </td>
<td>50 </td>
</tr>
<tr>
<td> Ranveer</td>
<td>Singh </td>
<td>94 </td>
</tr>
</table>
Output:
Add Border
There are 2 ways to add border in tables.
By border attribute of table in HTML
By border property in CSS
By border attribute
You can use border attribute of table tag in HTML to specify border.
Let's understand this by the following example:
<table border="1">
<tr>
<th> Firstname </th>
<th> Lastname </th>
<th> Age </th>
</tr>
<tr>
<td>Ranbir </td>
<td>Kapoor </td>
<td>50 </td>
</tr>
<tr>
<td> Ranveer</td>
<td>Singh </td>
<td>94 </td>
</tr>
</table>
Output:
By border property in CSS
It is now recommended to use border property of CSS to specify border in table.
Let's understand this by the following example:
<style> table,th,td {border:1 px solid black }
</style>
<table>
<tr>
<th> Firstname </th>
<th> Lastname </th>
<th> Age </th>
</tr>
<tr>
<td>Ranbir </td>
<td>Kapoor </td>
<td>50 </td>
</tr>
<tr>
<td> Ranveer</td>
<td>Singh </td>
<td>94 </td>
</tr>
</table>
Output:
Collapse Border
If you want to collapse border,use border-collapse property:
Let's understand this by the following example:
<style> table,th,td { border:1 px solid black
border-collapse:collapse; }
</style>
<table>
<tr>
<th> Firstname </th>
<th> Lastname </th>
<th> Age </th>
</tr>
<tr>
<td>Ranbir </td>
<td>Kapoor </td>
<td>50 </td>
</tr>
<tr>
<td> Ranveer</td>
<td>Singh </td>
<td>94 </td>
</tr>
</table>
Output:
Table Caption
Caption is display above table.It provide necessary information about table.To add caption to table use caption tag.
Let's understand this by the following example:
<table> border="2" style="border-collapse:collapse">
<caption> Employer Data </caption>
<tr>
<th> Firstname </th>
<th> Lastname </th>
<th> Age </th>
</tr>
<tr>
<td>Ranbir </td>
<td>Kapoor </td>
<td>50 </td>
</tr>
<tr>
<td> Ranveer</td>
<td>Singh </td>
<td>94 </td>
</tr>
</table>
Output:
HTML table with rowspan
Rowspan is used to make a cell span more than one row.It can apply with rowspan attribute.
Let's understand this by the following example:
<table style="width:100%">
<tr>
<th>Name</th>
<td>rahul</td>
</tr>
<tr>
<th rowspan="2">Phone</th>
<td>777777</td>
</tr>
<tr>
<td>888888</td>
</tr>
</table>
Output:
Name
rahul
Phone
777777
888888
HTML Table - Colspan
To make a cell span over multiple columns, use the colspan attribute:
<table style="width:100%">
<tr>
<th colspan="2">Name</th>
<th>Age</th>
</tr>
<tr>
<td>rahul</td>
<td>yadav</td>
<td>73</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>57</td>
</tr>
</table>
Output:
Name
Age
rahul
yadav
73
Eve
Jackson
57
Previous
Next