HTML & CSS

How to Change the Background Color in Bootstrap 5 with examples

Description

Bootstrap is a popular CSS framework that helps developers to create responsive and mobile-first websites quickly. Bootstrap 5 is the latest version of this framework, and it comes with many features that make website development easier than ever before. In this article, we will discuss how to change the background color in Bootstrap 5.

Bootstrap 5 comes with a variety of built-in classes that can be used to style HTML elements. One of the most commonly used classes is the “bg-” class, which is used to set the background color of an element. There are several options available for this class, such as “bg-primary”, “bg-secondary”, “bg-success”, “bg-danger”, “bg-warning”, “bg-info”, and “bg-light”. You can also use the “bg-dark” class to set a dark background color.




To change the background color of an element in Bootstrap 5, you need to add the “bg-” class followed by the color option to the HTML element. For example, if you want to set the background color of a div element to blue, you can use the following code:

in this example, we have used the “bg-primary” class to set the background color of the div element to blue. You can use any of the other available color options to set the background color of the element.

If you want to create a custom background color, you can use the “bg-” class followed by a hexadecimal color code. For example, if you want to set the background color of an element to a light green color, you can use the following code:

In this example, we have used the hexadecimal color code #98FB98 to set the background color of the div element to a light green color. You can use any other hexadecimal color code to set the background color of the element.

Additionally, you can also use the “bg-gradient-” class to set a gradient background color. Bootstrap 5 provides several options for gradient background colors, such as “bg-gradient-primary”, “bg-gradient-secondary”, “bg-gradient-success”, “bg-gradient-danger”, “bg-gradient-warning”, “bg-gradient-info”, and “bg-gradient-light”. You can use any of these classes to set a gradient background color.



Changing the Background Color in Bootstrap 5

There are 10 classes to change the background color in Bootstrap 5:

.bg-primary

.bg-secondary

.bg-success

.bg-danger

.bg-warning

.bg-info

.bg-light

.bg-dark

.bg-white

.bg-transparent

By changing the background color, the text color doesn’t immediately follow. We need to use a combination of color and background color if we want to change the text color and background color at the same time. Here’s an example:

Background Color in Bootstrap 5

In this example I combined the class text-color with bg-color . Notice that Bootstrap has a ” theme ” of its own, whereby if that class contains the word primary, it will be blue, as well as all other colors.



Make Background Color with Gradient

Bootstrap 5 adds a new class to add a gradient effect to the background color. To do this, add the .bg-gradient class like the following example:

Background Color in Bootstrap 5

With the addition of the .bg-gradient class , the background color has a gradient at the top. The resulting effect is not very visible, but if you pay close attention the color on the top side is slightly whiter than the color on the bottom side of the element.

In this tutorial, we have discussed how to change the background color using Bootstrap, including creating a gradient effect. In my previous article, I explained how to make badge buttons in Bootstrap 5.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button