How to change background color of table rows or individual cells in Bootstrap ?
In this article, we will discuss setting the background color of the table rows & how to set the background color for an individual cell using Bootstrap. Bootstrap provides a series of classes that can be used to apply various styling to the tables such as changing the heading appearance, making the rows stripped, adding or removing borders, making rows hoverable, etc. Bootstrap also provides classes for making tables responsive.
The purpose of creating the table is to display the complex & large-size data in a simple compact structured format that provides informative content while looking at a glance. This will save time to read & analyze unstructured large data. Bootstrap helps to create & decorate the content in a standard manner. We will use bootstrap build-in classes for creating the table structure. A simple HTML table can be created using below syntax:
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML course.
<table class="table"> Table Contents... <table>
Before we proceed to our main discussion, the knowledge of creating tables using HTML will help you to understand the article in a better way. Please refer to Bootstrap 4 | Tables for many other cases of tables in bootstrap.
Bootstrap CDN Link: Let’s take an example to understand how to set up & add a Bootstrap CDN link to apply the bootstrap pre-defined classes for creating the table.
<link rel=”stylesheet” href=”https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css”>
We have added the bootstrap CDN link in our code that makes a standard & nicely structured content table. Using pre-defined classes, we can change the color of table cells and table rows. In order to change the color of the table row, we need to specify in <tr> tag with the required class & for changing the color of the table row then specify it inside <td> tag with the required class. Let us learn classes one by one.
Predefined Classes: For changing the color of a row or whole table, we will use any one of the following classes.
- table-active: This class applies to the hover color of the table row or cell with grey color.
- table-default: This class is applied to change the color to white when the default row is selected.
- table-primary: This class indicates the important action.
- table-secondary: This class indicates the less important activities.
- table-success: This class indicates the positive or successful action.
- table-danger: This class indicates a potentially negative or dangerous action.
- table-warning: This class indicates a warning that might need attention.
- table-info: This class indicates a neutral informative change or action.
- table-light: This class is applied for table row background or light grey table.
- table-dark: This class is applied for a dark grey table.
Example: In this case, we have used all the pre-defined classes to change the color of the rows.
For changing the color of any particular cell, you can use any one of the following classes:
- bg-primary: Applies to indicate positive or successful action.
- bg-success: Applies to indicate a successful or positive action.
- bg-warning: Applies to indicate a warning that might need attention
- bg-danger: Applies to indicate a potentially negative or dangerous action
- bg-info: Applies to indicate a neutral informative change or action.
In order to change the color for the particular table cell, we need to specify it inside the <td> tag for the particular cell. The below example illustrates to declare for a particular cell.