Why Use Sass?
The CSS variables from the previous lesson are great for small changes. For deeper customization, such as new theme colors, new breakpoints or removing unused components, you edit Bootstrap's Sass source files and compile your own CSS.
What is Sass? Sass (SCSS) is an extension of CSS that adds variables, nesting and imports. A compiler turns your .scss files into normal .css files that browsers understand.
Step 1: Install
bash
pm init -y
npm install bootstrap sass
Step 2: Create Your SCSS File
Create scss/custom.scss. The golden rule: set your variables before importing Bootstrap.
scss
// 1. Your variable overrides
$primary: #7b2cbf;
$secondary: #6c757d;
$font-family-base: "Poppins", sans-serif;
$border-radius: 0.75rem;
$enable-shadows: true;
// 2. Import Bootstrap
@import "../node_modules/bootstrap/scss/bootstrap";
Note: Bootstrap's Sass uses the @import rule. Sass is moving toward @use and may show deprecation warnings in the newest versions, but your code still compiles. Check the official Bootstrap docs for the recommended setup for your Sass version.
Step 3: Compile
bash
px sass scss/custom.scss css/custom.css
# or watch for changes
npx sass --watch scss/custom.scss css/custom.css
Step 4: Use Your CSS
html
<link href="css/custom.css" rel="stylesheet">
Result
Every primary button, link and badge now uses your purple color, and all corners are rounder.
Adding Your Own Theme Color
scss
@import "../node_modules/bootstrap/scss/functions";
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/variables-dark";
@import "../node_modules/bootstrap/scss/maps";
@import "../node_modules/bootstrap/scss/mixins";
$custom-colors: ("brand": #ff6b57);
$theme-colors: map-merge($theme-colors, $custom-colors);
@import "../node_modules/bootstrap/scss/utilities";
@import "../node_modules/bootstrap/scss/bootstrap";
Now btn-brand, bg-brand and text-brand exist automatically.
Import Only What You Need
To make your CSS smaller, import only the parts you use instead of the full Bootstrap file:
scss
@import "bootstrap/scss/functions";
@import "bootstrap/scss/variables";
@import "bootstrap/scss/variables-dark";
@import "bootstrap/scss/maps";
@import "bootstrap/scss/mixins";
@import "bootstrap/scss/root";
@import "bootstrap/scss/reboot";
@import "bootstrap/scss/containers";
@import "bootstrap/scss/grid";
@import "bootstrap/scss/buttons";
@import "bootstrap/scss/utilities";
@import "bootstrap/scss/utilities/api";
Useful Variables
| Variable | Controls |
$primary ... $dark | Theme colors |
$body-bg, $body-color | Page background and text |
$font-size-base | Base font size |
$spacer | Base spacing unit |
$grid-breakpoints | Breakpoint widths |
$enable-rounded, $enable-shadows | Turn features on or off |
Conclusion
Sass customization turns Bootstrap into your own design system. Set variables first, import Bootstrap after, and compile to CSS.
Quick Summary: Install sass, override variables before importing Bootstrap, compile with npx sass, and import only the parts you need for a smaller file.