Elementos table, caption, tr, th y td


El elemento table define una tabla para organizar datos multidimensionales en filas y columnas. El elemento table se utiliza habitualmente como dispositivo de diseño, pero los autores deben evitar esta práctica en la medida de los posible y, en su lugar, utilizar hojas de estilo.

El elemento opcional caption establece una leyenda para la tabla

El cuerpo de la tabla está formado por una o más filas definidas por elementos tr. Cada fila se compone de una o más celdas definidas mediante los elementos td y th.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es">
  <head>
    <title>Elementos table, caption, tr, th y td</title>
  </head>
  <body>
    <table summary="Esta tabla muestra
    los nombres de los caracteres, el código de entidad, los códigos
    numéricos (en formato decimal y hexadecimal) y la visualización">
      <caption>
        Entidades Latin-1
      </caption>
      <tr>
        <th>
          Carácter
        </th>
        <th>
          Entidad
        </th>
        <th>
          Decimal
        </th>
        <th>
          Hexadec.
        </th>
        <th>
          Visualización
        </th>
      </tr>
      <tr>
        <td scope="row">
          no-break space = non-breaking space
        </td>
        <td>&nbsp;</td>
        <td>&#160;</td>
        <td>&#xA0;</td>
        <td>
              
        </td>
      <tr>
        <td scope="row">
          inverted exclamation mark
        </td>
        <td>&iexcl;</td>
        <td>&#161;</td>
        <td>&#xA1;</td>
        <td>¡</td>
      </tr>
      <tr>
        <td scope="row">
          cent sign
        </td>
        <td>&cent;</td>
        <td>&#162;</td>
        <td>&#xA2;</td>
        <td>¢</td>
      </tr>  
      <tr>
        <td scope="row">
          pound sign
        </td>
        <td>&pound;</td>     
        <td>&#163;</td>
        <td>&#xA3;</td>
        <td>£</td>
      </tr>   
    </table>
 </body>
</html>

El atributo summary de table es imprescindible para que la página sea accesible. El valor de atributo resume el propósito y estructura de la tabla.

Los elementos th y td tienen dos atributos específicos de uso relativamente frecuente:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es">
  <head>
    <title>Elementos table, caption, tr, th y td</title>
  </head>
  <body>
    <table summary="Esta tabla muestra
    los nombres de las asignaturas, el tipo, la temporalidad y los
    créditos que tiene desglosados">
      <caption>
        Primer curso
      </caption>
      <thead>
      
        <tr>
          <th rowspan="2">
            Asignatura
          </th>
          <th rowspan="2">
            Tipo
          </th>
          <th rowspan="2">
            Temporalidad
          </th>
          <th colspan="3">
            Créditos
          </th>
        </tr>

        <tr>
          <th>
            Teóricos
          </th>
          <th>
            Prácticos
          </th>
          <th>
            Laboratorio
          </th>
        </tr>

      </thead>
      <tbody>
        <tr>
          <td scope="row">
            Metodología de la Programación I
          </td>
          <td>Troncal</td>
          <td>Anual</td>
          <td>6</td>
          <td>4.5</td>
          <td>4.5</td>
        </tr>
      </tbody>
    </table>
 </body>
</html>