12 min read

In this article, by Andrea Saccà, the author of Mastering Magento Theme Design, we will learn how to integrate the Bootstrap 3 framework and how to develop the main theme blocks.

The following topics will be covered in this article:

  • An introduction to Bootstrap

  • Downloading Bootstrap (the current Version 3.1.1)

  • Downloading and including jQuery

  • Integrating the files into the theme

  • Defining the main layout design template

(For more resources related to this topic, see here.)

An introduction to Bootstrap 3

Bootstrap is a sleek, intuitive, powerful, mobile-first frontend framework that enables faster and easier web development, as shown in the following screenshot:

Bootstrap 3 is the most popular frontend framework that is used to create mobile-first websites. It includes a free collection of buttons, CSS components, and JavaScript to create websites or web applications; it was created by the Twitter team.

Downloading Bootstrap (the current Version 3.1.1)

First, you need to download the latest version of Bootstrap. The current version is 3.0. You can download the framework from http://getbootstrap.com/.

The fastest way to download Bootstrap 3 is to download the precompiled and minified versions of CSS, JavaScript, and fonts. So, click on the Download Bootstrap button and unzip the file you downloaded. Once the archive is unzipped, you will see the following files:

We need to take only the minified version of the files, that is, bootstrap.min.css from css, bootstrap.min.js from js, and all the files from font.

For development, you can use bootstrap.css so that you can inspect the code and learn, and then switch to bootstrap.min.css when you go live.

Copy all the selected files (CSS files inside the css folder, the .js files inside the js folder, and the font files inside the fonts folder) in the theme skin folder at skin/frontend/bookstore/default.

Downloading and including jQuery

Bootstrap is dependent on jQuery, so we have to download and include it before including boostrap.min.js. So, download jQuery from http://jquery.com/download/.

The preceding URL takes us to the following screenshot:

We will use the compressed production Version 1.10.2.

Once you download jQuery, rename the file as jquery.min.js and copy it into the js skin folder at skin/frontend/bookstore/default/js/.

In the same folder, also create the jquery.scripts.js file, where we will insert our custom scripts.

Magento uses Prototype as the main JavaScript library. To make jQuery work correctly without conflicts, you need to insert the no conflict code in the jquery.scripts.js file, as shown in the following code:

// This is important!jQuery.noConflict(); jQuery(document).ready(function() { // Insert your scripts here });

The following is a quick recap of CSS and JS files:

Integrating the files into the theme

Now that we have all the files, we will see how to integrate them into the theme.

To declare the new JavaScript and CSS files, we have to insert the action in the local.xml file located at app/design/frontend/bookstore/default/layout.

In particular, the file declaration needs to be done in the default handle to make it accessible by the whole theme.

The default handle is defined by the following tags:

. . .

The action to insert the JavaScript and CSS files must be placed inside the reference head block. So, open the local.xml file and first create the following block that will define the reference:

Declaring the .js files in local.xml

The action tag used to declare a new .js file located in the skin folder is as follows:


In our skin folder, we copied the following three .js files:

  • jquery.min.js

  • jquery.scripts.js

  • bootstrap.min.js

Let’s declare them as follows:


Declaring the CSS files in local.xml

The action tag used to declare a new CSS file located in the skin folder is as follows:


In our skin folder, we have copied the following three .css files:

  • bootstrap.min.css

  • styles.css

  • print.css

So let’s declare these files as follows:


Repeat this action for all the additional CSS files.

All the JavaScript and CSS files that you insert into the local.xml file will go after the files declared in the base theme.

Removing and adding the style.css file

By default, the base theme includes a CSS file called styles.css, which is hierarchically placed before the bootstrap.min.css.

One of the best practices to overwrite the Bootstrap CSS classes in Magento is to remove the default CSS files declared by the base theme of Magento, and declare it after Bootstrap’s CSS files.

Thus, the styles.css file loads after Bootstrap, and all the classes defined in it will overwrite the boostrap.min.css file.

To do this, we need to remove the styles.css file by adding the following action tag in the xml part, just before all the css declaration we have already made:


Hence, we removed the styles.css file and added it again just after adding Bootstrap’s CSS file (bootstrap.min.css):


If it seems a little confusing, the following is a quick view of the CSS declaration:


Adding conditional JavaScript code

If you check the Bootstrap documentation, you can see that in the HTML5 boilerplate template, the following conditional JavaScript code is added to make Internet Explorer (IE) HTML 5 compliant:

To integrate them into the theme, we can declare them in the same way as the other script tags, but with conditional parameters. To do this, we need to perform the following steps:

  1. Download the files at https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js and https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js.

  2. Move the downloaded files into the js folder of the theme.

  3. Always integrate JavaScript through the .xml file, but with the conditional parameters as follows:

    skin_jsjs/html5shiv.jslt IE 9skin_jsjs/respond.min.jslt IE 9

A quick recap of our local.xml file

Now, after we insert all the JavaScript and CSS files in the .xml file, the final local.xml file should look as follows:

skin_jsjs/jquery.min.jsskin_jsjs/bootstrap.min.jsskin_jsjs/jquery.scripts.jsskin_jsjs/html5shiv.jslt IE 9skin_jsjs/respond.min.jslt IE 9skin_csscss/styles.cssskin_csscss/bootstrap.min.cssskin_csscss/styles.css

Defining the main layout design template

A quick tip for our theme is to define the main template for the site in the default handle.

To do this, we have to define the template into the most important reference, root. In a few words, the root reference is the block that defines the structure of a page.

Let’s suppose that we want to use a main structure having two columns with the left sidebar for the theme To change it, we should add the setTemplate action in the root reference as follows:

You have to insert the reference name “root” tag with the action inside the default handle, usually before every other reference.

Defining the HTML5 boilerplate for main templates

After integrating Bootstrap and jQuery, we have to create our HTML5 page structure for the entire base template.

The following are the structure files that are located at app/design/frontend/bookstore/template/page/:

  • 1column.phtml

  • 2columns-left.phtml

  • 2columns-right.phtml

  • 3columns.phtml

The Twitter Bootstrap uses scaffolding with containers, a row, and 12 columns. So, its page layout would be as follows:

This structure is very important to create responsive sections of the store. Now we will need to edit the templates to change to HMTL5 and add the Bootstrap scaffolding.

Let’s look at the following 2columns-left.phtml main template file:

getChildHtml(‘head’) ?> getBodyClass()?’ class=”‘.$this->
getBodyClass().'”‘:” ?>> getChildHtml(‘after_body_start’) ?> getChildHtml(‘global_notices’) ?>
getChildHtml(‘header’) ?>
getChildHtml(‘slider’) ?>
getChildHtml(‘breadcrumbs’) ?>
getChildHtml(‘global_messages’) ?> getChildHtml(‘content’) ?>
getChildHtml(‘footer’) ?>
getChildHtml(‘before_body_end’) ?> getAbsoluteFooter() ?>

You will notice that I removed the Magento layout classes col-main, col-left, main, and so on, as these are being replaced by the Bootstrap classes.

I also added a new section, after-header, because we will need it after we develop the home page slider.

Don’t forget to replicate this structure on the other template files 1column.phtml, 2columns-right.phtml, and 3columns.phtml, changing the columns as you need.


We’ve seen how to integrate Bootstrap and start the development of a Magento theme with the most famous framework in the world. Bootstrap is very neat, flexible, and modular, and you can use it as you prefer to create your custom theme. However, please keep in mind that it can be a big drawback on the loading time of the page. Following these techniques by adding the JavaScript and CSS classes via XML, you can allow Magento to minify them to speed up the loading time of the site.

Resources for Article:

Further resources on this subject:

Subscribe to the weekly Packt Hub newsletter. We'll send you this year's Skill Up Developer Skills Report.

* indicates required


Please enter your comment!
Please enter your name here