How to Use CSS Variables in Twitter-Bootstrap 4 $Theme-Colors Array

Bootstrap v4: What's the point of theme-color vs. standard color variable?

theme-colors() is mostly used by Bootstrap internally to iterate all of the theme colors. There are a bunch of components like buttons, badges, alerts, etc.. that are "built" in CSS for each theme color. Instead of doing..

   .badge-danger {
@include badge-variant($danger);
}
.badge-warning {
@include badge-variant($warning);
}
...(repeat for each color)

This is much easier...

@each $color, $value in $theme-colors {
.badge-#{$color} {
@include badge-variant($value);
}
}

This would also make it easier to utilize the when extending or customizing Bootstrap (like this for example) with new elements that need use of all the theme colors.

But, if you're "not doing any fancy SASS stuff in my custom file, just standard CSS with these variables" it would be easiest to just reference the color variable...

$primary, $gray-300, etc...

Related: How to change the bootstrap primary color?

Bootstrap 5 - Custom theme-colors not updating classes

Bootstrap 5.1.0

A recent change to the way the text- and bg- classes are created requires additional SASS map merges in 5.1.0

@import "functions";
@import "variables";
@import "mixins";

$tertiary: #3fb247;

$custom-theme-colors:map-merge($theme-colors, (
"tertiary": $tertiary
));

$theme-colors: map-merge($theme-colors, $custom-theme-colors);
$theme-colors-rgb: map-loop($theme-colors, to-rgb, "$value");
$utilities-colors: map-merge($utilities-colors, $theme-colors-rgb);
$utilities-text-colors: map-loop($utilities-colors, rgba-css-var, "$key", "text");
$utilities-bg-colors: map-loop($utilities-colors, rgba-css-var, "$key", "bg");

@import "bootstrap";

Bootstrap 5.0.2

You need to add a "tertiary" var to the theme-colors map using map-merge if you want it to generate classes like bg-tertiary, btn-tertiary, etc...

@import "functions";
@import "variables";
@import "mixins";

$tertiary: #3fb247;

$theme-colors:map-merge($theme-colors, (
"tertiary": $tertiary
));

@import "bootstrap";

Demo

Bootstrap: setting custom colors

You can overwrite bootstrap styles by adding "!important;"
For example in your html you might have something like this:

<button type="button" class="btn btn-primary">Primary</button>

You can change the color like this:

<button type="button" class="btn" style="background-color: #009c68!important;">Primary</button>

In CSS, the !important means that “this is important”, ignore all the subsequent rules, and apply !important rule. For more information you can go here.

If this answer does not work with your given situation, I would suggest looking into Bootstrap Theming. Documentation explaining how to customize Bootstrap 4 and modify existing colors can be found here.

How to create new set of color styles in Bootstrap 4 with sass

There is not an all inclusive color mixin, so I think you need to use the mixins individually...

.btn-custom {
@include button-variant(#cece19, #222222, #cece19);
}

.btn-custom-outline {
@include button-outline-variant(#222222);
}

.card-custom {
@include card-variant(#aa9999, #997777)
}

http://www.codeply.com/go/MKGQCrLwDs


Update: In Bootstrap 4, you now can create a new custom "theme-color" to as explained in my answer here. This will allow you to use the color anywhere such as btn-custom, text-custom, bg-custom, etc...

Update Bootstrap 4.1

The button mixins have changed slightly and now require add'l params...

.btn-custom {
@include button-variant(#cece19, #222222, #cece19, #cece19, #cece19, #cece19);
}

.btn-custom-outline {
@include button-outline-variant(#222222, #cece19, #222222, #cece19);
}

@import "bootstrap";

Specify hover colours directly in customized Bootstrap 4

The mixin used to generate the button css colour states is called button-variant() which is located in bootstrap/scss/mixins.

This sass mixin does have the parameter capability for handling custom .btn hover and active colour states...

@mixin button-variant(
$background,
$border,
$hover-background: darken($background, 7.5%),
$hover-border: darken($border, 10%),
$active-background: darken($background, 10%),
$active-border: darken($border, 12.5%)
) {
//...
}

But as you can see the hover and active params are $default: params using sass colour darken() function as values to calculate the passed $theme-colors colour value to automatically generate colours for the states.

This makes it impossible to handle button state colours by just using $theme-colors.







There are a few ways to customise the .btn active and hover state
colours whilst minimising compiled css and avoiding
duplicate/overriding selectors in compiled css output.

First method (easiest)

See scss below which handles bootstrap vendor @imports individually and handling custom vars and map-removals in the correct order, so bootstrap compiles and uses variables correctly.

Follow comments in the scss so you know what is happening...

// import initial bootstrap vendors

@import "~bootstrap/scss/functions";
@import "~bootstrap/scss/variables";
@import "~bootstrap/scss/mixins";

// remove unused default bootstrap colours from bs colour maps to prevent larger compiled css file sizes
// this will also speed up compiling time as you are not generating css for all bootstrap standard colours
// if you wish to keep some standard bootstrap colours then remove the ones you want to keep from the map-remove lists below

$colors: map-remove($colors, "blue","indigo","purple","pink","red","orange","yellow","green","teal","cyan","white","gray","gray-dark");
$grays: map-remove($grays, "100","200","300","400","500","600","700","800","900");
$theme-colors: map-remove($theme-colors, "primary","secondary","success","danger","warning","info","light","dark");

// use sass @debug to check dump logs of your colour maps after modifying them if you wish...

// @debug $colors;
// @debug $grays;
// @debug $theme-colors;

// now add your custom colour variables

$new-color-1: #00a89c;
$new-color-2: #e61b53;

// now add your custom colour maps using above variables

$colors: (
"newcolor1": $new-color-1,
"newcolor2": $new-color-2
);

// you can always tidy this up by including these variables and any other bootstrap theming override variables into a separate sass _vars.scss file
// and then import _vars.scss here (before root.scss and after mixins.scss|map-removals)...

//@import "./vars";

// now import the rest of your needed bootstrap scss vendor files below this
// the below imports will now use any variable overrides you've defined above

// if you want to reduce you css output file size and speed up compiling
// you can remove unused scss vendor imports below, for example i've removed breadcrumb, toasts and carousel

@import "~bootstrap/scss/root";
@import "~bootstrap/scss/reboot";
@import "~bootstrap/scss/type";
@import "~bootstrap/scss/images";
@import "~bootstrap/scss/code";
@import "~bootstrap/scss/grid";
@import "~bootstrap/scss/tables";
@import "~bootstrap/scss/forms";
@import "~bootstrap/scss/buttons";
@import "~bootstrap/scss/transitions";
@import "~bootstrap/scss/dropdown";
@import "~bootstrap/scss/button-group";
@import "~bootstrap/scss/input-group";
@import "~bootstrap/scss/custom-forms";
@import "~bootstrap/scss/nav";
@import "~bootstrap/scss/navbar";
@import "~bootstrap/scss/card";
//@import "~bootstrap/scss/breadcrumb";
@import "~bootstrap/scss/pagination";
@import "~bootstrap/scss/badge";
@import "~bootstrap/scss/jumbotron";
@import "~bootstrap/scss/alert";
@import "~bootstrap/scss/progress";
@import "~bootstrap/scss/media";
@import "~bootstrap/scss/list-group";
@import "~bootstrap/scss/close";
//@import "~bootstrap/scss/toasts";
@import "~bootstrap/scss/modal";
@import "~bootstrap/scss/tooltip";
@import "~bootstrap/scss/popover";
//@import "~bootstrap/scss/carousel";
@import "~bootstrap/scss/spinners";
@import "~bootstrap/scss/utilities";
@import "~bootstrap/scss/print";

Because we emptied the $theme-colors map, bootstrap will not compile @each loops which utilise $theme-colors because it is now empty.

We can now manually use button-variant() mixin inside any .btn-* selector to create custom buttons without add extra css to the compiled output.

This option is easiest because you can pass any colour you wish to the button-variant() params.

.btn-primary {
@include button-variant(
$new-color-1, // background
$new-color-1, // border
$new-color-1, // hover background (customise how you wish)
$new-color-1, // hover border (customise how you wish)
$new-color-1, // active background (customise how you wish)
$new-color-1 // active border (customise how you wish)
);
}

.btn-secondary {
@include button-variant(
$new-color-2, // background
$new-color-2, // border
$new-color-2, // hover background (customise how you wish)
$new-color-2, // hover border (customise how you wish)
$new-color-2, // active background (customise how you wish)
$new-color-2 // active border (customise how you wish)
);
}






Second method (trickier)

Using the above code, re-introduce $theme-colors and modify/override the @include button-variant() in mixins.scss.

Removed the custom button selectors from the above code before continuing.

This method is trickier to precisely control hover and active state colours because @include button-variant() will use the same param scss colour adjustments for each passed colour.

You can try this... re-introduce $theme-colors before root.scss and immediately after $colors map array.

$theme-colors: (
"primary": $new-color-1,
"secondary": $new-color-2
);

Then create an overriding button mixin to handle all button variant calls.

Add this bootstrap v4.5.3 modified button-variant mixin after our @import "~bootstrap/scss/mixins"; and before the map-removals.

You can put this in a sass file called _mixins.scss in your project and import like this @import "./mixins"; to keep things tidy.

You can also keep all your custom mixins for this project in "./mixins" too.

Now we can modify the custom @mixin button-variant() parameters to handle passed $theme-colors.

// i've customised params in the button-variant() mixin below as an example
// see original default mixin params as comments below

// $background
// $border
// $hover-background: darken($background, 7.5%)
// $hover-border: darken($border, 10%)
// $active-background: darken($background, 10%)
// $active-border: darken($border, 12.5%)

@mixin button-variant(
$background,
$border,
$hover-background: lighten($background, 10%),
$hover-border: lighten($border, 10%),
$active-background: lighten($background, 2.5%),
$active-border: lighten($border, 2.5%)
) {
color: color-yiq($background);
@include gradient-bg($background);
border-color: $border;
@include box-shadow($btn-box-shadow);

@include hover() {
color: color-yiq($hover-background);
@include gradient-bg($hover-background);
border-color: $hover-border;
}

&:focus,
&.focus {
color: color-yiq($hover-background);
@include gradient-bg($hover-background);
border-color: $hover-border;
@if $enable-shadows {
@include box-shadow($btn-box-shadow, 0 0 0 $btn-focus-width rgba(mix(color-yiq($background), $border, 15%), .5));
} @else {
// Avoid using mixin so we can pass custom focus shadow properly
box-shadow: 0 0 0 $btn-focus-width rgba(mix(color-yiq($background), $border, 15%), .5);
}
}

// Disabled comes first so active can properly restyle
&.disabled,
&:disabled {
color: color-yiq($background);
background-color: $background;
border-color: $border;
// Remove CSS gradients if they're enabled
@if $enable-gradients {
background-image: none;
}
}

&:not(:disabled):not(.disabled):active,
&:not(:disabled):not(.disabled).active,
.show > &.dropdown-toggle {
color: color-yiq($active-background);
background-color: $active-background;
@if $enable-gradients {
background-image: none; // Remove the gradient for the pressed/active state
}
border-color: $active-border;

&:focus {
@if $enable-shadows and $btn-active-box-shadow != none {
@include box-shadow($btn-active-box-shadow, 0 0 0 $btn-focus-width rgba(mix(color-yiq($background), $border, 15%), .5));
} @else {
// Avoid using mixin so we can pass custom focus shadow properly
box-shadow: 0 0 0 $btn-focus-width rgba(mix(color-yiq($background), $border, 15%), .5);
}
}
}
}


Related Topics



Leave a reply



Submit