Bootstrap 5.3 (update 2023)
Here's an example using an existing color variable ($orange).
// required to get $orange variable
@import "functions";
@import "variables";
$primary: $orange; // set the $primary variable
// merge with existing $theme-colors map
$theme-colors: map-merge($theme-colors, (
"primary": $primary
));
// set changes
@import "bootstrap";
https://codeply.com/p/qFez3k8oIF
Bootstrap 5 (update 2021)
The SASS override method is still the same for Bootstrap 5. If you're not referencing any existing Bootstrap variables to set the the new colors, you just need to set the theme variable(s) and @import bootstrap
$primary: rebeccapurple;
@import "bootstrap"; //use full path to bootstrap.scss
https://codeply.com/p/iauLPArGqE
Bootstrap 4*
To change the primary, or any of the theme colors in Bootstrap 4 SASS, set the appropriate variables before importing bootstrap.scss. This allows your custom scss to override the !default values...
$primary: purple;
$danger: red;
@import "bootstrap";
Demo: https://codeply.com/go/f5OmhIdre3
In some cases, you may want to set a new color from another existing Bootstrap variable. For this @import the functions and variables first so they can be referenced in the customizations...
/* import the necessary Bootstrap files */
@import "bootstrap/functions";
@import "bootstrap/variables";
$theme-colors: (
primary: $purple
);
/* finally, import Bootstrap */
@import "bootstrap";
Demo: https://codeply.com/go/lobGxGgfZE
Also see: this answer, this answer or changing the button color in (CSS or SASS)
It's also possible to change the primary color with CSS only but it requires a lot of additional CSS since there are many -primary variations (btn-primary, alert-primary, bg-primary, text-primary, table-primary, border-primary, etc...) and some of these classes have slight colors variations on borders, hover, and active states. Therefore, if you must use CSS it's better to use target one component such as changing the primary button color.
Videos
Bootstrap 5.3 (update 2023)
Here's an example using an existing color variable ($orange).
// required to get $orange variable
@import "functions";
@import "variables";
$primary: $orange; // set the $primary variable
// merge with existing $theme-colors map
$theme-colors: map-merge($theme-colors, (
"primary": $primary
));
// set changes
@import "bootstrap";
https://codeply.com/p/qFez3k8oIF
Bootstrap 5 (update 2021)
The SASS override method is still the same for Bootstrap 5. If you're not referencing any existing Bootstrap variables to set the the new colors, you just need to set the theme variable(s) and @import bootstrap
$primary: rebeccapurple;
@import "bootstrap"; //use full path to bootstrap.scss
https://codeply.com/p/iauLPArGqE
Bootstrap 4*
To change the primary, or any of the theme colors in Bootstrap 4 SASS, set the appropriate variables before importing bootstrap.scss. This allows your custom scss to override the !default values...
$primary: purple;
$danger: red;
@import "bootstrap";
Demo: https://codeply.com/go/f5OmhIdre3
In some cases, you may want to set a new color from another existing Bootstrap variable. For this @import the functions and variables first so they can be referenced in the customizations...
/* import the necessary Bootstrap files */
@import "bootstrap/functions";
@import "bootstrap/variables";
$theme-colors: (
primary: $purple
);
/* finally, import Bootstrap */
@import "bootstrap";
Demo: https://codeply.com/go/lobGxGgfZE
Also see: this answer, this answer or changing the button color in (CSS or SASS)
It's also possible to change the primary color with CSS only but it requires a lot of additional CSS since there are many -primary variations (btn-primary, alert-primary, bg-primary, text-primary, table-primary, border-primary, etc...) and some of these classes have slight colors variations on borders, hover, and active states. Therefore, if you must use CSS it's better to use target one component such as changing the primary button color.
I've created this tool: https://lingtalfi.com/bootstrap4-color-generator, you simply put primary in the first field, then choose your color, and click generate.
Then copy the generated scss or css code, and paste it in a file named my-colors.scss or my-colors.css (or whatever name you want).
Once you compile the scss into css, you can include that css file AFTER the bootstrap CSS and you'll be good to go.
The whole process takes about 10 seconds if you get the gist of it, provided that the my-colors.scss file is already created and included in your head tag.
Note: this tool can be used to override bootstrap's default colors (primary, secondary, danger, ...), but you can also create custom colors if you want (blue, green, ternary, ...).
Note2: this tool was made to work with bootstrap 4 (i.e. not any subsequent version for now).
How can i use custom colors.
The documents say something about sass but i cant find anything else can you guys help me out?
Edite: i use bootstrap5
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
As of Bootstrap 5.1.3 the background color component is not automatically generated by
$utilities-bg-colors: map-loop($utilities-colors, rgba-css-var, "$key", "bg");
In the manual it is written,
Unfortunately at this time, not every component utilizes this Sass map. Future updates will strive to improve upon this. Until then, plan on making use of the ${color} variables and this Sass map.
Here is one way of manually adding .bg-* classes in Bootstrap 5.1.3. Compiled with Parcel V 2.3.2
/*Bootstrap core imports - adjust for your case*/
@import "~bootstrap/scss/functions";
@import "~bootstrap/scss/variables";
@import "~bootstrap/scss/utilities";
@import "~bootstrap/scss/mixins";
$custom-theme-colors:(
"custom-color": #8bd0da,
);
$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");
// Adjust path to bootstrap for your case
@import "~bootstrap/scss/bootstrap";
// .bg classes not automatically generated. As an example, manually add
// .bg-custom-color
.bg-custom-color{background-color: var(--bs-custom-color);};
Example html
<div class="container border mb-4 bg-primary">
Background: bg-primary
</div>
<div class="container border mb-4 bg-custom-color">
<code>bg-custom-color</code>
</div>
<div class="container mb-4">
<div class="btn btn-custom-color"><code>btn-custom-color</code></div>
</div>
<div class="container mb-4">
<div class="alert-custom-color">alert-custom-color</div>
</div>
<div class="container mb-4">
<ul class="list-group">
<li class="list-group-item-custom-color"><code>list-group-item-custom-color</code></li>
</ul>
</div>
The page render as shown below