Tag HTML <tabella>


Esempio

Una semplice tabella HTML, contenente due colonne e due righe:

<table>
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
</table>

Altri esempi "Provalo da solo" di seguito.


Definizione e utilizzo

Il <table>tag definisce una tabella HTML.

Una tabella HTML è composta da un <table>elemento e uno o più elementi <tr> , <th> e <td> .

L'elemento <tr> definisce una riga di tabella, l'elemento <th> definisce un'intestazione di tabella e l'elemento <td> definisce una cella di tabella.

Una tabella HTML può anche includere elementi <caption> , <colgroup> , <thead> , <tfoot> e <tbody> .


Supporto browser

Element
<table> Yes Yes Yes Yes Yes

Attributi globali

Il <table>tag supporta anche gli attributi globali in HTML .


Attributi dell'evento

Il <table>tag supporta anche gli attributi dell'evento in HTML .



Altri esempi

Esempio

Come aggiungere bordi compressi a una tabella (con 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>

Esempio

Come allineare a destra una tabella (con 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>

Esempio

Come allineare al centro una tabella (con 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>

Esempio

Come aggiungere il colore di sfondo a una tabella (con 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>

Esempio

Come aggiungere padding a una tabella (con 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>

Esempio

Come impostare la larghezza della tabella (con 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>

Esempio

Come creare intestazioni di tabella:

<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>

Esempio

Come creare una tabella con una didascalia:

<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>

Esempio

Come definire le celle di una tabella che si estendono su più di una riga o di una colonna:

<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>

Pagine correlate

Esercitazione HTML: Tabelle HTML

Riferimento HTML DOM: Oggetto tabella

Tutorial CSS: stilizzare le tabelle


Impostazioni CSS predefinite

La maggior parte dei browser visualizzerà l' <table>elemento con i seguenti valori predefiniti:

Esempio

table {
  display: table;
  border-collapse: separate;
  border-spacing: 2px;
  border-color: gray;
}