This tutorial covers using Git to clone, branch and merge the latest Bootstrap source code, modifying and compiling your modifications to the source LESS variables using Grunt and viewing your compiled changes with a local Node.js web server.
You could just use the Bootstrap customize and download form to customize the LESS variables and download a pre-compiled Bootstrap package. However, as of this writing, the user interface at the form doesn’t provide a preview option to monitor your changes before you download the package. And since the Bootstrap version 3 source now includes Grunt, we can use it to compile the LESS source and preview the changes via localhost.
Using Git, clone the Bootstrap repository. To keep our changes to the source separate and to make upgrading the bootstrap source easier, create a develop branch.
# git clone creates the bootstrap directory in your current directory git clone https://github.com/twbs/bootstrap.git # create and checkout develop branch git checkout -b develop
Load a Bootstrap example
I like to use the Node.js web-server.js that is bundled with the AngularJS tutorial. For example, here is how I start the NodeJs web-server.js that is included in the angular-phonecat repository on GitHub. This example presumes your CLI is in the bootstrap directory and the angular-phonecat repository is cloned to its own directory one level up.
Repository contents rearranged over the holidays and the examples have been moved to the docs folder. You can always take a look at the latest source on GitHaub.
# directory structure (partial) # | angular-phonecat/ # | -- scripts/ # | -- web-server.js # | bootstrap/ # | -- docs/ # | -- examples/ # | -- starter-template/ # | -- index.html # | -- starter-template.css # start the web server from the bootstrap directory node ../angular-phonecat/scripts/web-server.js Http Server running at http://localhost:8000/
Now open a web browser and go to http://localhost:8000/docs/examples/starter-template/index.html.
Modify LESS variables
Open the bootstrap/less/variables.less file in a text editor and find the
@navbar-inverse-link-color variables and change their values from
@gray-lighter. Also, find
@navbar-inverse-bg and change its value to
Install node modules and compile LESS
The Bootstrap source contains a package.json for node module installation and a fully configured gruntfile.js to run Grunt tasks.
Open another bash window CLI and navigate to the bootstrap directory to install the node modules. After the node modules are installed, run the Grunt
dist-css task which compiles the less files to
dist/css/ followed by task
dist-docs which copies the css files created in the previous task to
# install node modules npm install # run grunt task to compile less to dist/css/ grunt dist-css # run grunt task to copy to docs/dist/css/ grunt dist-docs
Refresh the web browser to see the compiled changes. The navigation bar at the top of the document in the starter template should now be orange instead of black.
Upgrading the source
Since we are only modifying the variables.less file from the source, upgrading is not that difficult. Using Git, merge the newer Bootstrap source master branch into the develop branch we created.
# checkout master branch git checkout master # get the latest bootstrap source git pull origin master # checkout develop git checkout develop # merge master into it git merge master