Bootstrap5 Tablas

El componente de tabla (Table) de Bootstrap 5 ofrece una serie de funciones y estilos que hacen que las tablas sean más fáciles de construir, responsivas y atractivas.

Podemos crear fácilmente tablas con diferentes estilos, funciones de ordenación, paginación y diseños responsivos.

Bootstrap5 Tabla básica

En Bootstrap 5, la estructura básica para crear una tabla es muy simple: solo necesita usar el HTML estándar<table>elemento, y añadir latableclase.

Bootstrap5 a través de.tablela clase para definir el estilo de la tabla básica, como se muestra a continuación:

Ejemplo

<table class="table"> <thead> <tr> <th>Firstname</th> <th>Lastname</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>John</td> <td>Doe</td> <td>john@example.com</td> </tr> <tr> <td>Mary</td> <td>Moe</td> <td>mary@example.com</td> </tr> <tr> <td>July</td> <td>Dooley</td> <td>july@example.com</td> </tr> </tbody> </table>

Pruébalo »

Tabla rayada

Al añadir la.table-stripedclase, verá<tbody>rayas en las filas, como se muestra en el siguiente ejemplo:

Ejemplo

<table class="table table-striped"> <thead> <tr> <th>Firstname</th> <th>Lastname</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>John</td> <td>Doe</td> <td>john@example.com</td> </tr> <tr> <td>Mary</td> <td>Moe</td> <td>mary@example.com</td> </tr> <tr> <td>July</td> <td>Dooley</td> <td>july@example.com</td> </tr> </tbody> </table>

Pruébalo »

Tabla con bordes

.table-borderedla clase puede agregar bordes a la tabla

Ejemplo

<table class="table table-bordered"> <thead> <tr> <th>Firstname</th> <th>Lastname</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>John</td> <td>Doe</td> <td>john@example.com</td> </tr> <tr> <td>Mary</td> <td>Moe</td> <td>mary@example.com</td> </tr> <tr> <td>July</td> <td>Dooley</td> <td>july@example.com</td> </tr> </tbody> </table>

Pruébalo »

Tabla con efecto hover

.table-hoverla clase puede agregar un efecto hover (fondo gris) a cada fila de la tabla:

Ejemplo

<table class="table table-hover"> <thead> <tr> <th>Firstname</th> <th>Lastname</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>John</td> <td>Doe</td> <td>john@example.com</td> </tr> <tr> <td>Mary</td> <td>Moe</td> <td>mary@example.com</td> </tr> <tr> <td>July</td> <td>Dooley</td> <td>july@example.com</td> </tr> </tbody> </table>

Pruébalo »

Tabla de fondo negro

.table-darkla clase puede agregar un fondo negro a la tabla:

Ejemplo

<table class="table table-dark"> <thead> <tr> <th>Firstname</th> <th>Lastname</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>John</td> <td>Doe</td> <td>john@example.com</td> </tr> <tr> <td>Mary</td> <td>Moe</td> <td>mary@example.com</td> </tr> <tr> <td>July</td> <td>Dooley</td> <td>july@example.com</td> </tr> </tbody> </table>

Pruébalo »

Tabla de rayas negras

Combinando las.table-darky.table-stripedclases, puede crear una tabla de rayas negras:

Ejemplo

<table class="table table-dark table-striped"> <thead> <tr> <th>Firstname</th> <th>Lastname</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>John</td> <td>Doe</td> <td>john@example.com</td> </tr> <tr> <td>Mary</td> <td>Moe</td> <td>mary@example.com</td> </tr> <tr> <td>July</td> <td>Dooley</td> <td>july@example.com</td> </tr> </tbody> </table>

Pruébalo »

Efecto hover - Tabla de fondo negro

Combinando las.table-darky.table-hoverclases, se puede configurar el efecto hover de la tabla de fondo negro:

Ejemplo

<table class="table table-dark table-hover"> <thead> <tr> <th>Firstname</th> <th>Lastname</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>John</td> <td>Doe</td> <td>john@example.com</td> </tr> <tr> <td>Mary</td> <td>Moe</td> <td>mary@example.com</td> </tr> <tr> <td>July</td> <td>Dooley</td> <td>july@example.com</td> </tr> </tbody> </table>

Pruébalo »

Tabla sin bordes

.table-borderlessla clase puede crear una tabla sin bordes:

Ejemplo

<table class="table table-borderless"> <thead> <tr> <th>Firstname</th> <th>Lastname</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>John</td> <td>Doe</td> <td>john@example.com</td> </tr> <tr> <td>Mary</td> <td>Moe</td> <td>mary@example.com</td> </tr> <tr> <td>July</td> <td>Dooley</td> <td>july@example.com</td> </tr> </tbody> </table>

Pruébalo »

Clases de color contextuales

Mediante las clases de color contextuales se puede establecer el color de las filas o celdas de la tabla:

Ejemplo

<table class="table"> <thead> <tr> <th>Firstname</th> <th>Lastname</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>Default</td> <td>Defaultson</td> <td>def@somemail.com</td> </tr> <tr class="table-primary"> <td>Primary</td> <td>Joe</td> <td>joe@example.com</td> </tr> <tr class="table-success"> <td>Success</td> <td>Doe</td> <td>john@example.com</td> </tr> <tr class="table-danger"> <td>Danger</td> <td>Moe</td> <td>mary@example.com</td> </tr> <tr class="table-info"> <td>Info</td> <td>Dooley</td> <td>july@example.com</td> </tr> <tr class="table-warning"> <td>Warning</td> <td>Refs</td> <td>bo@example.com</td> </tr> <tr class="table-active"> <td>Active</td> <td>Activeson</td> <td>act@example.com</td> </tr> <tr class="table-secondary"> <td>Secondary</td> <td>Secondson</td> <td>sec@example.com</td> </tr> <tr class="table-light"> <td>Light</td> <td>Angie</td> <td>angie@example.com</td> </tr> <tr class="table-dark text-dark"> <td>Dark</td> <td>Bo</td> <td>bo@example.com</td> </tr> </tbody> </table>

Pruébalo »

La siguiente tabla enumera las descripciones de las clases de color de la tabla:

Nombre de clase Descripción
.table-primary Azul: especifica que es una operación importante
.table-success Verde: especifica que es una operación permitida
.table-danger Rojo: especifica que es una operación peligrosa
.table-info Azul claro: indica que el contenido ha cambiado
.table-warning Naranja: indica una operación que requiere atención
.table-active Gris: se utiliza para el efecto hover
.table-secondary Gris: indica que el contenido no es muy importante
.table-light Gris claro, puede ser el fondo de una fila de la tabla
.table-dark Gris oscuro, puede ser el fondo de una fila de la tabla

Colores de la cabecera

También podemos establecer el color de la cabecera, por ejemplo.table-darkla clase se usa para agregar un fondo negro a la cabecera,.table-lightla clase se usa para agregar un fondo gris a la cabecera:

Ejemplo

<table class="table"> <thead class="table-dark"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>John</td> <td>Doe</td> <td>john@example.com</td> </tr> <tr> <td>Mary</td> <td>Moe</td> <td>mary@example.com</td> </tr> <tr> <td>July</td> <td>Dooley</td> <td>july@example.com</td> </tr> </tbody> </table> <table class="table"> <thead class="table-light"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>John</td> <td>Doe</td> <td>john@example.com</td> </tr> <tr> <td>Mary</td> <td>Moe</td> <td>mary@example.com</td> </tr> <tr> <td>July</td> <td>Dooley</td> <td>july@example.com</td> </tr> </tbody> </table>

Pruébalo »

Tabla más pequeña

.table-smla clase se usa para crear una tabla más pequeña reduciendo el padding interno:

Ejemplo

<table class="table table-bordered table-sm"> <thead> <tr> <th>Firstname</th> <th>Lastname</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>John</td> <td>Doe</td> <td>john@example.com</td> </tr> <tr> <td>Mary</td> <td>Moe</td> <td>mary@example.com</td> </tr> <tr> <td>July</td> <td>Dooley</td> <td>july@example.com</td> </tr> </tbody> </table>

Pruébalo »

Tabla responsiva

.table-responsivela clase se usa para crear tablas responsivas: cuando el ancho de pantalla es menor a 992px se crea una barra de desplazamiento horizontal; si el ancho del área visible es mayor a 992px se muestra un efecto diferente (sin barra de desplazamiento):

Ejemplo

<div class="table-responsive"> <table class="table"> <thead> <tr> <th>#</th> <th>Firstname</th> <th>Lastname</th> <th>Age</th> <th>City</th> <th>Country</th> <th>Sex</th> <th>Example</th> <th>Example</th> <th>Example</th> <th>Example</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Anna</td> <td>Pitt</td> <td>35</td> <td>New York</td> <td>USA</td> <td>Female</td> <td>Yes</td> <td>Yes</td> <td>Yes</td> <td>Yes</td> </tr> </tbody> </table> </div>

Pruébalo »

Puede usar las siguientes clases para mostrar una barra de desplazamiento en un ancho de pantalla específico:

Nombre de clase Ancho de pantalla
.table-responsive-sm < 576px
.table-responsive-md < 768px
.table-responsive-lg < 992px
.table-responsive-xl < 1200px
.table-responsive-xxl < 1400px

Ejemplo

<div class="table-responsive-sm"> <table class="table"> ... </table> </div>

Pruébalo »
Otras extensiones