Bootstrap 4 Grid only and SASS with Gulp

Bootstrap is a great CSS framework, but what if we only want to use the grid and not all the other features? You can do this if you either use the SASS or LESS version of the bootstrap framework. I’ll quickly demonstrate how you only take the necessary parts. I dug into this, because I was creating a landing page only featuring parts of the bootstrap framework to increase the page speed.

see the demo online | see the code on github

Because Bootstrap is coming in LESS (from version 4 only in SASS) and SASS variants we can use those to only include certain files and not the entire framework. If you look at the Twitter Bootstrap Github repository, you can find the files inside the sass folder.

We’ll make use of the bootstrap v4-dev branch for this tutorial, which in the fututre will be the master branch. Check out the v4 alpha documentation.

Get the code from the v4 bootstrap development branch.

What you will need to use the bootstrap 4 grid system:

  1. bootstrap
  2. a SASS compiler
  3. a code editor
  4. optional: gulp

To clone my repository, just use the following:

git clone --recursive git@github.com:JonathanMH/bootstrap-grid.git

(thanks https://github.com/BekoBou for the hint ;))
Now let’s go ahead and create a directory for our project:

mkdir bootstrap-grid
cd bootstrap-grid
git init

Next, let’s add the bootstrap repository as a git submodule and switch to the bootstrap v4-dev branch, you need to have git installed for the following step, but you can also download a .zip file manually.

git submodule add https://github.com/twbs/bootstrap.git
cd bootstrap
git checkout remotes/origin/v4-dev

You can also list all available branches with git branch -a. After this we change back to our project directory with cd ...

Now we need to create our example files and folders:

mkdir scss css
touch index.html scss/style.scss

Now we should have the following files in our project directory:

css/
index.html
scss/
  style.scss

including only the bootstrap grid in our SASS file

bootstrap-4-grid-sass-scss

To only use the bootstrap grid, we’ll just include the relevant .scss files straight from the bootstrap repository. Opening the style.scss file, we can put in the following to only include the bootstrap files that have something to do with the grid. Also the variables in the top set the grid work with the flexbox rules instead of floats.

$enable-flex:               true;
$enable-grid-classes:       true;

@import './bootstrap/scss/_variables.scss';
@import './bootstrap/scss/_mixins.scss';
@import './bootstrap/scss/_normalize.scss';
@import './bootstrap/scss/_reboot.scss';
@import './bootstrap/scss/_grid.scss';

Now when we compile the file to the css folder, it will show that the bootstrap grid relevant rules have been written to css/style.css. To make sure it works, we can insert something like the code below into the index.html file. Pay attention to, that we are not including the .scss file, but the compiled style.css:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Bootstrap v4 only grid system | JonathanMH</title>
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <div class="container">
      <!-- Stack the columns on mobile by making one full-width and the other half-width -->
      <div class="row">
        <div class="col-xs-12 col-md-8">.col-xs-12 .col-md-8</div>
        <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
      </div>

      <!-- Columns start at 50% wide on mobile and bump up to 33.3% wide on desktop -->
      <div class="row">
        <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
        <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
        <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
      </div>

      <!-- Columns are always 50% wide, on mobile and desktop -->
      <div class="row">
        <div class="col-xs-6">.col-xs-6</div>
        <div class="col-xs-6">.col-xs-6</div>
      </div>
    </div>
  </body>
</html>

automating sass builds gulp

Here’s the relevant part from my gulp file, that uses the gulp-sass module to process my style.scss file. Since we don’t want to manually compile this every time, we’ll automate this all with gulp and on top of that, minify our CSS. This is the content of my gulpfile.js inside my project:

var gulp = require('gulp');
var sass = require('gulp-sass');
var minifyCss = require('gulp-minify-css');

gulp.task('styles', function() {
  gulp.src('./scss/style.scss')
    .pipe(sass().on('error', sass.logError))
    .pipe(minifyCss({compatibility: 'ie9'}))
    .pipe(gulp.dest('./css/'))
});

// Watch task
gulp.task('default',function() {
  // run task initially, after that watch
  gulp.start('styles');
  gulp.watch('./scss/*.scss',['styles']);
});

To use it, make sure you install gulp globally and the dependencies inside the project:

sudo -H npm install -g gulp
npm install --save gulp gulp-sass gulp-minify-css
gulp

Now you should be getting the following output:

(master)⚡ % gulp
[17:19:32] Using gulpfile ~/projects/bootstrap-grid/gulpfile.js
[17:19:32] Starting 'default'...
[17:19:32] Starting 'styles'...
[17:19:32] Finished 'styles' after 19 ms
[17:19:32] Finished 'default' after 39 ms

Performance Matters!

One of the reasons to do this, is of course to save bandwidth and decrease the page load speed. By only using what we need for the grid system, we end up at a minified CSS file size of 28kb instead of the full bootstraps 104kb. You can of course do this with any part of the bootstrap framework or other frameworks that make use of a CSS preprocessor.

ls -lsh css
28K -rw-r--r-- 1 geronimo geronimo 19K Dec 26 17:29 style.css
ls -lsh bootstrap/dist/css
104K -rw-r--r-- 1 geronimo geronimo  95K Dec 26 16:58 bootstrap.min.css

Maybe you have heard that performance gets some additional attention, also with Facebook having their 2g tuesdays. Performance is really not measurable without context, but certainly an important point to pay attention to.

Summary

Now you know how to manage dependencies with git, automate a task with gulp and how to only take what you need from the bootstrap css framework. This is only one method to make your site faster and it should happen right from the beginning so you don’t include things you will not need anyways.

Would you do something different? How are you managing build processes and dependencies?

6 thoughts on “Bootstrap 4 Grid only and SASS with Gulp”

  1. Thanks for a great article. However when I try to compile this I receive this error “(Line 18 of bootstrap/_mixins.scss: File to import not found or unreadable: mixins/breakpoints.”

    1. I’ll have a look when I get home if the file names changed. Are you sure you’re on the right branch of the bootstrap repo?

  2. Hi, Just run through the above and needed to add a mixin folder with the below .scss files for it to run today:

    mixins/breakpoints
    mixins/grid-framework
    mixins/grid
    mixins/hover
    mixins/tab-focus

    Then commented out all the other referenced mixin files in /mixins.scss so that it will build.

    Thanks for the blog post – really useful as I want to look further into the flexgrid use/setup

    Thanks

  3. Brilliant article! I’ve got everything working after following your instructions.

    I’ve got a couple of questions though :

    1) In the bootstrap/scss/mixins there is a file called ‘_grid-framework.scss’. Is this also required to be imported into the style.scss file with the other bootstrap mixins listed in your article?

    2) “$enable-flex:true;” works! But what does “$enable-grid-classes:true;” do?

    I ask because if I comment this out the flexbox grid still works (as does the standard float-left grid if I also comment out $enable-flex).

    1. Replying to my own comment because I realised that “_grid-framework.scss” is imported via “_mixins.scss” – doh!

      Would still appreciate an answer to question 2) though 🙂

Leave a Reply

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