html

Tables Classes in Bootstrap 5 | Explained

Tables in web design are an intelligent way to display a huge amount of data in rows and columns, especially when you are comparing objects. Styling them using CSS can be a lengthy procedure, however, Bootstrap does this task in no time. Here we will discuss various classes in Bootstrap 5 that are associated with tables and help style them in an efficient manner.

Tables with a basic style

For the purpose of giving tables, a basic style that has a certain padding and horizontal dividers use the .table class. Here is a demonstration of a basic table.

How to create a simple table using Bootstrap 5

Suppose you want to generate a simple table then this is how it is done.

HTML

<div class="container">

<table class= "table">

<thead>

<tr>

<th>Author Name</th>

<th>Category</th>

<th>Articles Published</th>

</tr>

</thead>

<tbody>

<tr>

<td>Max William</td>

<td>Computer Science</td>

<td>267</td>

</tr>

<tr>

<td>Ana James</td>

<td>Physics</td>

<td>100</td>

</tr>

<tr>

<td>Harry Robertson</td>

<td>Biology</td>

<td>150</td>

</tr>

</tbody>

</table>

</div>

In the above code, we are placing the table inside a div container and then to generate the table we are using the <table> tag and assigning it the .table class to style it. Then we are using various tags such as <thead>, <tr>, <th>, <tbody>, and <td> to create the table.

Output

The table was given a simple style.

Table with a border

For the purpose of adding a border to your table you have to use the .table-bordered class.

How to create a table with border

Let’s create a table with a border.

HTML

<div class="container">

<table class= "table table-bordered">

<thead>

<tr>

<th>Author Name</th>

<th>Category</th>

<th>Articles Published</th>

</tr>

</thead>

<tbody>

<tr>

<td>Max William</td>

<td>Computer Science</td>

<td>267</td>

</tr>

<tr>

<td>Ana James</td>

<td>Physics</td>

<td>100</td>

</tr>

<tr>

<td>Harry Robertson</td>

<td>Biology</td>

<td>150</td>

</tr>

</tbody>

</table>

</div>

The table generated here is the same as the one created above with the only difference that here apart from giving it a basic style we are also adding borders by using the .table-bordered class.

Output

Borders were added successfully to the table.

Table with no borders

Another interesting thing that you can do using Bootstrap 5 is to make completely borderless tables and for doing so you have to use the .table-borderless class.

How to create a borderless table in Bootstrap 5

Here is how you generate a table with no borders.

HTML

<div class="container">

<table class= "table table-borderless">

<thead>

<tr>

<th>Author Name</th>

<th>Category</th>

<th>Articles Published</th>

</tr>

</thead>

<tbody>

<tr>

<td>Max William</td>

<td>Computer Science</td>

<td>267</td>

</tr>

<tr>

<td>Ana James</td>

<td>Physics</td>

<td>100</td>

</tr>

<tr>

<td>Harry Robertson</td>

<td>Biology</td>

<td>150</td>

</tr>

</tbody>

</table>

</div>

The code above will generate a borderless table having a basic style i.e. it will have a certain padding and horizontal dividers but it will have no borders.

Output

The class is working properly.

Table with striped rows

In order to beautify your table by generating stripped rows use the .table-striped class.

How to create a table with striped rows

To understand the working of this class consult the example below.

HTML

<div class="container">

<table class= "table table-striped">

<thead>

<tr>

<th>Author Name</th>

<th>Category</th>

<th>Articles Published</th>

</tr>

</thead>

<tbody>

<tr>

<td>Max William</td>

<td>Computer Science</td>

<td>267</td>

</tr>

<tr>

<td>Ana James</td>

<td>Physics</td>

<td>100</td>

</tr>

<tr>

<td>Harry Robertson</td>

<td>Biology</td>

<td>150</td>

</tr>

</tbody>

</table>

</div>

The code above will generate a table having a basic style and striped rows.

Output

A table with striped rows was generated with success.

Hoverable Table

If you desire to give your rows hover state then use .table-hover class. When the user brings the mouse over on the rows, a hover effect with a gray background color will be generated.

How to create a hoverable table

Let’s create a hoverable table.

HTML

<div class="container">

<table class= "table table-hover">

<thead>

<tr>

<th>Author Name</th>

<th>Category</th>

<th>Articles Published</th>

</tr>

</thead>

<tbody>

<tr>

<td>Max William</td>

<td>Computer Science</td>

<td>267</td>

</tr>

<tr>

<td>Ana James</td>

<td>Physics</td>

<td>100</td>

</tr>

<tr>

<td>Harry Robertson</td>

<td>Biology</td>

<td>150</td>

</tr>

</tbody>

</table>

</div>

Using the code snippet above you can easily generate a simply styled table with rows having a hover effect.

Output

We have generated a hoverable table.

Table with colored rows

You can also add various colors to multiple rows in a table using the color classes provided by Bootstrap 5.

How to create a colored rows table in Bootstrap 5

Suppose you want to give each row a different color then follow the example below.

HTML

<div class="container mt-3">

<table class="table">

<thead class="table-dark">

<tr>

<th>Author Name</th>

<th>Category</th>

<th>Articles Published</th>

</tr>

</thead>

<tbody>

<tr class="table-primary">

<td>Max William</td>

<td>Computer Science</td>

<td>267</td>

</tr>

<tr class="table-success">

<td>Ana James</td>

<td>Physics</td>

<td>100</td>

</tr>

<tr class="table-info">

<td>Harry Robertson</td>

<td>Biology</td>

<td>150</td>

</tr>

</tbody>

</table>

</div>

In the code above, we are first of all giving a dark background to the table head and then each of the other rows have been assigned a different color.

Output

A table with colored rows was generated successfully.

All of the table classes associated with colors in Bootstrap 5 are given below.

Class Description
.table-primary It gives blue color to the rows representing a significant action.
.table-success It provides green color to the rows representing a successful action.
.table-info It gives light blue color to the rows representing a neutral action.
.table-warning It assigns a yellow color to the rows and indicates a warning.
.table-danger It assigns a red color to the rows and represents danger or a negative action.
.table-light It provides a light gray background to the table rows.
.table-dark It gives a dark background to the table or rows.
.table.active It provides rows the same color as the hover state.
.table-secondary It gives gray color to the rows and represents a less important action.

Responsive Tables

For the purpose of creating responsive tables that correspond to the screen width and alter their size accordingly, use the .table-responsive class.

How to create a responsive table using bootstrap 5

Suppose you want to build a responsive table for your web page.

HTML

<div class="container">

<div class="table-responsive">

<table class="table">

<thead>

<tr>

<th>Author Name</th>

<th>Category</th>

<th>Articles Published</th>

<th>Working Years</th>

<th>Qualification</th>

</tr>

</thead>

<tbody>

<tr>

<td>Max William</td>

<td>Computer Science</td>

<td>267</td>

<td>3</td>

<td>Bachelors</td>

</tr>

<tr>

<td>Ana James</td>

<td>Physics</td>

<td>100</td>

<td>1</td>

<td>Masters</td>

</tr>

<tr>

<td>Harry Robertson</td>

<td>Biology</td>

<td>150</td>

<td>2</td>

<td>Bachelors</td>

</tr>

</tbody>

</table>

</div>

</div>

In the above code, to demonstrate the concept of responsiveness we have added more rows. Along with this we have also generated an additional div container and assigned it the .table-responsive class. Moreover, the table has a basic style.

Output

The class is working properly.

Various classes that add responsiveness to the tables depending upon the screen width are provided below.

1. .table-responsive-sm

For screen width less than 576px.

2. .table-responsive-md

For screen width lower than 768px.

3. .table-responsive-lg

For screen width less than 992px.

4. .table-responsive-xl

For screen width less than 1200px.

5. .table-responsive-xxl

For screen width less than 1400px.

Conclusion

Table classes in Bootstrap 5 allow you to style tables in a very easy and quick manner. Using these classes you can give tables some padding, horizontal dividers, colors, border, hover state, etc. Moreover, you can also make your tables responsive using the classes associated with tables in Bootstrap 5. All the table classes along with relevant examples have been discussed in this report.

About the author

Naima Aftab

I am a software engineering professional with a profound interest in writing. I am pursuing technical writing as my full-time career and sharing my knowledge through my words.