← Back to Bootstrap Course | Chapter 4: Colors & Spacing | Lesson 1 of 7

Background colors

Background utilities colour an element's background using the theme.

In this page:

  1. Background colors
Syntax
markup
<div class="bg-primary text-white p-3">content</div>
<div class="bg-success-subtle">content</div>

Background colors

Use .bg-primary, .bg-secondary, .bg-success, .bg-danger, .bg-warning, .bg-info, .bg-light and .bg-dark. Subtle variants such as .bg-primary-subtle are softer and support dark mode. .bg-gradient adds a subtle gradient.

Pair with text-* or text-bg-* for readable contrast.

Note: text-bg-primary sets both background and a readable text colour.

Example: Background colors

markup
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<div class="container">
  <div class="p-3 mb-2 bg-primary text-white">bg-primary</div>
  <div class="p-3 mb-2 bg-success-subtle text-success-emphasis">bg-success-subtle</div>
  <div class="p-3 mb-2 text-bg-dark">text-bg-dark</div>
  <div class="p-3 mb-2 bg-warning bg-gradient">bg-warning with gradient</div>
</div>
</body>
</html>
Live Example
Related Topics
Common Mistakes
  1. Using dark text on dark backgrounds
  2. Forgetting text colour changes
  3. Custom CSS for standard colours
Chapter Summary
  • bg-{theme} sets a background
  • -subtle variants are softer
  • bg-gradient adds a gradient
  • Pair with readable text colours
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.