29

I want to create a website theme based on Bootstrap. I want to extend Bootstrap's default components and change some of them. For this, I would need access to SASS variables that Bootstrap defines so I can override them.

I though of cloning Bootstrap from GitHub and changing it directly, but I heard that's actually not good practice. Could you give me some advice please?

Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
Victor
  • 13,914
  • 19
  • 78
  • 147

4 Answers4

72

Update 2022 (Bootstrap 5)

Generally speaking, customizing Bootstrap 5 SASS works the same way as it did in Bootstrap 4. However, some of the maps have changed (and new ones have been added) so you should follow the Bootstrap SASS documentation for details on how to add, remove or modify any of the maps.

Recent comments on this post indicate there's still some confusion about the order of customizations and imports. However, this concept hasn't changed...

"Variable overrides must come after our functions are imported, but before the rest of the imports"

Because of the way SASS variable defaults work, bootstrap ("the rest of the imports") should be imported AFTER any variable customizations/changes. Since your variable changes will not contain the !default flag, your changes will not be overridden by the Bootstrap defaults when bootstrap is imported at the end.

The proof is in the pudding


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...

|-- \bootstrap
|   |-- \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

Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
  • 1
    How do you approach avoiding writing BS classes in HTML? Say I want only custom classes: ```.title``` for example could be written in SCSS as ```.title { @extend .display-3; @extend .text-primary; } ``` But ```@extend``` is supposed to be cursed https://www.sitepoint.com/avoid-sass-extend/ so am not sure if this is a good idea? I do not want ANY Bootstrap classes in my HTML. – trainoasis Sep 06 '19 at 08:38
  • What happens if I set `$body-color: green;` without importing the `bootstrap/variables.scss` file before it. It wont work? – lordvcs Jun 17 '20 at 17:40
  • Given the file structure, wouldn't it be : ```@import "bootstrap/scss/functions"; @import "bootstrap/scss/variables"; @import "bootstrap/scss/bootstrap";``` ? – Fritzip Feb 04 '21 at 10:03
  • @Fritzip No it depends. Those are relative paths so they are relative to however bootstrap is included/installed in your SASS compiler. – Carol Skelly Feb 04 '21 at 12:10
  • Please see that this is almost correct, but the imports (of things like variables, functions, etc.) must come AFTER the setting of the overrides if using Bootstrap 4. https://github.com/twbs/bootstrap-npm-starter/issues/43 – Case Silva Nov 10 '21 at 20:12
  • No, because if you need to reference an existing variable you must import the variables first. What you're saying only works IF you don't need to reference/use another bootstrap variable. This is widely documented as the correct method for both Bootstrap 4 & 5 https://getbootstrap.com/docs/5.1/customize/sass/#importing – Carol Skelly Nov 12 '21 at 12:10
  • This is not a solution because all this does is override the stuff in the bootstrap files. This happens because "@import bootstrap" just reimports the same files over again and overrides it. I do not believe this is the way the utilities API is supposed to work. – AndrewBrntt Jan 04 '22 at 01:46
  • @AndrewBrntt - no, `@import bootstrap` doesn't override any prior overrides/changes .. read how SASS [variable defaults work](https://getbootstrap.com/docs/5.1/customize/sass/#variable-defaults). When you remove the !default flag in your customizations the variable gets set and subsequent !default flags (as set in bootstrap) will not override the changes., "Variable overrides must come after our functions are imported, but *before* the rest of the imports." – Carol Skelly Jan 04 '22 at 13:39
  • `@extend` comes with major performance implications and shouldn't be used – Charles Fries Aug 17 '22 at 19:58
5

I will explain from scratch: Download Ruby because sass is written in Ruby. Open the “start command prompt with Ruby” and enter this:

gem install sass

In macos, ruby is already installed. so in terminal:

sudo gem install sass

If you did install bootstrap through package manager; write this code into main.scss

@import "../node_modules/bootstrap/scss/bootstrap";

then navigate to your work directory where you keep your main.scss or main.css files. Enter this code into command line:

sass main.scss main.css

this will initialize compiling sass to css.

If you did download bootstrap source files, find the “scss” folder in your source folder

BOOTSTRAP\bootstrap-4.2.1\bootstrap-4.2.1\scss

and copy it into your style folder in your project where u keep all styling files:css,scss,bootstrap then import “bootstrap.scss” file into main.scss

@import "./scss/bootstrap.scss";

And now initialize compiling by entering this code:

sass main.scss main.css

Finally, you can type:

sass --watch main.scss:main.css //this should be running

to watch all changes in main.css DONT FORGET: You will write your css codes into main.scss, sass will compile it to main.css and you will LINK main.css file in your html.

If you are using visual studio code, you can install “Live Sass Compiler” extension and down below you will see “Watch Sass” button. create main.scss file in the style folder and after import the files the way I explained above, click on the “watch Sass” button and it will start compiling.

Now in bootstrap folder, under scss folder there is _variables.scss folder. this contains bootstrap variables. let's say you wanna change border-radius. in main.scss file, before importing your bootstrap:

$border-radius:10rem
@import "./scss/bootstrap.scss";
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
2

It took me a bit to piece together an adequate tutorial to install and use sass with homebrew to edit bootstrap 4 in a node/express project.

Install sass using homebrew

brew install sass/sass/sass

Install bootstrap in your project

npm install bootstrap --save

Change to assets dir, create sass directory, create app and custom file

cd public/assets/css && mkdir sass && touch sass/app.scss sass/custom.scss

In app.scss add the following

// import bootstrap files
@import "../../../../node_modules/bootstrap/scss/functions";
// import bootstrap defaults
@import "../../../../node_modules/bootstrap/scss/variables";
// import customization
@import "custom.scss";
// finally, import Bootstrap to set the changes!
@import "../../../../node_modules/bootstrap/scss/bootstrap";

In custom.scss add a sample customization

$hotness: #fb3f7c;
$link-color: $hotness;

Run sass; automatically export/update .css file

sass --watch sass/app.scss sass/bootstrap.css

Link to the new bootstrap file in your html

References

ow3n
  • 5,974
  • 4
  • 53
  • 51
1

Github version of Bootstrap always changes since the foundation itself is being updated constantly. Not to mention, the way Bootstrap is structured, it doesn't necessarily compile the way your SASS compiler will export. The best practice is to structure it the way best suitable for you.

Here's how I usually structure my entire project

/style/
/style/theme.scss
/style/theme.min.css
/style/bootstrap.scss
/style/bootstrap.min.css
/style/bootstrap/{subfiles}
/style/bootstrap/mixins/{subfiles}
/style/bootstrap/utilities/{subfiles}

/scripts/
/scripts/bootstrap.min.js
/scripts/main.min.js
/scripts/bootstrap/

This assumes you have sass and js minifier.