bootstrap
  1. bootstrap-background-colors

Bootstrap Background Colors

Bootstrap provides several classes to change the background colors of your elements easily.

Syntax

To apply a background color in Bootstrap, you can use the following classes:

  • Primary: bg-primary
  • Secondary: bg-secondary
  • Success: bg-success
  • Danger: bg-danger
  • Warning: bg-warning
  • Info: bg-info
  • Light: bg-light
  • Dark: bg-dark
  • etc..

You can also use additional classes with these background color classes to change the opacity or gradient of the background.

Example

Here's an example of using the background color in Bootstrap

<div class="p-3 mb-2 bg-primary text-white">.bg-primary</div>
<div class="p-3 mb-2 bg-primary-subtle text-emphasis-primary">.bg-primary-subtle</div>
<div class="p-3 mb-2 bg-secondary text-white">.bg-secondary</div>
<div class="p-3 mb-2 bg-secondary-subtle text-emphasis-secondary">.bg-secondary-subtle</div>
<div class="p-3 mb-2 bg-success text-white">.bg-success</div>
<div class="p-3 mb-2 bg-success-subtle text-emphasis-success">.bg-success-subtle</div>
<div class="p-3 mb-2 bg-danger text-white">.bg-danger</div>
<div class="p-3 mb-2 bg-danger-subtle text-emphasis-danger">.bg-danger-subtle</div>
<div class="p-3 mb-2 bg-warning text-dark">.bg-warning</div>
<div class="p-3 mb-2 bg-warning-subtle text-emphasis-warning">.bg-warning-subtle</div>
<div class="p-3 mb-2 bg-info text-dark">.bg-info</div>
<div class="p-3 mb-2 bg-info-subtle text-emphasis-info">.bg-info-subtle</div>
<div class="p-3 mb-2 bg-light text-dark">.bg-light</div>
<div class="p-3 mb-2 bg-light-subtle text-emphasis-light">.bg-light-subtle</div>
<div class="p-3 mb-2 bg-dark text-white">.bg-dark</div>
<div class="p-3 mb-2 bg-dark-subtle text-emphasis-dark">.bg-dark-subtle</div>
<p class="p-3 mb-2 bg-body-secondary">.bg-body-secondary</p>
<p class="p-3 mb-2 bg-body-tertiary">.bg-body-tertiary</p>

<div class="p-3 mb-2 bg-body text-body">.bg-body</div>
<div class="p-3 mb-2 bg-black text-white">.bg-black</div>
<div class="p-3 mb-2 bg-white text-dark">.bg-white</div>
<div class="p-3 mb-2 bg-transparent text-body">.bg-transparent</div>
Try Playground

Background gradient

By adding a .bg-gradient class, a linear gradient is added as background image to the backgrounds.

<div class="p-3 mb-2 bg-primary bg-gradient text-white">.bg-primary</div>
<div class="p-3 mb-2 bg-secondary bg-gradient text-white">.bg-secondary</div>
<div class="p-3 mb-2 bg-success bg-gradient text-white">.bg-success</div>
<div class="p-3 mb-2 bg-danger bg-gradient text-white">.bg-danger</div>
<div class="p-3 mb-2 bg-warning bg-gradient text-dark">.bg-warning</div>
<div class="p-3 mb-2 bg-info bg-gradient text-dark">.bg-info</div>
<div class="p-3 mb-2 bg-light bg-gradient text-dark">.bg-light</div>
<div class="p-3 mb-2 bg-dark bg-gradient text-white">.bg-dark</div>
Try Playground

Opacity

<div class="bg-success p-2 text-white">This is default success background</div>
<div class="bg-success p-2 text-white bg-opacity-75">This is 75% opacity success background</div>
<div class="bg-success p-2 text-dark bg-opacity-50">This is 50% opacity success background</div>
<div class="bg-success p-2 text-dark bg-opacity-25">This is 25% opacity success background</div>
<div class="bg-success p-2 text-dark bg-opacity-10">This is 10% opacity success background</div>
Try Playground

Explanation

In the code above, we used the .bg-primary, .bg-success, .bg-danger etc. classes to set the background color of the div as blue, green, red, etc. We also used the .text-white class to set the text color as white, which is more visible against a dark background.

Use

Background colors are essential for creating visually appealing websites. With Bootstrap, you can easily add background colors to any element on your website and make them stand out. You can use different background colors for different sections of your website, such as header, footer, or sections with specific content.

Important Points

  • Bootstrap provides several background color classes to apply different colors to your elements.
  • You can use additional classes to change the opacity or gradient of the background color.
  • Background colors are essential for creating visually appealing websites and can add personality to your website design.

Summary

With Bootstrap, you can easily add background colors to any element on your website and make them stand out. By using the .bg-* classes, you can apply different background colors to your elements quickly and efficiently. Background colors are an essential part of website design, and they can add personality and flair to your website.

Published on: