In Bootstrap 4 it is possible to change the theme colors by overriding the variables like this:
$primary: #0074d9;
$danger: #ff4136;
Or by modifying the theme-colors map like this:
$theme-colors: (
"primary": #0074d9,
"danger": #ff4136
);
When and why should we use one over the other?
Override Variables Bootstrap has its own “_variables. scss” in the “scss” folder, which contains all variables used in Bootstrap. Now, add the “abstracts” name folder in your scss folder and create “_custom-variables. scss” in that folder.
Bootstrap 4 supports 10 different color utility classes that can be used for text and background colors. Each of these colors has a name that can be used to describe the use of colors on a page.
Bootstrap includes a handful of Sass maps, key value pairs that make it easier to generate families of related CSS. We use Sass maps for our colors, grid breakpoints, and more. Just like Sass variables, all Sass maps include the ! default flag and can be overridden and extended.
In fact, it should be better to always override your theme colors in the $theme-colors
map to ensure replacing the colors from where bootstrap will use those colors later when generating the corresponding CSS. Bootstrap encourages to do so.
If you decide to override the default color definition, you will be overriding the creation of the colormap itself, so bootstrap won't have access to default colors anymore.
You could do the following:
Option 1
Override the default values (not encouraged by Bootstrap).
Option 2
Override the map, using hex values:
$theme-colors : (
'primary' : #0074d9,
'danger' : #ff4136
);
Then you could access any of your theme colors by using map_get
:
$my-color : map_get($theme-colors, 'primary'); // #0074d9
Option 3
You could define your own set of variables to override the map, then you'll always have access to your variables without using map_get
:
$my-primary-color : #0074d9;
$my-danger-color : #ff4136;
$theme-colors : (
'primary' : $my-primary-color,
'danger' : $my-danger-color
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With