Tweaking Bootstrap's Sass
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login SubscribeNow that we finally have Bootstrap's Sass working, we can celebrate! Inside _bootstrap.scss
, the first thing it imports is an _variables.scss
file. I'll hold Command
and click to open that.
Awesome! This holds a bunch of variables that are used throughout the rest of the Sass files. Yep, this means we can override these to control colors and many other things.
Copy the $brand-primary
variable line. Then, at the top of our main.scss
file, before we import Bootstrap, override that variable. Make sure to remove the default.
This variable is used to color some buttons and a lot of other things. Let's make it a little lighter: change the 6.5 to 2.5. It's not important, but the new color should be #3885C7
:
$brand-primary: darken(#428bca, 2.5%); // #3885c7 | |
// ... lines 2 - 82 |
Back in the browser, inspect the "I Lifted It!" button. Yep! The current color is #337AB7
. Now, refresh! Check it out. Yes! It's the slightly lighter #3885C7
.
So this is really the reason why you might want to import Bootstrap as Sass, instead of CSS.
Importing Only Parts of Bootstrap
Let's do one more thing! The _bootstrap.scss
file imports every part of Bootstrap. But what if you don't need every part of Bootstrap? For example, we are not using the glyphicons. Importing that CSS is wasteful!
Let's fix this! Copy the entire contents of _bootstrap.scss
. Then, in our css/
directory, create a new _bootstrap.scss
file and paste that here.
As soon as we do that... all the paths are broken! Yea, that makes sense: we're in a different directory. No problem, I'll copy "bootstrap
and do a "Find and Replace" to replace this with "~bootstrap-sass/assets/stylesheets/bootstrap
. Replace everything!
// Core variables and mixins | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/variables"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/mixins"; | |
// Reset and dependencies | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/normalize"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/print"; | |
//@import "~bootstrap-sass/assets/stylesheets/bootstrap/glyphicons"; | |
// Core CSS | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/scaffolding"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/type"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/code"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/grid"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/tables"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/forms"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/buttons"; | |
// Components | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/component-animations"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/dropdowns"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/button-groups"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/input-groups"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/navs"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/navbar"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/breadcrumbs"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/pagination"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/pager"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/labels"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/badges"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/jumbotron"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/thumbnails"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/alerts"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/progress-bars"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/media"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/list-group"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/panels"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/responsive-embed"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/wells"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/close"; | |
// Components w/ JavaScript | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/modals"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/tooltip"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/popovers"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/carousel"; | |
// Utility classes | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/utilities"; | |
@import "~bootstrap-sass/assets/stylesheets/bootstrap/responsive-utilities"; |
Yay! Happy paths!
Next, in main.scss
, import this file instead of the core one. Just, ./_bootstrap.scss.
:
// ... lines 1 - 2 | |
@import './_bootstrap.scss'; | |
// ... lines 4 - 82 |
If this is all we did, nothing would change: we're still importing everything. But now... we have power! In _bootstrap.scss
, comment out the glyphicons
line:
// ... lines 1 - 7 | |
//@import "~bootstrap-sass/assets/stylesheets/bootstrap/glyphicons"; | |
// ... lines 9 - 51 |
Cool! We don't need to restart Webpack after this change, but, stop it temporarily so that we can clean out the build/
directory:
rm -rf web/build/*
Now, re-run webpack:
./node_modules/.bin/webpack --watch
Check it out! In web/build
, we still have the Font Awesome fonts... but we do not have the glyphicon fonts anymore. That's awesome! We're not parsing the _glyphicons.scss
file anymore, so Webpack never sees those font files and so never moves them. This is proof that our CSS file is now just a little bit smaller.
Ok, let's add some sourcemaps to make debugging a lot nicer.
Hi folks,
I have phpStorm complaining about @import with `~` and marking error "Cannot resolve import into sass/scss file" when importing module assets. Any ideas?