6. Tablas en HTML
Las tablas organizan datos en filas y columnas, facilitando la lectura y comparación de información estructurada.
📋 Estructura Básica de una Tabla
Section titled “📋 Estructura Básica de una Tabla”Las tablas se construyen con elementos específicos que definen su estructura.
Elementos fundamentales
Section titled “Elementos fundamentales”<table> <!-- Contenedor de la tabla --> <tr> <!-- Table Row: Fila --> <td> <!-- Table Data: Celda de datos --> Contenido </td> </tr></table>Elementos principales:
| Elemento | Descripción | Uso |
|---|---|---|
<table> | Contenedor principal | Envuelve toda la tabla |
<tr> | Fila (Table Row) | Cada fila de la tabla |
<td> | Celda de datos (Table Data) | Celda normal con datos |
<th> | Celda de encabezado (Table Header) | Encabezado de columna/fila |
Tabla simple
Section titled “Tabla simple”<!-- Tabla básica 2x2 --><table> <tr> <td>Fila 1, Columna 1</td> <td>Fila 1, Columna 2</td> </tr> <tr> <td>Fila 2, Columna 1</td> <td>Fila 2, Columna 2</td> </tr></table>Tabla con encabezados
Section titled “Tabla con encabezados”<!-- Tabla con encabezados --><table> <tr> <th>Nombre</th> <th>Edad</th> <th>Ciudad</th> </tr> <tr> <td>Juan</td> <td>25</td> <td>Madrid</td> </tr> <tr> <td>María</td> <td>30</td> <td>Barcelona</td> </tr></table><th> vs <td>
Section titled “<th> vs <td>”<th> - Table Header (Encabezado):
<th>Encabezado</th>- ✅ Para títulos de columnas o filas
- ✅ Negrita por defecto
- ✅ Centrado por defecto
- ✅ Mejor para accesibilidad
- ✅ Atributo
scopepara especificar alcance
<td> - Table Data (Datos):
<td>Datos</td>- ✅ Para contenido normal
- ✅ Texto normal por defecto
- ✅ Alineado a la izquierda por defecto
- ✅ Para valores y contenido
Atributo scope en <th>
Section titled “Atributo scope en <th>”<!-- Encabezados de columna --><tr> <th scope="col">Producto</th> <th scope="col">Precio</th> <th scope="col">Stock</th></tr>
<!-- Encabezados de fila --><tr> <th scope="row">Enero</th> <td>1000</td> <td>1500</td></tr>Valores de scope:
col- Encabezado de columnarow- Encabezado de filacolgroup- Grupo de columnasrowgroup- Grupo de filas
Atributos de celdas
Section titled “Atributos de celdas”<!-- colspan: Combinar columnas --><td colspan="2">Celda que ocupa 2 columnas</td>
<!-- rowspan: Combinar filas --><td rowspan="3">Celda que ocupa 3 filas</td>
<!-- headers: Asociar con encabezados --><td headers="nombre edad">Datos</td>
<!-- Combinación de atributos --><td colspan="2" rowspan="2">Celda grande</td>Atributos de tabla
Section titled “Atributos de tabla”<!-- border: Borde (obsoleto, usar CSS) --><table border="1">
<!-- cellpadding: Espaciado interno (obsoleto, usar CSS) --><table cellpadding="10">
<!-- cellspacing: Espaciado entre celdas (obsoleto, usar CSS) --><table cellspacing="5">
<!-- width: Ancho (obsoleto, usar CSS) --><table width="100%"> 🎨 Tabla básica
<div style="font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 30px; background: #f8f9fa;">
<h2 style="color: #2c3e50; margin: 0 0 25px 0;">Tabla de Empleados</h2>
<!-- Tabla simple -->
<table style="width: 100%; border-collapse: collapse; background: white; box-shadow: 0 2px 8px rgba(0,0,0,0.1); border-radius: 8px; overflow: hidden;">
<tr style="background: #3498db; color: white;">
<th style="padding: 15px; text-align: left; font-weight: bold;">Nombre</th>
<th style="padding: 15px; text-align: left; font-weight: bold;">Cargo</th>
<th style="padding: 15px; text-align: left; font-weight: bold;">Departamento</th>
<th style="padding: 15px; text-align: right; font-weight: bold;">Salario</th>
</tr>
<tr style="border-bottom: 1px solid #ecf0f1;">
<td style="padding: 15px;">Juan Pérez</td>
<td style="padding: 15px;">Desarrollador</td>
<td style="padding: 15px;">IT</td>
<td style="padding: 15px; text-align: right; font-weight: bold; color: #27ae60;">$3,500</td>
</tr>
<tr style="background: #f8f9fa; border-bottom: 1px solid #ecf0f1;">
<td style="padding: 15px;">María García</td>
<td style="padding: 15px;">Diseñadora</td>
<td style="padding: 15px;">Diseño</td>
<td style="padding: 15px; text-align: right; font-weight: bold; color: #27ae60;">$3,200</td>
</tr>
<tr style="border-bottom: 1px solid #ecf0f1;">
<td style="padding: 15px;">Carlos López</td>
<td style="padding: 15px;">Project Manager</td>
<td style="padding: 15px;">Gestión</td>
<td style="padding: 15px; text-align: right; font-weight: bold; color: #27ae60;">$4,000</td>
</tr>
<tr style="background: #f8f9fa; border-bottom: 1px solid #ecf0f1;">
<td style="padding: 15px;">Ana Martínez</td>
<td style="padding: 15px;">Marketing</td>
<td style="padding: 15px;">Marketing</td>
<td style="padding: 15px; text-align: right; font-weight: bold; color: #27ae60;">$2,800</td>
</tr>
<tr>
<td style="padding: 15px;">Pedro Sánchez</td>
<td style="padding: 15px;">Desarrollador</td>
<td style="padding: 15px;">IT</td>
<td style="padding: 15px; text-align: right; font-weight: bold; color: #27ae60;">$3,600</td>
</tr>
</table>
<div style="margin-top: 20px; padding: 15px; background: #e8f4f8; border-radius: 8px; border-left: 4px solid #3498db;">
<p style="margin: 0; color: #2c3e50; font-size: 14px;">
<strong>💡 Tip:</strong> Los encabezados (<code><th></code>) están en negrita y con fondo azul.
Las filas alternas tienen fondo gris para mejor legibilidad.
</p>
</div>
</div> tr:hover {
background: #e3f2fd !important;
transition: background 0.3s;
} 🗂️ Agrupación de Contenido
Section titled “🗂️ Agrupación de Contenido”Las tablas se pueden dividir en secciones semánticas para mejor organización.
Estructura semántica
Section titled “Estructura semántica”<table> <thead> <!-- Encabezado de la tabla --> <tr> <th>Columna 1</th> <th>Columna 2</th> </tr> </thead>
<tbody> <!-- Cuerpo de la tabla (datos) --> <tr> <td>Dato 1</td> <td>Dato 2</td> </tr> </tbody>
<tfoot> <!-- Pie de la tabla (totales, resumen) --> <tr> <td>Total</td> <td>100</td> </tr> </tfoot></table>Elementos de agrupación:
| Elemento | Descripción | Uso típico |
|---|---|---|
<thead> | Encabezado de tabla | Títulos de columnas |
<tbody> | Cuerpo de tabla | Datos principales |
<tfoot> | Pie de tabla | Totales, resúmenes |
Ventajas de la agrupación
Section titled “Ventajas de la agrupación”- ✅ Semántica: Estructura clara y significativa
- ✅ Accesibilidad: Lectores de pantalla entienden mejor
- ✅ Estilo: Fácil aplicar CSS a secciones
- ✅ Impresión:
<thead>y<tfoot>se repiten en cada página - ✅ JavaScript: Fácil manipular secciones específicas
Tabla con todas las secciones
Section titled “Tabla con todas las secciones”<table> <!-- Encabezado --> <thead> <tr> <th>Producto</th> <th>Cantidad</th> <th>Precio</th> <th>Subtotal</th> </tr> </thead>
<!-- Cuerpo --> <tbody> <tr> <td>Laptop</td> <td>2</td> <td>$800</td> <td>$1,600</td> </tr> <tr> <td>Mouse</td> <td>5</td> <td>$20</td> <td>$100</td> </tr> <tr> <td>Teclado</td> <td>3</td> <td>$50</td> <td>$150</td> </tr> </tbody>
<!-- Pie --> <tfoot> <tr> <td colspan="3"><strong>Total</strong></td> <td><strong>$1,850</strong></td> </tr> </tfoot></table>Múltiples tbody
Section titled “Múltiples tbody”<table> <thead> <tr> <th>Mes</th> <th>Ventas</th> </tr> </thead>
<!-- Primer trimestre --> <tbody> <tr> <td>Enero</td> <td>$10,000</td> </tr> <tr> <td>Febrero</td> <td>$12,000</td> </tr> <tr> <td>Marzo</td> <td>$15,000</td> </tr> </tbody>
<!-- Segundo trimestre --> <tbody> <tr> <td>Abril</td> <td>$14,000</td> </tr> <tr> <td>Mayo</td> <td>$16,000</td> </tr> <tr> <td>Junio</td> <td>$18,000</td> </tr> </tbody>
<tfoot> <tr> <td><strong>Total</strong></td> <td><strong>$85,000</strong></td> </tr> </tfoot></table><caption> - Título de la tabla
Section titled “<caption> - Título de la tabla”<table> <caption>Ventas Mensuales 2024</caption> <thead> <tr> <th>Mes</th> <th>Ventas</th> </tr> </thead> <tbody> <tr> <td>Enero</td> <td>$10,000</td> </tr> </tbody></table>Posición del caption:
<!-- Por defecto: arriba --><caption>Título arriba</caption>
<!-- Abajo con CSS --><caption style="caption-side: bottom;">Título abajo</caption>Características:
- ✅ Describe el contenido de la tabla
- ✅ Mejora la accesibilidad
- ✅ Debe ser el primer elemento dentro de
<table> - ✅ Solo uno por tabla
Tabla completa con caption
Section titled “Tabla completa con caption”<table> <caption> <strong>Reporte de Ventas Q1 2024</strong> <br> <small>Datos en miles de dólares</small> </caption>
<thead> <tr> <th scope="col">Región</th> <th scope="col">Enero</th> <th scope="col">Febrero</th> <th scope="col">Marzo</th> <th scope="col">Total</th> </tr> </thead>
<tbody> <tr> <th scope="row">Norte</th> <td>$50</td> <td>$55</td> <td>$60</td> <td>$165</td> </tr> <tr> <th scope="row">Sur</th> <td>$45</td> <td>$50</td> <td>$55</td> <td>$150</td> </tr> </tbody>
<tfoot> <tr> <th scope="row">Total</th> <td>$95</td> <td>$105</td> <td>$115</td> <td><strong>$315</strong></td> </tr> </tfoot></table> 🎨 Tabla con agrupación
<div style="font-family: Arial, sans-serif; max-width: 900px; margin: 0 auto; padding: 30px; background: #f8f9fa;">
<table style="width: 100%; border-collapse: collapse; background: white; box-shadow: 0 4px 12px rgba(0,0,0,0.1); border-radius: 12px; overflow: hidden;">
<!-- Caption -->
<caption style="padding: 20px; font-size: 24px; font-weight: bold; color: #2c3e50; text-align: left; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white;">
📊 Reporte de Ventas Trimestrales 2024
<div style="font-size: 14px; font-weight: normal; margin-top: 5px; opacity: 0.9;">
Datos en miles de dólares (USD)
</div>
</caption>
<!-- Thead -->
<thead>
<tr style="background: #34495e; color: white;">
<th style="padding: 15px; text-align: left; font-weight: bold; border-right: 1px solid rgba(255,255,255,0.1);">Región</th>
<th style="padding: 15px; text-align: center; font-weight: bold; border-right: 1px solid rgba(255,255,255,0.1);">Enero</th>
<th style="padding: 15px; text-align: center; font-weight: bold; border-right: 1px solid rgba(255,255,255,0.1);">Febrero</th>
<th style="padding: 15px; text-align: center; font-weight: bold; border-right: 1px solid rgba(255,255,255,0.1);">Marzo</th>
<th style="padding: 15px; text-align: center; font-weight: bold;">Total Q1</th>
</tr>
</thead>
<!-- Tbody -->
<tbody>
<tr style="border-bottom: 1px solid #ecf0f1;">
<th scope="row" style="padding: 15px; text-align: left; background: #f8f9fa; font-weight: 600; color: #2c3e50;">🌍 Norte</th>
<td style="padding: 15px; text-align: center;">$50.2</td>
<td style="padding: 15px; text-align: center;">$55.8</td>
<td style="padding: 15px; text-align: center;">$60.5</td>
<td style="padding: 15px; text-align: center; font-weight: bold; color: #27ae60;">$166.5</td>
</tr>
<tr style="border-bottom: 1px solid #ecf0f1;">
<th scope="row" style="padding: 15px; text-align: left; background: #f8f9fa; font-weight: 600; color: #2c3e50;">🌎 Sur</th>
<td style="padding: 15px; text-align: center;">$45.3</td>
<td style="padding: 15px; text-align: center;">$50.1</td>
<td style="padding: 15px; text-align: center;">$55.7</td>
<td style="padding: 15px; text-align: center; font-weight: bold; color: #27ae60;">$151.1</td>
</tr>
<tr style="border-bottom: 1px solid #ecf0f1;">
<th scope="row" style="padding: 15px; text-align: left; background: #f8f9fa; font-weight: 600; color: #2c3e50;">🌏 Este</th>
<td style="padding: 15px; text-align: center;">$62.8</td>
<td style="padding: 15px; text-align: center;">$68.4</td>
<td style="padding: 15px; text-align: center;">$72.9</td>
<td style="padding: 15px; text-align: center; font-weight: bold; color: #27ae60;">$204.1</td>
</tr>
<tr style="border-bottom: 1px solid #ecf0f1;">
<th scope="row" style="padding: 15px; text-align: left; background: #f8f9fa; font-weight: 600; color: #2c3e50;">🗺️ Oeste</th>
<td style="padding: 15px; text-align: center;">$38.5</td>
<td style="padding: 15px; text-align: center;">$42.3</td>
<td style="padding: 15px; text-align: center;">$47.8</td>
<td style="padding: 15px; text-align: center; font-weight: bold; color: #27ae60;">$128.6</td>
</tr>
</tbody>
<!-- Tfoot -->
<tfoot>
<tr style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; font-weight: bold;">
<th scope="row" style="padding: 18px; text-align: left; font-size: 16px;">💰 TOTAL GENERAL</th>
<td style="padding: 18px; text-align: center; font-size: 16px;">$196.8</td>
<td style="padding: 18px; text-align: center; font-size: 16px;">$216.6</td>
<td style="padding: 18px; text-align: center; font-size: 16px;">$236.9</td>
<td style="padding: 18px; text-align: center; font-size: 18px; background: rgba(255,255,255,0.2);">$650.3</td>
</tr>
</tfoot>
</table>
<div style="margin-top: 25px; display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px;">
<div style="padding: 15px; background: white; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); text-align: center;">
<div style="font-size: 12px; color: #7f8c8d; margin-bottom: 5px;">THEAD</div>
<div style="font-size: 18px; font-weight: bold; color: #34495e;">Encabezados</div>
</div>
<div style="padding: 15px; background: white; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); text-align: center;">
<div style="font-size: 12px; color: #7f8c8d; margin-bottom: 5px;">TBODY</div>
<div style="font-size: 18px; font-weight: bold; color: #27ae60;">Datos</div>
</div>
<div style="padding: 15px; background: white; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); text-align: center;">
<div style="font-size: 12px; color: #7f8c8d; margin-bottom: 5px;">TFOOT</div>
<div style="font-size: 18px; font-weight: bold; color: #667eea;">Totales</div>
</div>
</div>
</div> tbody tr:hover {
background: #e3f2fd !important;
transition: background 0.3s;
} 🔀 Atributos colspan y rowspan
Section titled “🔀 Atributos colspan y rowspan”Combinar celdas para crear estructuras complejas.
colspan - Combinar columnas
Section titled “colspan - Combinar columnas”Sintaxis:
<td colspan="número">Contenido</td>Ejemplos:
<!-- Celda que ocupa 2 columnas --><table> <tr> <td colspan="2">Ocupa 2 columnas</td> </tr> <tr> <td>Columna 1</td> <td>Columna 2</td> </tr></table>
<!-- Encabezado que abarca todas las columnas --><table> <tr> <th colspan="4">Título Principal</th> </tr> <tr> <th>Col 1</th> <th>Col 2</th> <th>Col 3</th> <th>Col 4</th> </tr> <tr> <td>A</td> <td>B</td> <td>C</td> <td>D</td> </tr></table>
<!-- Combinación en el pie --><table> <tr> <td>Producto 1</td> <td>$10</td> <td>5</td> <td>$50</td> </tr> <tr> <td>Producto 2</td> <td>$20</td> <td>3</td> <td>$60</td> </tr> <tr> <td colspan="3">Total</td> <td>$110</td> </tr></table>Casos de uso:
- ✅ Títulos que abarcan varias columnas
- ✅ Totales y subtotales
- ✅ Agrupación de columnas relacionadas
- ✅ Celdas de resumen
rowspan - Combinar filas
Section titled “rowspan - Combinar filas”Sintaxis:
<td rowspan="número">Contenido</td>Ejemplos:
<!-- Celda que ocupa 2 filas --><table> <tr> <td rowspan="2">Ocupa 2 filas</td> <td>Fila 1</td> </tr> <tr> <td>Fila 2</td> </tr></table>
<!-- Encabezado de fila --><table> <tr> <th rowspan="3">Categoría A</th> <td>Item 1</td> <td>$10</td> </tr> <tr> <td>Item 2</td> <td>$20</td> </tr> <tr> <td>Item 3</td> <td>$30</td> </tr></table>
<!-- Agrupación de datos --><table> <tr> <td rowspan="2">Enero</td> <td>Semana 1</td> <td>$1,000</td> </tr> <tr> <td>Semana 2</td> <td>$1,200</td> </tr> <tr> <td rowspan="2">Febrero</td> <td>Semana 1</td> <td>$1,100</td> </tr> <tr> <td>Semana 2</td> <td>$1,300</td> </tr></table>Casos de uso:
- ✅ Encabezados de fila que agrupan datos
- ✅ Categorías que abarcan múltiples filas
- ✅ Datos jerárquicos
- ✅ Comparaciones verticales
Combinar colspan y rowspan
Section titled “Combinar colspan y rowspan”<!-- Tabla compleja --><table> <tr> <th colspan="3">Reporte de Ventas</th> </tr> <tr> <th rowspan="2">Región</th> <th colspan="2">Trimestre 1</th> </tr> <tr> <th>Enero</th> <th>Febrero</th> </tr> <tr> <td>Norte</td> <td>$10,000</td> <td>$12,000</td> </tr> <tr> <td>Sur</td> <td>$8,000</td> <td>$9,000</td> </tr> <tr> <td colspan="1">Total</td> <td>$18,000</td> <td>$21,000</td> </tr></table>
<!-- Horario escolar --><table> <tr> <th>Hora</th> <th>Lunes</th> <th>Martes</th> <th>Miércoles</th> </tr> <tr> <td>8:00 - 9:00</td> <td rowspan="2">Matemáticas</td> <td>Historia</td> <td>Ciencias</td> </tr> <tr> <td>9:00 - 10:00</td> <td>Inglés</td> <td>Arte</td> </tr> <tr> <td>10:00 - 11:00</td> <td colspan="3">Recreo</td> </tr></table>Tips importantes:
Cálculo de celdas:
<!-- ✅ CORRECTO: 3 celdas en cada fila --><tr> <td colspan="2">2 columnas</td> <td>1 columna</td></tr>
<!-- ❌ INCORRECTO: Número inconsistente --><tr> <td colspan="2">2 columnas</td> <td>1 columna</td> <td>1 columna</td> <!-- Sobra una celda --></tr> 🎨 Colspan y Rowspan
<div style="font-family: Arial, sans-serif; max-width: 900px; margin: 0 auto; padding: 30px; background: #f8f9fa;">
<h2 style="color: #2c3e50; margin: 0 0 25px 0;">Ejemplos de Colspan y Rowspan</h2>
<!-- Ejemplo 1: Colspan -->
<div style="margin-bottom: 30px;">
<h3 style="color: #34495e; margin: 0 0 15px 0;">📊 Colspan - Combinar Columnas</h3>
<table style="width: 100%; border-collapse: collapse; background: white; box-shadow: 0 2px 8px rgba(0,0,0,0.1); border-radius: 8px; overflow: hidden;">
<tr>
<th colspan="4" style="padding: 15px; background: #3498db; color: white; font-size: 18px;">
Reporte Anual 2024
</th>
</tr>
<tr style="background: #34495e; color: white;">
<th style="padding: 12px;">Trimestre</th>
<th style="padding: 12px;">Ventas</th>
<th style="padding: 12px;">Gastos</th>
<th style="padding: 12px;">Ganancia</th>
</tr>
<tr style="border-bottom: 1px solid #ecf0f1;">
<td style="padding: 12px;">Q1</td>
<td style="padding: 12px; text-align: right;">$50,000</td>
<td style="padding: 12px; text-align: right;">$30,000</td>
<td style="padding: 12px; text-align: right; color: #27ae60; font-weight: bold;">$20,000</td>
</tr>
<tr style="background: #f8f9fa; border-bottom: 1px solid #ecf0f1;">
<td style="padding: 12px;">Q2</td>
<td style="padding: 12px; text-align: right;">$60,000</td>
<td style="padding: 12px; text-align: right;">$35,000</td>
<td style="padding: 12px; text-align: right; color: #27ae60; font-weight: bold;">$25,000</td>
</tr>
<tr style="background: #667eea; color: white; font-weight: bold;">
<td colspan="3" style="padding: 12px; text-align: right;">TOTAL:</td>
<td style="padding: 12px; text-align: right; font-size: 16px;">$45,000</td>
</tr>
</table>
</div>
<!-- Ejemplo 2: Rowspan -->
<div style="margin-bottom: 30px;">
<h3 style="color: #34495e; margin: 0 0 15px 0;">📈 Rowspan - Combinar Filas</h3>
<table style="width: 100%; border-collapse: collapse; background: white; box-shadow: 0 2px 8px rgba(0,0,0,0.1); border-radius: 8px; overflow: hidden;">
<tr style="background: #34495e; color: white;">
<th style="padding: 12px;">Categoría</th>
<th style="padding: 12px;">Producto</th>
<th style="padding: 12px;">Precio</th>
</tr>
<tr style="border-bottom: 1px solid #ecf0f1;">
<th rowspan="3" style="padding: 12px; background: #e8f4f8; color: #2c3e50; vertical-align: middle; font-weight: 600;">
💻 Electrónica
</th>
<td style="padding: 12px;">Laptop</td>
<td style="padding: 12px; text-align: right; color: #27ae60; font-weight: bold;">$800</td>
</tr>
<tr style="background: #f8f9fa; border-bottom: 1px solid #ecf0f1;">
<td style="padding: 12px;">Mouse</td>
<td style="padding: 12px; text-align: right; color: #27ae60; font-weight: bold;">$25</td>
</tr>
<tr style="border-bottom: 1px solid #ecf0f1;">
<td style="padding: 12px;">Teclado</td>
<td style="padding: 12px; text-align: right; color: #27ae60; font-weight: bold;">$50</td>
</tr>
<tr style="border-bottom: 1px solid #ecf0f1;">
<th rowspan="2" style="padding: 12px; background: #fef5e7; color: #2c3e50; vertical-align: middle; font-weight: 600;">
📚 Libros
</th>
<td style="padding: 12px;">HTML5</td>
<td style="padding: 12px; text-align: right; color: #27ae60; font-weight: bold;">$30</td>
</tr>
<tr>
<td style="padding: 12px;">JavaScript</td>
<td style="padding: 12px; text-align: right; color: #27ae60; font-weight: bold;">$35</td>
</tr>
</table>
</div>
<!-- Ejemplo 3: Combinación -->
<div>
<h3 style="color: #34495e; margin: 0 0 15px 0;">🎯 Colspan + Rowspan Combinados</h3>
<table style="width: 100%; border-collapse: collapse; background: white; box-shadow: 0 2px 8px rgba(0,0,0,0.1); border-radius: 8px; overflow: hidden;">
<tr>
<th colspan="4" style="padding: 15px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; font-size: 18px;">
📅 Horario Semanal
</th>
</tr>
<tr style="background: #34495e; color: white;">
<th style="padding: 12px;">Hora</th>
<th style="padding: 12px;">Lunes</th>
<th style="padding: 12px;">Martes</th>
<th style="padding: 12px;">Miércoles</th>
</tr>
<tr style="border-bottom: 1px solid #ecf0f1;">
<td style="padding: 12px; background: #f8f9fa; font-weight: 600;">8:00 - 9:00</td>
<td rowspan="2" style="padding: 12px; background: #e3f2fd; text-align: center; vertical-align: middle; font-weight: 600; color: #1976d2;">
Matemáticas
</td>
<td style="padding: 12px; text-align: center;">Historia</td>
<td style="padding: 12px; text-align: center;">Ciencias</td>
</tr>
<tr style="border-bottom: 1px solid #ecf0f1;">
<td style="padding: 12px; background: #f8f9fa; font-weight: 600;">9:00 - 10:00</td>
<td style="padding: 12px; text-align: center;">Inglés</td>
<td style="padding: 12px; text-align: center;">Arte</td>
</tr>
<tr>
<td style="padding: 12px; background: #f8f9fa; font-weight: 600;">10:00 - 11:00</td>
<td colspan="3" style="padding: 12px; background: #fff3cd; text-align: center; font-weight: 600; color: #856404;">
☕ Recreo
</td>
</tr>
</table>
</div>
</div> td:hover {
background: #e8f5e9 !important;
transition: background 0.3s;
} 🎨 Estilos Recomendados para Tablas
Section titled “🎨 Estilos Recomendados para Tablas”CSS moderno para tablas profesionales y responsive.
CSS fundamental para tablas
Section titled “CSS fundamental para tablas”/* Tabla básica */table { width: 100%; border-collapse: collapse; /* Elimina espacio entre bordes */ font-family: Arial, sans-serif;}
/* Celdas */th, td { padding: 12px 15px; text-align: left; border: 1px solid #ddd;}
/* Encabezados */th { background-color: #3498db; color: white; font-weight: bold; text-transform: uppercase;}
/* Filas alternas (zebra striping) */tbody tr:nth-child(odd) { background-color: #f8f9fa;}
tbody tr:nth-child(even) { background-color: white;}
/* Hover en filas */tbody tr:hover { background-color: #e3f2fd; transition: background-color 0.3s;}Tabla con bordes redondeados
Section titled “Tabla con bordes redondeados”table { border-collapse: separate; border-spacing: 0; border-radius: 10px; overflow: hidden; box-shadow: 0 4px 12px rgba(0,0,0,0.1);}
th:first-child { border-top-left-radius: 10px;}
th:last-child { border-top-right-radius: 10px;}Tabla sin bordes (clean)
Section titled “Tabla sin bordes (clean)”table { border-collapse: collapse;}
th, td { padding: 15px; border: none; border-bottom: 1px solid #ecf0f1;}
th { background-color: transparent; color: #2c3e50; border-bottom: 2px solid #3498db;}
tbody tr:last-child td { border-bottom: none;}Técnica 1: Scroll horizontal
Section titled “Técnica 1: Scroll horizontal”<div class="table-container"> <table> <!-- Contenido de la tabla --> </table></div>.table-container { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; /* Smooth scroll en iOS */}
table { min-width: 600px; /* Ancho mínimo */}
/* Indicador de scroll */.table-container { position: relative;}
.table-container::after { content: '→ Desliza →'; position: absolute; right: 0; top: 50%; transform: translateY(-50%); background: rgba(0,0,0,0.7); color: white; padding: 5px 10px; border-radius: 5px; font-size: 12px; pointer-events: none; opacity: 0; transition: opacity 0.3s;}
.table-container:hover::after { opacity: 1;}Técnica 2: Tabla apilada (mobile)
Section titled “Técnica 2: Tabla apilada (mobile)”/* Desktop */table { width: 100%; border-collapse: collapse;}
/* Mobile: Apilar celdas */@media (max-width: 768px) { table, thead, tbody, th, td, tr { display: block; }
thead { display: none; /* Ocultar encabezados */ }
tr { margin-bottom: 15px; border: 1px solid #ddd; border-radius: 8px; padding: 10px; }
td { text-align: right; padding: 10px; border: none; border-bottom: 1px solid #eee; position: relative; padding-left: 50%; }
td::before { content: attr(data-label); position: absolute; left: 10px; font-weight: bold; text-align: left; }
td:last-child { border-bottom: none; }}<!-- HTML con data-label --><table> <thead> <tr> <th>Nombre</th> <th>Email</th> <th>Teléfono</th> </tr> </thead> <tbody> <tr> <td data-label="Nombre">Juan Pérez</td> <td data-label="Email">juan@ejemplo.com</td> <td data-label="Teléfono">123-456-789</td> </tr> </tbody></table>Técnica 3: Tabla con scroll fijo de encabezados
Section titled “Técnica 3: Tabla con scroll fijo de encabezados”.table-wrapper { max-height: 400px; overflow-y: auto; position: relative;}
table { width: 100%; border-collapse: collapse;}
thead { position: sticky; top: 0; z-index: 10; background-color: #3498db;}
th { padding: 15px; color: white; box-shadow: 0 2px 4px rgba(0,0,0,0.1);}Tabla con gradientes
Section titled “Tabla con gradientes”table { border-collapse: collapse; width: 100%; box-shadow: 0 4px 12px rgba(0,0,0,0.15); border-radius: 12px; overflow: hidden;}
thead { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);}
th { padding: 18px; color: white; font-weight: 600; text-transform: uppercase; letter-spacing: 1px;}
tbody tr { border-bottom: 1px solid #ecf0f1; transition: all 0.3s;}
tbody tr:hover { background: linear-gradient(90deg, #e3f2fd 0%, #f3e5f5 100%); transform: scale(1.02); box-shadow: 0 4px 8px rgba(0,0,0,0.1);}
td { padding: 15px;}Tabla con iconos y badges
Section titled “Tabla con iconos y badges”/* Badge en celdas */.badge { display: inline-block; padding: 4px 12px; border-radius: 20px; font-size: 12px; font-weight: bold; text-transform: uppercase;}
.badge-success { background-color: #d4edda; color: #155724;}
.badge-warning { background-color: #fff3cd; color: #856404;}
.badge-danger { background-color: #f8d7da; color: #721c24;}
/* Iconos en encabezados */th i { margin-right: 8px;}
/* Alineación de números */.text-right { text-align: right;}
.text-center { text-align: center;}<table> <thead> <tr> <th><i>👤</i> Usuario</th> <th><i>📧</i> Email</th> <th><i>✅</i> Estado</th> </tr> </thead> <tbody> <tr> <td>Juan Pérez</td> <td>juan@ejemplo.com</td> <td><span class="badge badge-success">Activo</span></td> </tr> <tr> <td>María García</td> <td>maria@ejemplo.com</td> <td><span class="badge badge-warning">Pendiente</span></td> </tr> </tbody></table>Tabla con ordenamiento visual
Section titled “Tabla con ordenamiento visual”/* Indicador de columna ordenable */th.sortable { cursor: pointer; user-select: none; position: relative; padding-right: 30px;}
th.sortable::after { content: '⇅'; position: absolute; right: 10px; opacity: 0.5;}
th.sortable:hover::after { opacity: 1;}
th.sorted-asc::after { content: '↑'; opacity: 1; color: #3498db;}
th.sorted-desc::after { content: '↓'; opacity: 1; color: #3498db;} 🎨 Estilos de tablas
<div style="font-family: Arial, sans-serif; max-width: 1000px; margin: 0 auto; padding: 30px; background: #f8f9fa;">
<h2 style="color: #2c3e50; margin: 0 0 30px 0;">Estilos Modernos de Tablas</h2>
<!-- Tabla 1: Gradiente -->
<div style="margin-bottom: 40px;">
<h3 style="color: #34495e; margin: 0 0 15px 0;">🌈 Tabla con Gradiente</h3>
<table style="width: 100%; border-collapse: collapse; box-shadow: 0 4px 12px rgba(0,0,0,0.15); border-radius: 12px; overflow: hidden;">
<thead style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
<tr>
<th style="padding: 18px; color: white; font-weight: 600; text-align: left;">👤 Usuario</th>
<th style="padding: 18px; color: white; font-weight: 600; text-align: left;">📧 Email</th>
<th style="padding: 18px; color: white; font-weight: 600; text-align: center;">✅ Estado</th>
<th style="padding: 18px; color: white; font-weight: 600; text-align: right;">💰 Saldo</th>
</tr>
</thead>
<tbody style="background: white;">
<tr style="border-bottom: 1px solid #ecf0f1; transition: all 0.3s;" onmouseover="this.style.background='linear-gradient(90deg, #e3f2fd 0%, #f3e5f5 100%)'" onmouseout="this.style.background='white'">
<td style="padding: 15px;">Juan Pérez</td>
<td style="padding: 15px;">juan@ejemplo.com</td>
<td style="padding: 15px; text-align: center;">
<span style="display: inline-block; padding: 4px 12px; border-radius: 20px; font-size: 12px; font-weight: bold; background: #d4edda; color: #155724;">ACTIVO</span>
</td>
<td style="padding: 15px; text-align: right; font-weight: bold; color: #27ae60;">$1,250.00</td>
</tr>
<tr style="border-bottom: 1px solid #ecf0f1; transition: all 0.3s;" onmouseover="this.style.background='linear-gradient(90deg, #e3f2fd 0%, #f3e5f5 100%)'" onmouseout="this.style.background='white'">
<td style="padding: 15px;">María García</td>
<td style="padding: 15px;">maria@ejemplo.com</td>
<td style="padding: 15px; text-align: center;">
<span style="display: inline-block; padding: 4px 12px; border-radius: 20px; font-size: 12px; font-weight: bold; background: #fff3cd; color: #856404;">PENDIENTE</span>
</td>
<td style="padding: 15px; text-align: right; font-weight: bold; color: #27ae60;">$850.50</td>
</tr>
<tr style="transition: all 0.3s;" onmouseover="this.style.background='linear-gradient(90deg, #e3f2fd 0%, #f3e5f5 100%)'" onmouseout="this.style.background='white'">
<td style="padding: 15px;">Carlos López</td>
<td style="padding: 15px;">carlos@ejemplo.com</td>
<td style="padding: 15px; text-align: center;">
<span style="display: inline-block; padding: 4px 12px; border-radius: 20px; font-size: 12px; font-weight: bold; background: #f8d7da; color: #721c24;">INACTIVO</span>
</td>
<td style="padding: 15px; text-align: right; font-weight: bold; color: #7f8c8d;">$0.00</td>
</tr>
</tbody>
</table>
</div>
<!-- Tabla 2: Minimalista -->
<div style="margin-bottom: 40px;">
<h3 style="color: #34495e; margin: 0 0 15px 0;">✨ Tabla Minimalista</h3>
<table style="width: 100%; border-collapse: collapse; background: white; box-shadow: 0 2px 8px rgba(0,0,0,0.1); border-radius: 8px; overflow: hidden;">
<thead>
<tr style="border-bottom: 2px solid #3498db;">
<th style="padding: 15px; text-align: left; color: #2c3e50; font-weight: 600;">Producto</th>
<th style="padding: 15px; text-align: center; color: #2c3e50; font-weight: 600;">Cantidad</th>
<th style="padding: 15px; text-align: right; color: #2c3e50; font-weight: 600;">Precio</th>
</tr>
</thead>
<tbody>
<tr style="border-bottom: 1px solid #ecf0f1; transition: background 0.3s;" onmouseover="this.style.background='#f8f9fa'" onmouseout="this.style.background='white'">
<td style="padding: 15px;">💻 Laptop Dell XPS</td>
<td style="padding: 15px; text-align: center;">2</td>
<td style="padding: 15px; text-align: right; font-weight: bold;">$1,600.00</td>
</tr>
<tr style="border-bottom: 1px solid #ecf0f1; transition: background 0.3s;" onmouseover="this.style.background='#f8f9fa'" onmouseout="this.style.background='white'">
<td style="padding: 15px;">🖱️ Mouse Logitech</td>
<td style="padding: 15px; text-align: center;">5</td>
<td style="padding: 15px; text-align: right; font-weight: bold;">$125.00</td>
</tr>
<tr style="transition: background 0.3s;" onmouseover="this.style.background='#f8f9fa'" onmouseout="this.style.background='white'">
<td style="padding: 15px;">⌨️ Teclado Mecánico</td>
<td style="padding: 15px; text-align: center;">3</td>
<td style="padding: 15px; text-align: right; font-weight: bold;">$450.00</td>
</tr>
</tbody>
<tfoot>
<tr style="background: #34495e; color: white; font-weight: bold;">
<td colspan="2" style="padding: 15px; text-align: right;">TOTAL:</td>
<td style="padding: 15px; text-align: right; font-size: 18px;">$2,175.00</td>
</tr>
</tfoot>
</table>
</div>
<!-- Tabla 3: Zebra Striping -->
<div>
<h3 style="color: #34495e; margin: 0 0 15px 0;">🦓 Tabla con Filas Alternas</h3>
<table style="width: 100%; border-collapse: collapse; box-shadow: 0 2px 8px rgba(0,0,0,0.1); border-radius: 8px; overflow: hidden;">
<thead style="background: #2c3e50; color: white;">
<tr>
<th style="padding: 15px; text-align: left;">Mes</th>
<th style="padding: 15px; text-align: right;">Ventas</th>
<th style="padding: 15px; text-align: right;">Gastos</th>
<th style="padding: 15px; text-align: right;">Ganancia</th>
</tr>
</thead>
<tbody>
<tr style="background: white; transition: background 0.3s;" onmouseover="this.style.background='#e3f2fd'" onmouseout="this.style.background='white'">
<td style="padding: 12px;">Enero</td>
<td style="padding: 12px; text-align: right;">$50,000</td>
<td style="padding: 12px; text-align: right;">$30,000</td>
<td style="padding: 12px; text-align: right; color: #27ae60; font-weight: bold;">$20,000</td>
</tr>
<tr style="background: #f8f9fa; transition: background 0.3s;" onmouseover="this.style.background='#e3f2fd'" onmouseout="this.style.background='#f8f9fa'">
<td style="padding: 12px;">Febrero</td>
<td style="padding: 12px; text-align: right;">$55,000</td>
<td style="padding: 12px; text-align: right;">$32,000</td>
<td style="padding: 12px; text-align: right; color: #27ae60; font-weight: bold;">$23,000</td>
</tr>
<tr style="background: white; transition: background 0.3s;" onmouseover="this.style.background='#e3f2fd'" onmouseout="this.style.background='white'">
<td style="padding: 12px;">Marzo</td>
<td style="padding: 12px; text-align: right;">$60,000</td>
<td style="padding: 12px; text-align: right;">$35,000</td>
<td style="padding: 12px; text-align: right; color: #27ae60; font-weight: bold;">$25,000</td>
</tr>
<tr style="background: #f8f9fa; transition: background 0.3s;" onmouseover="this.style.background='#e3f2fd'" onmouseout="this.style.background='#f8f9fa'">
<td style="padding: 12px;">Abril</td>
<td style="padding: 12px; text-align: right;">$58,000</td>
<td style="padding: 12px; text-align: right;">$33,000</td>
<td style="padding: 12px; text-align: right; color: #27ae60; font-weight: bold;">$25,000</td>
</tr>
</tbody>
</table>
</div>
</div> 🎓 Resumen
Section titled “🎓 Resumen”🚀 Próximos pasos
Section titled “🚀 Próximos pasos”Ahora que dominas las tablas, aprenderás:
- ✅ Formularios en HTML
- ✅ Campos de entrada
- ✅ Validación de formularios
- ✅ Botones y controles
🐝