HTML En-têtes de tableau
Les tableaux HTML peuvent avoir des en-têtes pour chaque colonne ou ligne, ou pour plusieurs colonnes/lignes.
| EMIL | TOBIAS | LINUS |
|---|---|---|
| 8:00 | ||
|---|---|---|
| 9:00 | ||
| 10:00 | ||
| 11:00 | ||
| 12:00 | ||
| 13:00 |
| MON | TUE | WED | THU | FRI | |
|---|---|---|---|---|---|
| 8:00 | |||||
| 9:00 | |||||
| 10:00 | |||||
| 11:00 | |||||
| 12:00 |
| DECEMBER | ||
|---|---|---|
HTML En-têtes de tableau
Les en-têtes de tableau sont définis avec les ième éléments, chaque ième élément représente une cellule du tableau.
Exemple
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
Try it Yourself »
En-têtes de tableau verticaux
Pour utiliser la première colonne comme en-tête de tableau, définissez la première cellule de chaque ligne comme un th élément :
Exemple
<table>
<tr>
<th>Firstname</th>
<td>Jill</td>
<td>Eve</td>
</tr>
<tr>
<th>Lastname</th>
<td>Smith</td>
<td>Jackson</td>
</tr>
<tr>
<th>Age</th>
<td>94</td>
<td>50</td>
</tr>
</table>
Try it Yourself »
Aligner les en-têtes de tableau
Par défaut, les en-têtes des tableaux sont en gras et centrés :
| Prénom | Nom | Âge |
|---|---|---|
| Jill | Smith | 50 |
| Eve | Jackson | 94 |
Pour aligner à gauche les en-têtes du tableau, utilisez la propriété CSS text-align :
En-tête pour plusieurs colonnes
Vous pouvez avoir un en-tête qui s'étend sur deux colonnes ou plus.
| Nom | Âge | |
|---|---|---|
| Jill | Smith | 50 |
| Eve | Jackson | 94 |
Pour ce faire, utilisez l'attribut colspan sur l'élément <th> :
Exemple
<table>
<tr>
<th colspan="2">Name</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
Try it Yourself »
Vous en apprendrez plus sur colspan et rowspan dans le chapitre Table colspan & rowspan.
Légende du tableau
Vous pouvez ajouter une légende qui sert de titre pour l'ensemble du tableau.
| Mois | Économies |
|---|---|
| Janvier | $100 |
| Février | $50 |
Pour ajouter une légende à un tableau, utilisez la balise <caption> :
Exemple
<table style="width:100%">
<caption>Monthly savings</caption>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$50</td>
</tr>
</table>
Try it Yourself »
Note : La balise <caption> doit être insérée immédiatement après la balise <table>.
