Class 40 of 48 • Advance Architecture

Bootstrap 5 CSS Custom Properties (--bs-*) & Dynamic Theming

Prior to Bootstrap 5, theming required compiling Sass with Node.js build tools. Bootstrap 5 revolutionized frontend theming by exposing hundreds of Native CSS Custom Properties (CSS Variables) prefixed with --bs-*. This allows developers to inspect, modify, and animate design tokens in real time directly in the browser using plain CSS or JavaScript!

Bootstrap 5 Native CSS Custom Properties Runtime Architecture
Figure 40: Cascading hierarchy of CSS variables: root level variables (--bs-primary), component-scoped variables, and JS runtime overrides.

1. Root Variables vs Component Variables

Bootstrap organizes CSS custom properties into two distinct levels:

     1. Root Variables (:root): Global tokens including colors (--bs-primary, --bs-primary-rgb), font families, body backgrounds, and breakpoint values.

     2. Component-Scoped Variables: Scoped directly to individual components (such as --bs-card-bg, --bs-btn-padding-y, --bs-modal-width), allowing local overrides without affecting global styles.


2. Modifying Component Variables Locally

Customize a single card's background, border, and padding without writing new CSS class selectors:

<!-- Card customized entirely via inline CSS custom properties -->
<div class="card shadow-sm" style="--bs-card-bg: #faf8fc; --bs-card-border-color: #332246; --bs-card-spacer-y: 2rem;">
  <div class="card-body">
    <h4 class="card-title text-primary">Custom Token Card</h4>
    <p class="card-text">Locally scoped variables override Bootstrap defaults for this container only!</p>
  </div>
</div>

3. Updating Theme Tokens at Runtime with JavaScript

Allow users to select a custom brand accent color using an HTML5 color picker:

<label for="colorPicker" class="form-label">Pick Custom Theme Color:</label>
<input type="color" class="form-control form-control-color" id="colorPicker" value="#332246">

<script>
  const picker = document.getElementById('colorPicker');
  picker.addEventListener('input', (e) => {
    // Dynamically update root primary theme color across entire webpage
    document.documentElement.style.setProperty('--bs-primary', e.target.value);
  });
</script>

Explore Related Bootstrap Tutorials & Next Steps

Strengthen your responsive web design mastery by studying these connected classes:

Sass Compilation Workflow

Understand how Sass variables compile down into native CSS variables.

Color Modes via CSS Variables

Leverage root CSS variables to toggle light and dark color schemes.

Theme Colors & RGB Utilities

Use --bs-primary-rgb and rgba() for custom transparent color tints.

Manipulating CSS Vars with JS

Update CSS variables dynamically at runtime using document.documentElement.

Frequently Asked Questions (FAQ) - Student & Interview Review

CSS hex colors (#0d6efd) cannot accept alpha transparency directly in CSS variables. RGB variables store raw numbers (13, 110, 253), enabling Bootstrap to create transparent colors: rgba(var(--bs-primary-rgb), 0.5).
Yes! CSS custom properties have 100% universal support across all modern web browsers (Chrome, Edge, Firefox, Safari, iOS, and Android).
No. According to CSS specifications, CSS variables cannot be used directly inside @media query width definitions.

Next Step in Your Bootstrap Learning Path:

Advance your practical frontend engineering skills with the next topic in our synchronized curriculum.

Continue to Class 41: Utility API in Sass → Previous: ← Class 39: Sass Customization | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!