0

We are trying to use dynamic colors coming from our backend to replace Bootstrap’s primary and secondary colors at runtime. Our approach is to map Bootstrap’s $primary and $secondary SCSS variables to CSS custom properties so we can update them in the browser:

$primary: var(--bs-primaryColor) !default;
$secondary: var(--bs-secondaryColor) !default;

$theme-colors: (
  "primary": $primary,
  "secondary": $secondary,
);

@function theme-color($key: "primary") {
  @return map.get($theme-colors, $key);
}

:root {
  @each $color, $value in $theme-colors {
    --#{$color}: #{$value};
  }
}

However, when compiling, Bootstrap’s functions like shade-color() or mix() throw an error:

X [ERROR] $color2: var(--bs-primary) is not a color.
   212 │   @return mix(black, $color, $weight);

Steps to reproduce

  1. In an Angular project with Bootstrap SCSS, set $primary: var(--bs-primaryColor) before importing Bootstrap.
  2. Import Bootstrap: @import "bootstrap/scss/bootstrap";
  3. Run the build (ng serve or sass).
  4. Compilation fails with the above error.

What we expected

We expected Bootstrap to allow var(--bs-primary) as a valid $primary so we can update theme colors dynamically via JavaScript (document.documentElement.style.setProperty('--bs-primaryColor', '#hexvalue');).

What actually happens

The Sass compiler treats var(--bs-primaryColor) as a string instead of a color, so functions like mix(), shade-color(), and tint-color() fail.

Environment

  • Bootstrap version: [5.3.3]
  • Framework: Angular 19
  • Build tools: Angular CLI

Additional context

We’ve tried workarounds like color-mix() in pure CSS and runtime shading in JavaScript, but those require rewriting Bootstrap utilities and components manually.

Here is repro on stackblitz

Please provide a recommended approach for dynamic theming without recompiling Bootstrap?

6
  • please replicate the issue on this stackblitz Commented Sep 16 at 15:46
  • It'll be useful if you provide a playground for reproducing this. Follow these guidelines to create a minimal reproducible example. Commented Sep 16 at 16:51
  • @NarenMurali, UncleBigBay here is stackblitz Commented Sep 16 at 17:18
  • It's not possible (check this a bit old dicussion in github. The resumen is "You can think of it like the difference between server-side JavaScript and client-side JavaScript. Sass is a server-side version of CSS. While Sass acts as a proving-ground for many new CSS features (including variables!), Sass itself does not run in the browser, and therefor cannot be dynamic. That's not something that we can change." Commented Sep 17 at 15:32
  • you can create differents "bootstraps.css" and add dinamically like this SO Commented Sep 17 at 15:34

1 Answer 1

0

Declaring classes using bootstrap variables:

Technically you are not overriding any bootstrap variables, but declaring other variables using the already present variables. So you can import the variables and bootstrap functions then construct the classes.

@import 'bootstrap/scss/functions';
@import 'bootstrap/scss/variables';
@import 'bootstrap/scss/variables-dark';

$primary: var(--bs-primaryColor) !default;
$secondary: var(--bs-secondaryColor) !default;
$theme-colors: (
  'primary': $primary,
  'secondary': $secondary,
);

@function theme-color($key: 'primary') {
  @return map.get($theme-colors, $key);
}

:root {
  @each $color, $value in $theme-colors {
    --#{$color}: #{$value};
  }
}

@import 'bootstrap/scss/bootstrap';

Stackblitz Demo


Overriding the bootstrap variables:

By meaning of overriding variables, it would be declaring the variables before the bootstrap import. Bootstrap variables are declared with !default, so if there is already a value present, then that value is taken instead of the default value set by bootstrap.

$primary: #ff0000;

@import "bootstrap/scss/bootstrap";

Stackblitz Demo

Sign up to request clarification or add additional context in comments.

3 Comments

reason to keep @import 'bootstrap/scss/bootstrap'; at bottom is I want to override bootstrap primary / secondary color theme with my custom primary secondary color - Bootstrap Saas.
updated my answer
my usecase is dynamic overriding of bootstrap variables (all places like btn-primary, bg-primary it should use my primarycolor) here is updated stackblitz

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.