Bootstrap icon

Customizing Bootstrap 4 without changing the core files

By

in

This post provides a simple instruction on how to customize the Bootstrap 4.0 stylesheet using SASS and Autoprefixer.

But why? You can either…

  1. download the compiled version of Bootstrap or
  2. use the source of Bootstrap 4 and compile a custom version or
  3. use the CDN server

When you choose number 2, then it’s necessary to use SASS for compiling the style sheet and Autoprefixer for CSS vendor prefixing. And this is how to get a customized version of Bootstrap:

First make sure, you’ve installed Node.js and SASS.

Then create an empty folder my_directory with the following folders and some empty text files:

my_directory
+- node_modules [folder]
+- custom.scss
+- package.json

The node_modules folder is required for some node.js modules. custom.css will hold all your customized stylesheets and package.json is used for some node.js package settings. You will see this later.

Now it’s time to load the current version of Bootstrap. Download the source of Bootstrap 4 and extract the zip to the folder /node_modules/bootstrap/.

Point your terminal to my_directory and install some node.js modules with the following command:

npm install postcss-cli autoprefixer npm-run-all

Now you can open the custom.scss file and add some costumized styles. This file can look like this:

SCSS
// Custom.scss

$mixable: #ca0027;

$colors: (
 "mixable": $mixable,
);
$theme-colors: (
 "primary": $mixable
);

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

In this simple example, we just set a new color and use this color as primary color. A more detailed description on how to customize your theme can be found in the Bootstrap docs.

Let’s say we have made all of our customizations, so let’s open the empty file package.json. Add the following lines, which are required to combine your custom stylesheet and the Bootstrap source:

JSON
{
  "scripts": {
    "style":"sass custom.scss | postcss --use autoprefixer -o custom.css",
    "style-min":"sass custom.scss --style compressed | postcss --use autoprefixer -o custom.min.css",
    "all":"npm-run-all --parallel style style-min"
  }
}

This file contains three scripts, which can be executed like this:

To create the output file custom.css:

npm run style

To create a compressed output file custom.min.css:

npm run style-min

And to create both files at once:

npm run all

That’s it. Keep in mind, that this will just compile the stylesheet. To compile the javascript, you should refer to the Bootstrap docs and the build tools there.



Comments

Leave a Reply

Your email address will not be published. Required fields are marked *