Name | Surname | Nationality | Age | Position |
David | Ospina Ramírez | Colombia | 33 | Portero |
To start, add a
<table>
tag to the
<body>
of your code.
<table>
</table>
<table>
<thead>
</thead>
<tbody>
</tbody>
</table>
As you might expect,
<thead>
contains the headings and
<tbody>
contains the data.
You don't need to use these in every table, but they are useful if you want to change the data dynamically using javascript so it's good to get used to using them.
This assumes you want your headings along the top, but if you choose to put your headings in the first column, that's a good time to not use
<thead>
and
<tbody>
.
Let's add the heading row with five heading cells.
<table>
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Nationality</th>
<th>Age</th>
<th>Position</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<tr>
signifies the table row.
<th>
signifies a table heading cell.
Run the code to see the results. Right now, there is no border, but the headings are automatically bold.
Let's add a line of data.
<table>
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Nationality</th>
<th>Age</th>
<th>Position</th>
</tr>
</thead>
<tbody>
<tr>
<td>David</td>
<td>Ospina Ramírez</td>
<td>Colombia</td>
<td>33</td>
<td>Portero</td>
</tr>
</tbody>
</table>
Name | Surname | Nationality | Age | Position |
David | Ospina Ramírez | Colombia | 33 | Portero |
Carlos | Eccehomo Cuesta Figueroa | Colombia | 22 | Defensa |
Juan Guillermo | Cuadrado Bello | Colombia | 33 | Centrocampista |
Radamel Falcao | García Zárate | Colombia | 35 | Delantero |
How did you go? Does the table look right?
Notice that the width of each column changed to fit the width of the largest cell (longest text). Tables are clever like that.
This will be more obvious when we show the borders.