Skip to main content

Helpers

Color and background

One class that sets a theme background and a text colour that passes contrast against it.

How it works

.text-bg-* combines a background utility with a text colour computed at build time by Bootstrap's color-contrast() function. The theme sets $min-contrast-ratio: 4.5 and offers two candidates: navy-900 (#0a1b3d) and cream-50 (#faf3df). Whichever contrasts better wins, so you never pick a text colour by hand.

There are eleven of them — Bootstrap's eight theme colours plus gold, navy and cream.

Primary — gold-700, navy text
Secondary — navy-400, cream text
Success — green-600, cream text
Info — navy-500, cream text
Warning — gold-300, navy text
Danger — red-600, cream text
Light — cream-50, navy text
Dark — navy-900, cream text
Gold — gold-500, navy text
Navy — navy-700, cream text
Cream — cream-100, navy text
HTML
<div class="text-bg-primary p-3 mb-2 rounded-1">Primary — gold-700, navy text</div>
<div class="text-bg-secondary p-3 mb-2 rounded-1">Secondary — navy-400, cream text</div>
<div class="text-bg-success p-3 mb-2 rounded-1">Success — green-600, cream text</div>
<div class="text-bg-info p-3 mb-2 rounded-1">Info — navy-500, cream text</div>
<div class="text-bg-warning p-3 mb-2 rounded-1">Warning — gold-300, navy text</div>
<div class="text-bg-danger p-3 mb-2 rounded-1">Danger — red-600, cream text</div>
<div class="text-bg-light p-3 mb-2 rounded-1">Light — cream-50, navy text</div>
<div class="text-bg-dark p-3 mb-2 rounded-1">Dark — navy-900, cream text</div>
<div class="text-bg-gold p-3 mb-2 rounded-1">Gold — gold-500, navy text</div>
<div class="text-bg-navy p-3 mb-2 rounded-1">Navy — navy-700, cream text</div>
<div class="text-bg-cream p-3 rounded-1">Cream — cream-100, navy text</div>

Values

The background reads the live --bs-*-rgb variable, so .text-bg-primary turns gold-500 in dark mode. The text colour is a fixed hex, chosen against the light-mode background. The only one that is not navy or cream is .text-bg-secondary, where pure white beat both candidates.

ClassBackgroundText
.text-bg-primary--bs-primary — #b8941f (dark: #d4af37)#0a1b3d
.text-bg-secondary--bs-secondary — #4a6398#faf3df
.text-bg-success--bs-success — #277432#faf3df
.text-bg-info--bs-info — #1e3a6e#faf3df
.text-bg-warning--bs-warning — #e6c769#0a1b3d
.text-bg-danger--bs-danger — #b3261e#faf3df
.text-bg-light--bs-light — #faf3df#0a1b3d
.text-bg-dark--bs-dark — #0a1b3d#faf3df
.text-bg-gold--bs-gold — #d4af37#0a1b3d
.text-bg-navy--bs-navy — #122553#faf3df
.text-bg-cream--bs-cream — #f4ecd8#0a1b3d
Both declarations carry !important. A .text-* utility on the same element will not override the text colour; wrap the text instead.

With components

Use the helper on badges and cards instead of pairing .bg-* with a guessed .text-*.

Champion Contender Gold Cream DQ
HTML
<span class="badge text-bg-primary">Champion</span>
<span class="badge text-bg-navy">Contender</span>
<span class="badge text-bg-gold">Gold</span>
<span class="badge text-bg-cream">Cream</span>
<span class="badge text-bg-danger">DQ</span>
Main event
Steel cage, no escape

The text colour is cream-50 because navy-700 is dark. No extra class needed.

Title match
Gold on gold

Gold-500 is light enough that navy-900 text wins the contrast check.

HTML
<div class="card text-bg-navy mb-2" style="max-width: 18rem">
  <div class="card-header">Main event</div>
  <div class="card-body">
    <h5 class="card-title">Steel cage, no escape</h5>
    <p class="card-text">The text colour is cream-50 because navy-700 is dark. No extra class needed.</p>
  </div>
</div>
<div class="card text-bg-gold" style="max-width: 18rem">
  <div class="card-header">Title match</div>
  <div class="card-body">
    <h5 class="card-title">Gold on gold</h5>
    <p class="card-text">Gold-500 is light enough that navy-900 text wins the contrast check.</p>
  </div>
</div>

With background opacity

The background uses --bs-bg-opacity, so the .bg-opacity-* utilities apply. Keep in mind the text colour does not recompute — at low opacity the contrast is no longer guaranteed.

Navy, full strength
Navy, 75% — still readable
Navy, 25% — cream text on cream page. Avoid.
HTML
<div class="text-bg-navy p-3 mb-2 rounded-1">Navy, full strength</div>
<div class="text-bg-navy bg-opacity-75 p-3 mb-2 rounded-1">Navy, 75% — still readable</div>
<div class="text-bg-navy bg-opacity-25 p-3 rounded-1">Navy, 25% — cream text on cream page. Avoid.</div>

Sass

The helper is generated from $theme-colors, which scss/_maps.scss extends:

$rb-theme-colors: (
  "gold": $rb-gold-500,
  "navy": $rb-navy-700,
  "cream": $rb-cream-100,
);
$theme-colors: map-merge($theme-colors, $rb-theme-colors);

Add a key to that map and a matching .text-bg-* appears. The contrast candidates are $color-contrast-dark (navy-900) and $color-contrast-light (cream-50); see Color.