Bootstrap 4 CSS source code is written in Sass. You can customize Bootstrap CSS in various ways. A typical workflow is as follows.
Bootstrap Sass customization requires at least two (or three) processing tools. You may also need a build tool.
Sass has three official implementations: Ruby Sass (original), libsass (C++), and Dart Sass (just released recently). Ruby Sass and libsass are both popular but libsass has been getting major as it is much faster than Ruby Sass. This tutorial uses node-sass, which is an interface of libsass for node.js.
Most parts of Bootstrap 4 are written in pure (i.e. no vender prefix) Sass. It relies on Autoprefixer to process vendor prefixes. It runs as a PostCSS plugin on node.js (no alternative).
Supported browsers are written in package.json
with browserslist format (see Best Practices and Full List for query syntax). This tutorial borrows the same settings as Bootstrap 4.2 as below. It is referenced by Autoprefixer to add or remove vender prefixes for each CSS property.
.browserslistrc
# https://github.com/browserslist/browserslist#readme
>= 1%
last 1 major version
not dead
Chrome >= 45
Firefox >= 38
Edge >= 12
Explorer >= 10
iOS >= 9
Safari >= 9
Android >= 4.4
Opera >= 30
You can optimize CSS output with a CSS minifier. There are a lot of CSS minifiers for various environments (you can use anything you like). This tutorial uses node.js for the working environment. There are several choises for CSS minifiers for node.js.
You may also need a build tool. You can use anything you prefer (like GNU make). This tutorial recommends node.js-based tools as Autoprefixer (runs only on node.js) is required. There are numbers of build (task runner) tools on node.js (like Webpack, Gulp, Grunt, etc.).
This tutorial uses npm CLI for simplicity. Npm is mainly a package manager for node.js but also can be a tiny task runner. You can build this tutorial by yourself as the following instruction.
npm install
npm run build
(files are generated to ./site/*
)npm run start
(hosted to http://localhost:8888/
)See "scripts:"
field entries of package.json for more commands. These commands are executed with npm run[-script]
(⇒ Reference).
Bootstrap Sass source code consists of multiple .scss
source files. They are classified into two types.
bootstrap.scss
, bootstrap.grid.scss
, and bootstrap.reboot.scss
_variables.scss
)
Use @import
statement to use (import) Sass code. You can import whole Bootstrap code with @import "{path}/bootstrap";
. Or you can also import by partials as @import "{path}/variables";
(for _variables.scss
). You do not have to type underscores.
Bootstrap uses a lot of (over 500!) Sass variables (with prefix $
as Sass syntax) for the library implementation and also for customization. All variables are defined in _variable.scss
with variable default syntax !default
.
_variable.scss
(excerpts)$white: #fff !default;
$gray-100: #f8f9fa !default;
$gray-200: #e9ecef !default;
...
$body-bg: $white !default;
$body-color: $gray-900 !default;
...
To customize Bootstrap, you define these variables with appropreate values you want first and import Bootstrap next. For example, you can change the basic page colors as below.
// Initialize variable to customize before importing Bootstrap
$body-bg: ivory; // or #fffff0;
$body-color: navy; // or #000080;
// Import Bootstrap (whole library)
@import "bootstrap/scss/bootstrap";
You can also customize Bootstrap with importing by partials. See Importing Sass partials for detail.