Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 58 ยท Content

Colors and Backgrounds

Learn Bootstrap 5.3 color utilities: theme colors, text-* and bg-* classes, subtle and emphasis variants, opacity, gradients and link colors, with live examples.

Color is the fastest way to give a page meaning. A red message says "something went wrong", a green one says "done", and a muted gray says "this is secondary". Bootstrap packages a small, consistent palette and exposes it through utility classes, so you can color text and backgrounds without writing a single line of CSS.

In this lesson you will use the theme colors, the text-* and bg-* utilities, the subtle and emphasis variants that arrived in Bootstrap 5.3, opacity classes, gradients and the new link utilities. If you have not yet seen how utility classes fit together, read Your First Bootstrap Page first, and see Text Utilities for the non-color text helpers.

The theme colors

Bootstrap defines eight theme colors. Each has a name, and that name appears inside many class names:

Name Typical meaning
primary main brand or action color
secondary less important actions and text
success positive result
danger error or destructive action
warning caution
info neutral information
light very light surface
dark very dark surface

Behind each name sits a CSS variable such as --bs-primary. The classes text-primary, bg-primary, border-primary (see Borders, Radius and Shadows), btn-primary and alert-primary all read from that one source. Change the variable and every use follows, which is the idea behind Customizing with CSS Variables.

flowchart LR
  V["--bs-primary<br/>(CSS variable)"] --> T["text-primary"]
  V --> B["bg-primary"]
  V --> N["btn-primary"]
  V --> A["alert-primary"]

One color value feeds many classes, so a theme stays consistent.

Text colors with text-*

Add text- plus a name to color the text of an element:

Theme text colors

Two things are worth noting. First, text-body-secondary and text-body-tertiary replace the old text-muted class from Bootstrap 4, which is deprecated in 5.3. They also adapt to dark mode (see Color Modes and Dark Mode). Second, text-warning and text-info are bright colors and can be hard to read on white. A color class changes appearance, not readability, so check contrast yourself (more in Accessibility in Bootstrap).

Background colors with bg-*

The same names work with bg-. Bootstrap does not pick a text color for you, so bg-dark with default dark text is unreadable. Combine the two:

Backgrounds and the text-bg helper

The text-bg-* helpers (for example text-bg-primary) set a background and choose a foreground that contrasts with it. Prefer them to pairing bg-* and text-* by hand. Other background classes worth knowing are bg-white, bg-black, bg-transparent and bg-body.

Color alone must not carry meaning. Roughly one in twelve men has some form of color vision deficiency, and screen readers do not see color at all. If a red box means "error", also say "Error" in words or add an icon with a text alternative.

Subtle and emphasis variants (Bootstrap 5.3)

Bootstrap 5.3 added a gentler set of colors that work together as a trio:

  • bg-*-subtle: a pale tinted background.
  • border-*-subtle: a matching soft border.
  • text-*-emphasis: a darker text color that reads well on the subtle background.
Subtle backgrounds with emphasis text

These combinations are easier on the eyes than solid colored blocks and keep good contrast, which is why Bootstrap's own alerts use them in 5.3. In dark mode the subtle and emphasis colors swap to dark-friendly values automatically.

Opacity

Colors are written as rgba() with an opacity variable, so you can fade them with opacity classes:

  • text-opacity-25, -50, -75, -100 on elements that have a text-* color.
  • bg-opacity-10, -25, -50, -75, -100 on elements that have a bg-* color.
Fading text and backgrounds

Notice that bg-opacity-* only changes the background; the text inside stays fully opaque. That is the difference from the general opacity-* utilities, which fade the whole element, children included.

Gradients

Add bg-gradient next to a bg-* class and Bootstrap lays a soft white-to-transparent gradient over the color. It is subtle by design:

bg-gradient

For any other gradient, write your own linear-gradient() in CSS. bg-gradient is only a finishing touch.

Links are blue and underlined by default. Bootstrap 5.3 adds utilities to change that without custom CSS:

Class Effect
link-primary, link-danger, ... colors the link and darkens it on hover
link-body-emphasis link in the strong body color, good for navigation
link-opacity-50, link-opacity-75-hover fades the link or only on hover
link-underline-opacity-0, link-underline-opacity-25-hover fades the underline
link-offset-2 moves the underline away from the text
text-decoration-none, link-underline-primary removes or recolors the underline
Link utilities

Keep an underline on links inside running text. Without it, people cannot tell a link from ordinary words unless they can see color differences well.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which class gives an element a pale success-colored background in Bootstrap 5.3?

    The subtle variants are named bg-{color}-subtle. The other names do not exist in Bootstrap.
  2. 2. Why use text-bg-primary instead of bg-primary alone?

    bg-primary only sets the background. The text-bg-* helpers also choose a readable foreground color.
  3. 3. What does bg-opacity-25 do together with bg-danger?

    bg-opacity-* changes the alpha of the background color only. The general opacity-* classes would fade the whole element.
  4. 4. Which class replaces the deprecated text-muted from Bootstrap 4?

    text-body-secondary is the 5.3 muted text color, and it also adapts to dark mode.

Summary

  • Eight theme colors (primary to dark) feed text-*, bg-*, btn-* and more from one CSS variable each.
  • bg-* does not set a readable text color; use text-bg-* or pair classes yourself.
  • Bootstrap 5.3 adds bg-*-subtle, border-*-subtle and text-*-emphasis, which also follow dark mode.
  • text-opacity-* and bg-opacity-* fade a single color; bg-gradient adds a soft sheen.
  • Link utilities (link-*, link-opacity-*, link-underline-*, link-offset-*) restyle links without custom CSS.
  • Never rely on color alone to convey meaning.

Next lesson: Images and Figures.