HTML <table> -Tag
Beispiel
Eine einfache HTML-Tabelle mit zwei Spalten und zwei Zeilen:
<table>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
</table>
Weitere „Probieren Sie es selbst“-Beispiele weiter unten.
Definition und Verwendung
Das <table>
Tag definiert eine HTML-Tabelle.
Eine HTML-Tabelle besteht aus einem <table>
Element und einem oder mehreren <tr> -,
<th> - und <td> -Elementen.
Das Element <tr> definiert eine Tabellenzeile, das Element <th> definiert einen Tabellenkopf und das Element <td> definiert eine Tabellenzelle.
Eine HTML-Tabelle kann auch die Elemente <caption> , <colgroup> , <thead> , <tfoot> und <tbody> enthalten.
Browser-Unterstützung
Element | |||||
---|---|---|---|---|---|
<table> | Yes | Yes | Yes | Yes | Yes |
Globale Attribute
Das <table>
Tag unterstützt auch die globalen Attribute in HTML .
Ereignisattribute
Das <table>
Tag unterstützt auch die Ereignisattribute in HTML .
Mehr Beispiele
Beispiel
So fügen Sie einer Tabelle reduzierte Rahmen hinzu (mit 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>
Beispiel
So richten Sie eine Tabelle rechts aus (mit 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>
Beispiel
So zentrieren Sie eine Tabelle (mit 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>
Beispiel
So fügen Sie einer Tabelle eine Hintergrundfarbe hinzu (mit 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>
Beispiel
So fügen Sie einer Tabelle Padding hinzu (mit 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>
Beispiel
So legen Sie die Tabellenbreite fest (mit 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>
Beispiel
So erstellen Sie Tabellenüberschriften:
<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>
Beispiel
So erstellen Sie eine Tabelle mit Beschriftung:
<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>
Beispiel
So definieren Sie Tabellenzellen, die sich über mehr als eine Zeile oder eine Spalte erstrecken:
<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>
Verwandte Seiten
HTML-Tutorial: HTML-Tabellen
HTML-DOM-Referenz: Tabellenobjekt
CSS-Tutorial: Tabellen formatieren
Standard-CSS-Einstellungen
Die meisten Browser zeigen das <table>
Element mit den folgenden Standardwerten an:
Beispiel
table {
display: table;
border-collapse: separate;
border-spacing: 2px;
border-color: gray;
}