Maybe you are a good website programmer, a born coder but you suck terribly bad in art and overall design. Maybe your job demands fast websites for clients to see and you can't waste time. Maybe you even are a talented artist, but you fight a never ending battle to make sure your website looks consistent in all browsers. Maybe your projects have become tangled with many sets of random tools and you just need a framework that encompasses all of your basic needs.
Bootstrap is one of the many solutions out there that will help you fix these problems! By the end of this tutorial, you should be able to answer the following questions on your own:
So, troops! Let's advance...
Twitter Bootstrap (mainly known as Bootstrap only) is a framework developed by Mark Otto and Jacob Thornton when they were creating Twitter (that is why some people call it Twitter Bootstrap).
Bootstrap is a fast, intuitive and powerful front-end framework for faster and easier web development. It gives you all the tools you need to make websites, web applications and prototypes.
Bootstrap has template files, as well as a lot of optional JavaScript extensions that allow you to make modal windows, drop down menus, tab controls and carousel controls, which are all styled in advance, saving you from the trouble.
As if that was not enough, it also comes with styles for buttons, forms, tables, typography and a collection of over 140 icons in both black and white versions.
Another very important feature of Bootstrap is that it comes with a 12 column grid system. If you don't know what that is, there is no problem - just know that it allows you to divide your website in equally spaced columns, thus making it look the same in all computers, with big screens, small screens, and even smartphones and tablets.
Bootstrap was originally designed to help provide some consistency across tools the guys were building internally. Since its first public (open source) release in 2011 (see the blog post here) it has found huge popularity among web developers for its ease of use and feature set, and in 2012 was the most popular project on GitHub. Its advocates praise Bootstrap’s ability to build usable applications, with clean code, quickly and easily.
Before Bootstrap, various libraries and tools were used for interface development and overall web design, which led to inconsistencies and resulted in code very hard to maintain over time. According to Twitter developer Mark Otto, in the face of those challenges:
(...) [A] super small group of developers and I got together to design and build a new internal tool and saw an opportunity to do something more. Through that process, we saw ourselves build something much more substantial than another internal tool. Months later, we ended up with an early version of Bootstrap as a way to document and share common design patterns and assets within the company. (...)
Bootstrap was born from the need to fix this problem, and it is this focus that made it the popular framework many know and use today.
At its heart, Twitter Bootstrap is basically simple good old CSS, but it uses the LESS pre-processor to add a lot more functionality than CSS could otherwise provide, together with JavaScript plugins and an HTML grid system. Because you can actually choose what you want to download from the web page, the package size is variable, giving you a lot of choice to make, or if you are like me, just download everything without second thoughts :P
There are two ways to get Bootstrap:
For version 2.3.2, you should get a folder named simply bootstrap. Inside this folder you should find the folders and files shown:
bootstrap/
├── css/
│ ├── bootstrap.css
│ ├── bootstrap.min.css
│ ├── bootstrap-responsive.css
│ └── bootstrap-responsive.min.css
├── js/
│ ├── bootstrap.js
│ └── bootstrap.min.js
└── img/
├── glyphicons-halflings.png
└── glyphicons-halflings-white.png
For this simple example, I am going to focus on the following functionalities of bootstrap:
Once we are done with this lesson, your website will look like this (click the image to visit the website!):
As you can see, this website comes loaded with some resources, namely images. Although I could use a link to the images I decided not to do so because that would be a bad practice, thus I downloaded all the necessary images into a nice folder. You can download all the necessary images by clicking the following links:
The full CSS file and HTML files can also be found here:
Before we start with the mini-guide, I want to make clear, that although this website does have a CSS file, the main focus of this lesson will be the Bootstrap code, which is all in the HTML file. That is the beauty of Bootstrap, you don't need to worry about anything else.
One important aspect of any web developer is how he/she organizes the files of the website. In my case, I will be using the following folder structure:
myBootstrapWebsite/
├── css/
│ └── style.css
├── html/
│ └── index.html
├── image/
│ ├── codecademy-logo-white.png
│ └── gridbg.gif
└── library/
└── bootstrap (folder, with all extracted files)
So, as you can see the structure of my website is quite simple. Just as any first example should be :P
Responsive design is an optional feature that you can enable in your website. It basically allows you to display your website in multiple devices of various screen sizes making it look consistent and readable without you having to change a single line of code. By detecting the size of the screen Bootstrap automatically adjusts your website in the best possible way.
This power however, should not be abused. Bootstrap official documentation defends that if you are ever to be serious about web development, you should make a version of your website for each device. For the purposes of this simple tutorial however, we will be using the responsive design feature, so you can see it in action.
To enable the responsive design feature to your website, you have to add the following piece of HTML code to the header:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="../library/bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
And you are done! Just remember to update the paths!
How can you test it? Well, that is easy. You can simply resize your browser to any size. In fact, you can try it with my example website.
This is what it looks like when I have responsive design enabled:
And this is what happens when I have responsive design disabled:
As you can see there is quite a difference. By just entering 2 lines of code, my website now looks presentable to a client in an Android Tablet or to a client in an iPhone.
The navigation bar is quite simple. The following code example is quite straight:
<div class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container-fluid">
<a class="brand" href="#"><img src="../image/codecademy-logo-white.png" width="111" height="30" alt="w3resource logo"></a>
<div class="nav-collapse">
<ul class="nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
<p class="navbar-text pull-right">Logged in as <a href="#">username</a></p>
</div>
</div>
</div>
</div>
So, let us now decompose what is happening. This is structured as a set of nested HTML <div></div>
elements, with some links <a ...></a>
and an unordered list <ul><li></li></ul>
. If you made the Web Fundamentals Track you should be quite familiar with these tags by now.
The amazing and important thing here, are the classes. They are what defines the style of everything in your page.
So, with no further delay, by reading the Twitter Bootstrap How-To guide, you can easily find out that:
navbar
provides the outside containing element for all contents of the navbar. navbar-inverse
calls for a dark navbar background with light font color.navbar-fixed-top
fixes the navbar at the top of the window and lets page content
scroll underneath it (this requires padding at the top of the body element. See it in
the embedded style sheet in the head of the index.html file.)navbar-inner
provides height, padding, and background color and gradients.nav-collapse
serves as the containing element for the menu
items which are dropped down in narrow-viewport navigation.class="active"
on the Home navigation item provides the darkened
background color which indicates the current page.container-fluid
is the fluid brother of container
and it constrains the width of the navbar contents, centers them within a wide screen, and includes a clearfix so that it will contain floated child elements.navbar-text pull-right
simply define that its a text field and we want it placed in the right.See? Reading the documentation actually helps! And this was pretty much easy :p
Bootstrap has two options for your 940 grid layout. One of them is the fixed grid layout. In this layout, everything is measured by pixels, and the website overall will look like what you expect if all the screens have a similar size or width-height ratio.
The other option, and the one I use, is the fluid grid. This fluid grid allows us to build fluid layouts. These layouts use %
instead of pixels for the measurements, and so everything will always scale accordingly, even if the screen sizes differ significantly or their ratio is not perfect.
Fixed and Fluid grids allows us to do Fixed and Fluid Layouts respectively (you can actually mix them, but you should never do that). The two have some basic differences and I understand that it may be confusing for some beginners. For this reason, I strongly recommend watching and reading the following sources:
Now that you probably understand better the differences between fixed and fluid layouts (and why I decided to use a fluid layout) let us delve into the code:
<div class="container-fluid">
<div class="row-fluid">
<div class="span3">
<!-- Stuff inside -->
</div>
<div class="span9">
<div class="row-fluid">
<div class="span3">
<!-- Stuff inside -->
</div>
<div class="span3">
<!-- Stuff inside -->
</div>
<div class="span3">
<!-- Stuff inside -->
</div>
</div>
<!-- More stuff inside, like footers and such -->
</div>
</div>
Presented here is the skeleton of the application, the Fluid Grid that I used. Assuming that you have seen the previous video on grid systems and that you played a little bit with the stackoverflow question that I presented earlier, this should be familiar.
First I define a container-fluid
, which will hold my rows, and then I create one giant row for the website. This row shall be the mother row. I divided the mother row into two spans, one with size 3 and another with size 9. The span3 portion will receive a sidebar, which we will discuss later. The span9 portion is a little more tricky. Inside this portion I add a leaderboard and create another row, dividing the later one evenly with three span4 tags.
To better understand this concept you should fire up Chrome Dev Tools, or in may case (Firefox Fanboy), Firebug!
Firebug will highlight the areas of the respective HTML tags that we have selected.
Mother Row:
As you can see, the mother row encompasses all.
Then we have the span3 (with a sidebar child tag) and span9 tags, which are children of the mother row:
span9 has a leaderboard (which we do not really care about) and another row.
And now, the row inside span9, has three span4 tags.
By now the Fluid layout on my website should be clear to you. If not, I strongly recommend that you either install and use Firebug, or use Chrome Dev Tools to toy around with the example.
With the skeleton base explain we can now go to the sidebar:
<div class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">Frontend</li>
<li class="active"><a href="#">HTML 4.01</a></li>
<li><a href="#">HTML5</a></li>
<li><a href="#">CSS</a></li>
<li><a href="#">JavaScript</a></li>
<li><a href="#">Twitter Bootstrap</a></li>
<li><a href="#">Flex</a></li>
<li><a href="#">JQuery</a></li>
<li><a href="#">Javascript</a></li>
<li class="nav-header">Backend</li>
<li><a href="#">PHP</a></li>
<li><a href="#">Ruby</a></li>
<li><a href="#">Python</a></li>
</ul>
</div>
The sidebar is quite simple as you can see. You create a sidebar by adding class=sidebar-nav
. The well
part is just to give a simple inset effect to the bar.
The content of the sidebar is an unordered list.The classes nav nav-list
set our sidebar with a content that we can "navigate" through.
Finally, nav-header
defines a header, or a separation if you will, on our bar.
And that is it! By pulling all of these concepts together you should now have your first Bootstrap website done! This website is responsive and fluid, so it will work in pretty much every device! Here we explored some of Bootstrap basic concepts, but trust me, your journey has only begun, there is a lot more for you to learn!
Bootstrap is quite a complex framework, and so you have a lot of learning to do. In this section we provide guides and links for you to learn more. If you find any resources you feel like sharing, please suggest them!
One of the main advantages of using a well known framework is the community support. This is very helpful in our case, mainly because it means we can use free template and themes from the Internet to develop our websites. Following are a few examples of websites that fill this niche:
There are far more free themes and templates in the Internet that you can explore, in fact these are just some of the top results from my Google search. If however you find something worth mentioning, please suggest it here so we can add it!
By now you probably think that Bootstrap is the holy grail. Well, Bootstrap is very good, but it also has many problems. Some of the mains hassles you will find when working are:
This tutorial served as a small introduction to Bootstrap. If you have never heard of it before then now you should be at least familiar with the basic concepts of the framework, and hopefully ready to expand your knowledge and fly on your own. If you have read / watched all of our recommendations, you should now be able to answer all of the introductory questions. Still, here is a small summary:
Here are more guides from me