Balise HTML <table>
Exemple
Un tableau HTML simple, contenant deux colonnes et deux lignes :
<table>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
</table>
Plus d'exemples "Essayez-le vous-même" ci-dessous.
Définition et utilisation
La <table>
balise définit un tableau HTML.
Un tableau HTML se compose d'un <table>
élément et d'un ou plusieurs éléments <tr> ,
<th> et <td> .
L'élément <tr> définit une ligne de tableau, l'élément <th> définit un en-tête de tableau et l'élément <td> définit une cellule de tableau.
Un tableau HTML peut également inclure des éléments <caption> , <colgroup> , <thead> , <tfoot> et <tbody> .
Prise en charge du navigateur
Element | |||||
---|---|---|---|---|---|
<table> | Yes | Yes | Yes | Yes | Yes |
Attributs globaux
La <table>
balise prend également en charge les attributs globaux en HTML .
Attributs d'événement
La <table>
balise prend également en charge les attributs d'événement en HTML .
Plus d'exemples
Exemple
Comment ajouter des bordures réduites à un tableau (avec CSS) :
<html>
<head>
<style>
table, th, td {
border: 1px solid
black;
border-collapse: collapse;
}
</style>
</head>
<body>
<table>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</table>
</body>
</html>
Exemple
Comment aligner à droite un tableau (avec CSS):
<table style="float:right">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</table>
Exemple
Comment centrer un tableau (avec CSS):
<html>
<head>
<style>
table, th, td {
border: 1px solid
black;
}
table.center {
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<table
class="center">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</table>
Exemple
Comment ajouter une couleur de fond à un tableau (avec CSS):
<table style="background-color:#00FF00">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</table>
Exemple
Comment ajouter un rembourrage à un tableau (avec CSS) :
<html>
<head>
<style>
table, th, td {
border: 1px solid
black;
}
th, td {
padding: 10px;
}
</style>
</head>
<body>
<table>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</table>
</body>
</html>
Exemple
Comment définir la largeur du tableau (avec CSS):
<table style="width:400px">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</table>
Exemple
Comment créer des en-têtes de tableau :
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
</tr>
<tr>
<td>John Doe</td>
<td>[email protected]</td>
<td>123-45-678</td>
</tr>
</table>
Exemple
Comment créer un tableau avec une légende :
<table>
<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>$80</td>
</tr>
</table>
Exemple
Comment définir des cellules de tableau qui s'étendent sur plusieurs lignes ou colonnes :
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th colspan="2">Phone</th>
</tr>
<tr>
<td>John Doe</td>
<td>[email protected]</td>
<td>123-45-678</td>
<td>212-00-546</td>
</tr>
</table>
Pages connexes
Tutoriel HTML : Tableaux HTML
Référence HTML DOM : objet de table
Tutoriel CSS : styliser les tableaux
Paramètres CSS par défaut
La plupart des navigateurs afficheront l' <table>
élément avec les valeurs par défaut suivantes :
Exemple
table {
display: table;
border-collapse: separate;
border-spacing: 2px;
border-color: gray;
}