Trey Hunner

I help developers level-up their Python skills

Hire Me For Training

Creating a custom Bootstrap build

| Comments

Have you ever tried to customize the font size for your Bootstrap-powered website?

It’s not something you can easily do without recompiling Bootstrap’s CSS. If you change the font size on your page, you’ll want that change to cascade through. A change in the base font size should recalculate header font sizes and margins/padding for text elements like paragraph tags.

Let’s look at how to create a custom Bootstrap SASS build without maintaining our own fork of Bootstrap.

Using Bootstrap with Bower

We’re going to use bower and gulp to compile Bootstrap, so we’ll need Node.js.

First let’s make a package.json file for Node to see. Either use npm init -y or create a package.json file containing just an empty JSON object ({}).

Now let’s install gulp, gulp-bower, and gulp-sass:

$ npm install --save-dev gulp gulp-bower gulp-sass

Our package.json file should now look something like this:

package.json
1
2
3
4
5
6
7
{
  "devDependencies": {
    "gulp": "^3.8.11",
    "gulp-bower": "0.0.10",
    "gulp-sass": "^1.3.3"
  }
}

Now let’s use Bower to install bootstrap. This will allow us to import Bootstrap into our SCSS code and compile it down to CSS manually.

Create a bower.json file using bower init or by manually creating one:

bower.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
  "name": "custom-bootstrap-example",
  "authors": [
    "Lillian Langston <[email protected]>"
  ],
  "private": true,
  "ignore": [
    "**/.*",
    "node_modules",
    "bower_components",
    "test",
    "tests"
  ],
  "dependencies": {
  }
}

Now let’s install bootstrap-sass with Bower.

1
$ bower install --save bootstrap-sass

Our bower.json file should now have bootstrap-sass listed as a dependency:

1
2
3
"dependencies": {
  "bootstrap-sass": "~3.3.3"
}

Now we can make an SCSS file that includes bootstrap into our project. Let’s call our SCSS file css/app.scss:

css/app.scss
1
2
@import "bootstrap";
@import "bootstrap/theme";

Now let’s use gulp to compile our app.scss which includes Bootstrap SASS:

gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var gulp = require('gulp');
var sass = require('gulp-sass');

var config = {
    bootstrapDir: './bower_components/bootstrap-sass',
    publicDir: './public',
};

gulp.task('css', function() {
    return gulp.src('./css/app.scss')
    .pipe(sass({
        includePaths: [config.bootstrapDir + '/assets/stylesheets'],
    }))
    .pipe(gulp.dest(config.publicDir + '/css'));
});

gulp.task('fonts', function() {
    return gulp.src(config.bootstrapDir + '/assets/fonts/**/*')
    .pipe(gulp.dest(config.publicDir + '/fonts'));
});

gulp.task('default', ['css', 'fonts']);

Now when we run gulp, our compiled Bootstrap CSS should appear in the public/css directory:

1
2
3
$ gulp
$ ls public/css
app.css

Customizing the font size

Now let’s look at how we can go about customizing the font size in Bootstrap.

Notice that the value of the $font-size-base variable in the _variables.scss file is used for calculating a variety of other important variables. For example 8 of the lines below rely on $font-size-base:

1
2
3
4
5
6
7
8
9
10
$font-size-base:          14px !default;
$font-size-large:         ceil(($font-size-base * 1.25)) !default; // ~18px
$font-size-small:         ceil(($font-size-base * 0.85)) !default; // ~12px

$font-size-h1:            floor(($font-size-base * 2.6)) !default; // ~36px
$font-size-h2:            floor(($font-size-base * 2.15)) !default; // ~30px
$font-size-h3:            ceil(($font-size-base * 1.7)) !default; // ~24px
$font-size-h4:            ceil(($font-size-base * 1.25)) !default; // ~18px
$font-size-h5:            $font-size-base !default;
$font-size-h6:            ceil(($font-size-base * 0.85)) !default; // ~12px

Notice those !default flags? That !default flag means the variables will be set only if they don’t have a value already.

All of the variables assigned in Bootstrap SASS’s _variables.scss file have a !default flag. That means we can override those variables by assigning our own values before we import Bootstrap.

Let’s copy Bootstrap’s _variables.scss file and make our own custom version:

1
$ cp bower_components/bootstrap-sass/assets/stylesheets/bootstrap/_variables.scss css/_variables.scss

Now we need to reference our custom variables module from our app.scss file.

1
2
3
@import "variables";
@import "bootstrap";
@import "bootstrap/theme";

Remember to import our variables module before we import Bootstrap! If we import it afterward, our changes customizations won’t be applied.

Now let’s change $font-size-base to 16px in css/_variables.scss:

1
$font-size-base:          16px;

Now if we recompile our CSS we should see our larger font size reflected throughout our application:

1
$ gulp

Try it out!

I made a sample project to demonstrate how easy it is to customize Bootstrap variables before building Bootstrap SASS.

Check out the sample project on Github

Know about a different way to customize Bootstrap? Did I make a mistake in my explanation? Leave a comment and let me know.

Comments

Write more Pythonic code

Need to fill-in gaps in your Python skills? I send regular emails designed to do just that.