Bootstrap 4 provided classes you can use to add color depending on the context the element is being used in. Colors are the main source of conveying important information in website design.
The class names use the context that may show the purpose of the text or some other element. For example, as using the red-colored text, background or button, the class name contains the word “–danger” showing some dangerous or critical information or action. In the same way, -warning is the orange color for the context where you are alarming a visitor for certain actions. Likewise, Bootstrap 4 has the standard pattern for using class names e.g. –success, -primary, -secondary, -light, -dark, -warning, etc.
These class names may be element’s names or shortcodes. i.e. text- for text color, btn- for buttons color, bg- for backgrounds color.
Text Colors:
Bootstrap offers many classes to set the text color of an element. The text colors classes are as follows- .text-muted, .text-primary, .text-success, .text-info, .text-warning, .text-danger, .text-secondary, .text-white, .text-dark, .text-body, .text-light.
Example:
<p class="text-muted"> Muted. </p>
<p class="text-primary"> Primary. </p>
<p class="text-success"> Success. </p>
<p class="text-info"> Info. </p>
<p class="text-warning"> Warning. </p>
<p class="text-danger"> Danger. </p>
<p class="text-secondary"> Secondary.</p>
<p class="text-dark"> Dark. </p>
<p class="text-body"> Default. </p>
<p class="text-light"> Light. </p>
<p class="text-white"> White. </p>
You can also apply these bootstrap color classes to link text.
Example:
<a href="#" class="text-muted"> Muted. </a>
<a href="#" class="text-primary"> Primary. </a>
<a href="#" class="text-success"> Success. </a>
<a href="#" class="text-info"> Info. </a>
<a href="#" class="text-warning"> Warning. </a>
<a href="#" class="text-danger"> Danger. </a>
<a href="#" class="text-secondary"> Secondary.</a>
<a href="#" class="text-dark"> Dark. </a>
<a href="#" class="text-body"> Default. </a>
<a href="#" class="text-light"> Light. </a>
<a href="#" class="text-white"> White. </a>
Background Colors:
Similar to the contextual text color classes, you can easily set the background of an element to any contextual class. The background colors classes are as follows- .bg-primary, .bg-success, .bg-info, .bg-warning, .bg-danger, .bg-secondary, .bg-dark, .bg-light.
Example:
<p class="bg-primary"> Primary. </p>
<p class="bg-success"> Success. </p>
<p class="bg-info"> Info. </p>
<p class="bg-warning"> Warning. </p>
<p class="bg-danger"> Danger. </p>
<p class="bg-secondary"> Secondary.</p>
<p class="bg-dark"> Dark. </p>
<p class="bg-light"> Light. </p>
Combining Classes:
For more flexibility, you can also use Bootstrap text color and background color classes combined. In below example, we take bg-success and text-black-50 property. In text-black-50 property by adding a third parameter, modify the opacity of bootstrap colors.
Example:
<p class="bg-success text-black-50"> Green background with 50% black text </p>
<p class="bg-danger text-white-50"> red background with 50% white text </p>
<p class="bg-info text-danger"> blue background with red text </p>
Button Color
Bootstrap contextual classes are also be used to color various page elements, such as buttons. For changing the color of Bootstrap button, you should apply a btn prefix for the chosen color class.
Example:
<button class="btn btn-success" type="button"> Button </button>
<button class="btn btn-info" type="button"> Button </button>
<button class="btn btn-warning" type="button"> Button </button>
<button class="btn btn-danger" type="button"> Button </button>
<button class="btn btn-primary" type="button"> Button </button>
<button class="btn btn-secondary" type="button"> Button </button>
<button class="btn btn-light" type="button"> Button </button>
<button class="btn btn-dark" type="button"> Button </button>