Etiqueta HTML <tabla>


Ejemplo

Una tabla HTML simple, que contiene dos columnas y dos filas:

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

Más ejemplos de "Pruébelo usted mismo" a continuación.


Definición y uso

La <table>etiqueta define una tabla HTML.

Una tabla HTML consta de un <table>elemento y uno o más elementos <tr> , <th> y <td> .

El elemento <tr> define una fila de tabla, el elemento <th> define un encabezado de tabla y el elemento <td> define una celda de tabla.

Una tabla HTML también puede incluir elementos <caption> , <colgroup> , <thead> , <tfoot> y <tbody> .


Compatibilidad con navegador

Element
<table> Yes Yes Yes Yes Yes

Atributos globales

La <table>etiqueta también es compatible con los atributos globales en HTML .


Atributos de eventos

La <table>etiqueta también es compatible con los atributos de eventos en HTML .



Más ejemplos

Ejemplo

Cómo agregar bordes colapsados ​​a una tabla (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>

Ejemplo

Cómo alinear a la derecha una tabla (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>

Ejemplo

Cómo centrar una tabla (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>

Ejemplo

Cómo agregar color de fondo a una tabla (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>

Ejemplo

Cómo agregar relleno a una tabla (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>

Ejemplo

Cómo establecer el ancho de la tabla (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>

Ejemplo

Cómo crear encabezados de tabla:

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

Ejemplo

Cómo crear una tabla con un título:

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

Ejemplo

Cómo definir celdas de tabla que abarcan más de una fila o una columna:

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

páginas relacionadas

Tutorial de HTML: tablas HTML

Referencia HTML DOM: Objeto de tabla

Tutorial de CSS: tablas de estilo


Configuración predeterminada de CSS

La mayoría de los navegadores mostrarán el <table>elemento con los siguientes valores predeterminados:

Ejemplo

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