Bootstrap Background Color


Background Color in Bootstrap

Bootstrap provides a range of background color utilities to easily apply color to elements. These utilities help in setting the background color of elements quickly and consistently, ensuring that your design adheres to a color scheme and maintains visual harmony.


1. Background Color Classes

Bootstrap uses a set of predefined classes for background colors. The classes follow the format bg-{color}, where {color} specifies the color.

a. Standard Colors

Bootstrap includes a set of standard colors which you can apply using classes like:

  • bg-primary: Applies the primary color (usually blue).
  • bg-secondary: Applies the secondary color (usually gray).
  • bg-success: Applies a green color, often used for success messages.
  • bg-danger: Applies a red color, often used for error or danger messages.
  • bg-warning: Applies a yellow color, often used for warnings.
  • bg-info: Applies a light blue color, often used for informational messages.
  • bg-light: Applies a light gray color.
  • bg-dark: Applies a dark gray color.
  • bg-white: Applies a white color.
  • bg-transparent: Makes the background transparent.

Example:

<div class="bg-primary text-white p-3">Primary Background</div> <div class="bg-success text-white p-3">Success Background</div>

b. Custom Colors

If you need colors beyond the standard palette, you can use custom classes or inline styles. You can define your own colors in your CSS and apply them as needed.

Example:

CSS:

.bg-custom { background-color: #ff5733; /* Custom color */ }

HTML:

<div class="bg-custom text-white p-3">Custom Background</div>

2. Background Color with Utilities

In addition to the color classes, Bootstrap also provides utility classes for background color that adapt to different contexts:

a. Background Color for Different Screen Sizes

You can apply different background colors at various screen sizes using responsive background color classes:

  • bg-sm-{color}: Background color on small screens and up.
  • bg-md-{color}: Background color on medium screens and up.
  • bg-lg-{color}: Background color on large screens and up.
  • bg-xl-{color}: Background color on extra-large screens and up.

Example:

<div class="bg-primary bg-md-secondary bg-lg-success p-3"> Primary on xs, secondary on sm and md, success on lg and up </div>

b. Background Color for Print

Bootstrap provides print-specific background color utilities:

  • bg-print-{color}: Applies background color when printing.

Example:

<div class="bg-print-light"> This background color will appear in the print version. </div>

3. Background Color with Text Color

When using background color utilities, it’s important to ensure that the text color provides enough contrast for readability. Bootstrap includes text color utilities that can be used in conjunction with background color classes:

  • text-light: Applies a light text color, useful for dark background colors.
  • text-dark: Applies a dark text color, useful for light background colors.

Example:

<div class="bg-dark text-light p-3">Dark Background with Light Text</div> <div class="bg-light text-dark p-3">Light Background with Dark Text</div>

Summary of Background Color in Bootstrap

  • Background Color Classes: Use predefined classes like bg-primary, bg-success, bg-danger to apply standard colors.
  • Custom Colors: Define custom colors in CSS and apply them using custom classes.
  • Responsive Background Colors: Use responsive classes like bg-sm-{color}, bg-md-{color} to apply different colors based on screen size.
  • Print Background Colors: Use classes like bg-print-{color} to apply background colors specifically for print.
  • Text and Background Color: Ensure good contrast between background and text colors using classes like text-light, text-dark