Here's how to override / customize Bootstrap 4 with SASS...
Overrides and customizations should be kept in a separate custom.scss
file that is separate from the Bootstrap SASS source files. This way any changes you make don't impact the Bootstrap source, which makes changes or upgrading Bootstrap later much easier.
1- Consider Bootstrap's SASS folder structure, alongside your custom.scss
...
|-- ootstrap
| |-- scss
| | |-- mixins
| | |-- utilities
| | |-- bootstrap.scss
| | |-- variables.scss
| | |-- functions.scss
| | |-- ...more bootstrap scss files
| custom.scss
2- In your custom.scss
, import the Bootstrap files that are needed for the overrides. (Usually, this is just variables.scss
. In some cases, with more complex cutomizations, you may also need the functions
, mixins
, and other Bootstrap files.). Make the changes, then @import "bootstrap"
. It's important to import Bootstrap after the changes...
/* custom.scss */
/* import the necessary Bootstrap files */
@import "bootstrap/functions";
@import "bootstrap/variables";
/* make changes to the !default Bootstrap variables */
$body-color: green;
/* finally, import Bootstrap to set the changes! */
@import "bootstrap";
2a (optional) - Also, you can extend existing Bootstrap classes after the @import "bootstrap";
to create new custom classes. For example, here is a new .row-dark
class that extends (inherits from) the Bootstrap .row
class and then add a background-color.
/* create new custom classes from existing classes */
.row-dark {
@extend .row;
background-color: #333333;
color: #ffffff;
}
3- Compile the SASS (node-sass, gulp-sass, webpack/NPM, etc..). The CSS output will contain the overrides! Don't forget to check the includePaths if your @imports fail. For a full list of variables you can override, see the variables.scss file. There are also these global variables.
Bootstrap SASS Demo on Codeply
In summary, here's how it works:
1_ First, when the custom.scss
file is processed using SASS, the !default values are defined in the bootstrap/variables.scss
.?
2_ Next, our custom values are set, which will override any of the variables that had !default values set in bootstrap/variables.scss
.?
3_ Finally, Bootstrap is imported (@import "bootstrap"
) which enables the SASS processor (A.K.A. compiler) to generate all the appropriate CSS using both the Bootstrap defaults and the custom overrides.
For those that don't know SASS, try this tool that I made.
Also see:
How to get 15 columns in Bootstrap 4 in SASS CSS?
Bootstrap v4 grid sizes / Sass List
Customizing Bootstrap CSS template
Extending Bootstrap 4 and SASS