donturner
donturner

Reputation: 19146

How can I customise Bootstrap without losing the changes?

I'm using Bower to manage Bootstrap and would like to make some changes (colours, font size etc) to the default Bootstrap look and feel. Here's my workflow:

  1. Edit bower_components/bootstrap/less/variables.less
  2. Recompile bootstrap using grunt build

The problem is that I want to be able to upgrade bootstrap when a new version comes out and presumably I'll lose my changes to variables.less.

Is there a way I can keep my changes outside of bower_components and also avoid having bower_components in source control since it's 122MB?

Upvotes: 3

Views: 2077

Answers (4)

donturner
donturner

Reputation: 19146

Here's the solution which worked for me:

  • Use bower to install all UI packages e.g. bower install bootstrap chosen
  • Create a separate folder less which contains all the LESS modifications. This article was very helpful here.

Here's my less/styles.less file:

@import "../bower_components/bootstrap/less/bootstrap.less";
@import "../bower_components/bootstrap-chosen/bootstrap-chosen.less";

//My custom variables - overrides the bootstrap variables file
@import "variables-custom.less";
  • Use grunt to monitor changes within the less folder and compile them into .css

Here's my Gruntfile.js (thanks to this answer):

module.exports = function(grunt) {
    grunt.initConfig({
        less: {
            development: {
                options: {
                    paths: ["./less"],
                    yuicompress: true
                },
                files: {
                    "./static/css/styles.css": "./less/styles.less"
                }
            }
        },
        watch: {
            files: "./less/*",
            tasks: ["less"]
        }
    });
    grunt.loadNpmTasks('grunt-contrib-less');
    grunt.loadNpmTasks('grunt-contrib-watch');
};

Upvotes: 2

montecruiseto
montecruiseto

Reputation: 544

This is indeed the best customization method. You create a theme.less and pull in original Bootstrap files (which can get upgraded in the future) and in the same file you call your own custom overrides. Either you @import them from a custom file which is not in the Bower directory or you just write your custom rules in your theme.less itself. You'll find this technique explained in this tutorial as well.

With Grunt, custom setups can get tricky. But with Brunch it's a piece of cake (yes!) and all pretty much goes automatically. Your grandma could do it.

As for avoiding the inclusion of bower_components in source control: with Git it's easy. You just check-in your bower.json but make sure to add /bower_components to your .gitignore file.

Upvotes: 1

Josh
Josh

Reputation: 89

You should just create your own style sheet, use both with your custom one listed secondly. That way you can make changes but not change bootstrap at all.

Also, when you update, you keep your style sheet the same.

This allows you to change bits and pieces of Bootstrap but not actually changing the file, you're overriding it.

To be clear, your second CSS file would be SIGNIFICANTLY smaller... Only putting things your needed to change in it.

Upvotes: 0

solidau
solidau

Reputation: 4081

you can create a variables-custom.less and import it into theme.less like this:

//
// Load core variables and mixins
// --------------------------------------------------

@import "variables.less";
//import custom-variables after variables so the values will override.
@import "custom-variables.less"; //only has variables that have changed.
@import "mixins.less";

IMO this is a little bit better than the first solution because you wont have to load two (almost) identical CSS files on the client.

I'm sorry I cant help you with what to to about Bower and your source control as I do not use Bower

Upvotes: 4

Related Questions