Nadi Dikun    Interface Mechanics
   About    Contact   

How to add Autoprefixer to work with Bootstrap using LESS

If you’ve been following the latest Bootstrap releases, you are probably aware that starting with version 3.2, it doesn’t use mixins for vendor prefixes.

#12670: Drop use of vendor prefix mixins and use autoprefixer as part of our Gruntfile.

Mixins were moved to a separate vendor-prefixes.less file and kept for backward compatibility. However, they will be dropped entirely starting with version 4.

Who does it actually affect?

Well, it really affects me. And you, if you use a CSS pre-processor! :)

Usually, when I use Bootstrap I work with its LESS files. This way I don’t need to include a separate bootstrap.min.css into my HTML. It also gives me 100% control over my final CSS file, because I can exclude those Bootstrap modules that I don’t need in a current project, to keep it efficient.

So now, if you compile your LESS files into CSS, you will notice that you don’t have browser-specific prefixes in your CSS. Actually, some Bootstrap modules would be entirely broken. For example, Bootstrap carousel won’t slide at all, because it lacks transition browser prefixes.

To solve this issue, we have three options:

  1. Include pre-compiled bootstrap.min.css. This is not an option for me, because I rarely use all the Bootstrap modules and I’d like users to load only useful code.

  2. Use Bootstrap Customizer to build a custom version of Bootstrap, which would include only those modules you need for the project. This option is better. But as your project evolves, you’ll need to re-create and download a new version again and again. Also, you will still need to include a separate CSS file in your HTML.

  3. Change your build process for LESS to include Autoprefixer. This is the best solution for my needs. I will explain how to set this up below.

Please note:
If you prefer to use Bootstrap Customizer, please update to v3.3.1 where Autoprefixer support has been added.

What is Autoprefixer?

It’s a plug-in that enables you to write normal CSS and completely forget about vendor prefixes.

Autoprefixer will use the data based on current browser popularity and property support to apply prefixes for you. Autoprefixer is recommended by Google and used in Twitter, WordPress, Bootstrap and CodePen.

This code:

a {
    display: flex;
}

would be transformed by Autoprefixer into this:

a {
    display: -webkit-box;
    display: -webkit-flex;
    display: -moz-box;
    display: -ms-flexbox;
    display: flex
}

Because Autoprefixer is a post-processor for CSS, you can use it with pre-processors such as LESS, Sass or Stylus.

How to include Autoprefixer into your CSS build process?

The best way to do it is to use one of the task runners that will automatically convert and combine your LESS files into a single CSS.

I use Gulp, because it’s extremely declarative and straightforward. It has a number of really useful plug-ins to choose from.

Below is an example of Gulpfile.js that you can set up to convert your LESS files into CSS. This code will compress, minify and prefix your code every time you run your Gulp task named less.

// Include Gulp and all required plugins

var gulp = require('gulp'); var less = require('gulp-less'); var autoprefixer = require('gulp-autoprefixer'); var minifyCSS = require('gulp-minify-css'); var gutil = require('gulp-util');

var sourcePath = 'src/custom'; var targetPath = 'assets/css';

// Create gulp task named 'less' that // will take 'custom.less' file from 'sourcePath' folder, // compress it, // add browser specific prefixes, // minify it, // save result CSS file into 'targetPath' folder

gulp.task('less', function () { return gulp.src([sourcePath + '/custom.less']) .pipe(less({compress: true}).on('error', gutil.log)) .pipe(autoprefixer('last 10 versions', 'ie 9')) .pipe(minifyCSS({keepBreaks: false})) .pipe(gulp.dest(targetPath)); });

As you can see, this JavaScript code is really easy to read and write.

First, it requires several Gulp plug-ins. To install all of them locally in your project folder, use this command:

$ npm install gulp-less gulp-autoprefixer gulp-minify-css gulp-notify gulp-util del --save-dev
  • --save-dev saves all listed plug-ins into devDependencies in package.json
  • a gist of a basic package.json file

Then it runs a tiny function that chains all the complex processes. Each plug-in imports a file from the previous one, converts it, then passes it to the next plug-in.

It’s ss simple as that!

Let me know if you have any questions and good luck automating your next project!

comments powered by Disqus