Bootstrap · Chapter 42 of 43

Customizing Bootstrap with Sass Variables

For deeper customization than utility classes allow, Bootstrap is built with Sass, letting you override its default variables — like colors, fonts, and spacing scale — before compiling your own custom CSS build.

This approach lets you change Bootstrap's entire look (like the primary color or default border radius) in one place, rather than overriding CSS with !important everywhere.

Syntax
// custom.scss
$primary: #ff6600;
@import "bootstrap/scss/bootstrap";

Overriding variables

Create a custom Sass file that first sets variables like $primary or $border-radius, then imports Bootstrap's source Sass files, so your values replace the defaults before Bootstrap compiles.

Building the project

Install Bootstrap via npm (npm install bootstrap), then use a Sass compiler (like the sass npm package or a bundler's Sass loader) to compile your custom.scss file into a final CSS file to link in your HTML.

Example 1 (scss)
// custom.scss
$primary: #ff6600;
$border-radius: 1rem;

@import "bootstrap/scss/bootstrap";
Output
A compiled CSS file where .btn-primary and .bg-primary now use orange instead of the default blue

Setting $primary before importing Bootstrap's Sass overrides the default primary color used across all components.

Example 2 (bash)
npm install bootstrap sass
npx sass custom.scss custom.css
Output
Generates custom.css containing your customized version of Bootstrap

The sass command compiles your custom.scss (which imports and overrides Bootstrap) into a regular CSS file you can link like any stylesheet.

Key points

  • Bootstrap's Sass source lets you override default variables before compiling.
  • Variables must be set before the @import of Bootstrap's Sass files.
  • Common variables to customize include $primary, $font-family-base, and $border-radius.
  • You need a Sass compiler to turn your custom .scss file into usable CSS.
💡 Note: This approach produces a single optimized CSS file tailored to your brand, instead of loading extra override CSS on top of the default Bootstrap file.

📝 Quick Quiz

1. What must you do before importing Bootstrap's Sass files?

2. Which variable would you change to alter Bootstrap's main theme color?

3. What tool is needed to turn a .scss file into CSS?