Click here to Skip to main content
Click here to Skip to main content

Create Clean Webpages with Twitter Bootstrap

, 10 Dec 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Twitter Bootstrap is a lean and clean framework for website UIs. See what it can do and how it can help you!

Introduction  

Do you need to a lay out a webpage in a clean and straightforward way? Do you find yourself Googling how to deal with tabs, menus, popups, forms and other UI elements? Do you find other layouts and samples and end up ripping out the basics to get started? If so, then Twitter Bootstrap may be useful to you. It's a lean User Interface framework for building webpages that render consistently in desktop browsers, smartphones, tablets and more.

Bootstrap is a very good framework to get a page up and running quickly - and provides plenty of advanced features. A bit of understanding of the basics will prove to be very useful in a whole variety of scenarios.

In this article, I'm going to show you how to get started with Bootstrap, and why you should bother. 

What're We Going To Use?   

We're going to be dealing with very basic code and only a few files - however, for the screenshots and example project I'll use Visual Studio 2012. You can use whatever editors or environments you feel most comfortable with - Studio is a quick and easy way for me to package all of the samples together. 

Creating the Project 

Let's create an empty web project: 

We should have something pretty clean and lean, like so: 

Now head to Twitter Bootstrap's site and download the package: 

The bootstrap download contains three folders - css, img and js. You can extract these folders into your website. I tend to use a folder per third party component, but many people just have one css, images and scripts folder per site. 

Now let's create the most basic example we can, create a new page in the site root called 'index.html' and write out the clean HTML 5 below: 

<!DOCTYPE html>
<html>
<head>
    <title>Twitter Bootstrap Examples</title>
</head>
<body>
    <h1>Twitter Bootstrap</h1>
    <p>This is the first example!</p>
</body>
</html>

This is the basic project structure. Next we'll tie in Bootstrap. 

Setting up Bootstrap 

First, we need to include jQuery. If you haven't used it before, jQuery is a nearly ubiquitous Javascript library that helps you deal with the really common things you come across in web pages. Add the line that's in bold below. 

<!DOCTYPE html>
<html>
<head>
    <title>Twitter Bootstrap Examples</title>
</head>
<body>
    <h1>Twitter Bootstrap</h1>
    <p>This is the first example!</p>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
</body>
</html> 

Now add the CSS for Bootstrap and the script.

<!DOCTYPE html>
<html>
<head>
    <title>Twitter Bootstrap Examples</title>
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<body>
    <h1>Twitter Bootstrap</h1>
    <p>This is the first example!</p>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>    

Quick Tip If you are using Visual Studio 2012 you might not have yet used the 'page inspector' give it a try - right on the document and press 'View in Page Inspector' or press Ctrl + G, Ctrl + K. This'll show the page, live in Visual Studio to the left of the HTML.

Run the page, we've got the same content as before but styled slightly differently. 

Actually including the Bootstrapper CSS styles the basic content like the headings and the paragraph text. In fact, you can check out the Bootstrap CSS page for details on how the basic elements are styled.

The Most Important Elements 

Next we're going to look at some of the most important elements we'll see that'll be common to most pages. We'll add these elements to our base page - then we'll create some special pages with other elements on. 

A Container

Yup - this is very important. A container is an element for the main content that's going to position it properly, typically we're going to use this just to give the content a sensible width.

    <body>
        
        <!-- The main container. -->
        <div class="container">
            <h1>Twitter Bootstrap</h1>
            <p>This is the first example!</p>
        </div>
        
        <!-- Include jQuery and Boostrap. -->
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script src="bootstrap/js/bootstrap.min.js"></script>
 
    </body> 

Nothing too special here - it's a div with the 'container' class. This'll centre the content and give it a sensible width that most screens will handle.  

 

A Menu Bar  

Your page will probably need some kind of menu bar, let's deal with that now. 

        <!-- The main container. -->
        <div class="container">
            
            <!-- A menu bar. -->
            <div class="navbar">
                <div class="navbar-inner">
                    <a class="brand" href="#">Example Page</a>
                    <ul class="nav">
                        <li class="active"><a href="#">Home</a></li>
                        <li><a href="#">About</a></li>
                        <li><a href="#">Contact</a></li>
                    </ul>
                </div>
            </div>
 
            <h1>Twitter Bootstrap</h1>
            <p>This is the first example!</p>
 
        </div> 

This is pretty much the leanest example I could come up with. Here we've got a menu bar (or navigation bar in the Bootstrapper parlance) with a 'brand' element (normally the page title) and three links.

Simple! That's the basic navigation bar. You can also (deep breath):

  • Stretch it across the top
  • Align it differently
  • Invert the colour scheme to make it dark
  • Include a search bar
  • Include a search form
  • Include a drop down 
  • Fix it to the top of the screen
And this is just the beginning. Find out how to use the more advanced features on the Components - Navbar Page.  

A Grid System  

Bootstrapper gives you easy access to a 12 column grid system. This means that you can create a row, then mark a div as spanning a certain number of columns in an imaginary system of 12 equally sized columns, like this: 

Here's how it'd work: 

<!-- Example row, with four columns. -->
<div class="row">
    <div class="span3">
        <h2>Apple</h2>
        <p>The apple is the pomaceous fruit of the apple tree, species Malus domestica in the rose family.</p>
    </div>
    <div class="span3">
        <h2>Orange</h2>
        <p>The orange (specifically, the sweet orange) is the fruit of the citrus Citrus × ​sinensis, species Citrus × ​sinensis in the family Rutaceae.</p>
    </div>
    <div class="span3">
        <h2>Peach</h2>
        <p>The peach, Prunus persica, is a deciduous tree, native to China and South Asia, where it was first cultivated.</p>
    </div>
    <div class="span3">
        <h2>Pear</h2>
        <p>The pear is any of several tree and shrub species of genus Pyrus, in the family Rosaceae.</p>
    </div>
</div>
 
<hr />
 
 <!-- Another row, with two columns. -->
<div class="row">
    <div class="span6">
        <h2>Cat</h2>
        <p>The domestic cat (Felis catus or Felis silvestris catus) is a small, usually furry, domesticated, carnivorous mammal.</p>
    </div>
    <div class="span6">
        <h2>Dog</h2>
        <p>The domestic dog (Canis lupus familiaris), is a subspecies of the gray wolf (Canis lupus), a member of the Canidae family of the mammalian order Carnivora.</p>
    </div>
</div>

This'll render like below: 


You're going to find this really useful, a decent layout system is critical for a clean looking site. By the way, if you're interested in using a system like this in Silverlight, check out my article 'Grid960 Layout for Silverlight'. 

Other Highlights

Twitter Bootstrap is well featured, there's a lot of stuff there. In this section I'll take you through a few personal favourites. In each case I'll add them to part of the the sample page.

Tab Controls  

It was the easy to use Tab Control that first attracted me to the framework. Here's how we can create, for example, three tabs:

<!-- The tab headings. -->
<ul class="nav nav-tabs">
    <li class="active"><a href="#homer" data-toggle="tab">Homer</a></li>
    <li><a href="#marge" data-toggle="tab">Marge</a></li>
    <li><a href="#grandpa" data-toggle="tab">Grandpa</a></li>
</ul>
 
<!-- The tab content. -->
<div id="my-tab-content" class="tab-content">
 
    <div class="tab-pane active" id="homer">
        <p>Homer Simpson</p>
    </div>
 
    <div class="tab-pane" id="marge">
        <p>Marge Simpson</p>
    </div>
 
    <div class="tab-pane" id="grandpa">
        <p>Grandpa Simpson</p>
    </div>
</div>

These tabs are easy to use and can also be customised extensively. 

 

Popups

Popups (or as they're called in Bootstrapper, Modals) can be really useful. Look at the code below:

<!-- Button to trigger modal -->
<a href="#popup" role="button" class="btn" data-toggle="modal">Login</a>
 
<!-- Modal -->
<div id="popup" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="popupLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="popupLabel">Login</h3>
    </div>
    <div class="modal-body">
        <p>Enter login details...</p>
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
        <button class="btn btn-primary">Login</button>
    </div>
</div>

This is small and clean markup to get such nice results, as below:

Pagination Controls 

You'll use them again and again...

<div class="pagination">
    <ul>
        <li><a href="#">Prev</a></li>
        <li><a href="#">1</a></li>
        <li><a href="#">2</a></li>
        <li><a href="#">3</a></li>
        <li><a href="#">4</a></li>
        <li><a href="#">Next</a></li>
    </ul>
</div>

This gives you the below:

Conclusion

Much more and I'll just be duplicating what's on the Twitter Bootstrap main page - this article was really to highlight how quickly you can get started with the Framework. Hopefully you'll see that for those common webpage tasks, this library is a really good thing to know about! 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Dave Kerr
Software Developer
United Kingdom United Kingdom
Follow my blog at www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.
Follow on   Twitter

Comments and Discussions

 
QuestionGood article [modified] PinmemberMember 16562146-Sep-13 7:59 
AnswerRe: Good article PinmvpDave Kerr6-Sep-13 13:14 
GeneralMy vote of 5 Pinmemberhemantrautela5-Jul-13 21:48 
GeneralRe: My vote of 5 PinmvpDave Kerr6-Sep-13 13:12 
GeneralMy vote of 5 Pinmembersoujiro seta14-Jun-13 6:28 
GeneralRe: My vote of 5 PinmvpDave Kerr14-Jun-13 7:21 
Thanks Smile | :)

SuggestionNice article. Pinmember GeekBond 10-May-13 6:23 
GeneralRe: Nice article. PinmvpDave Kerr10-May-13 10:06 
GeneralMy vote of 5 PinmentorDaveAuld30-Jan-13 2:55 
GeneralRe: My vote of 5 PinmvpDave Kerr10-May-13 10:08 
GeneralMy vote of 5 PinmemberRenju Vinod15-Jan-13 21:27 
GeneralRe: My vote of 5 PinmvpDave Kerr15-Jan-13 21:41 
GeneralMy vote of 5 PinmemberMihai MOGA15-Jan-13 7:56 
GeneralRe: My vote of 5 PinmvpDave Kerr15-Jan-13 8:42 
GeneralMy vote of 5 PinmemberNajeeb Shaikh17-Dec-12 22:34 
GeneralRe: My vote of 5 PinmvpDave Kerr17-Dec-12 23:36 
GeneralMy vote of 4 Pinmembersaxenaabhi613-Dec-12 20:10 
GeneralRe: My vote of 4 PinmvpDave Kerr13-Dec-12 21:17 
GeneralRe: My vote of 4 Pinmemberlaverrod14-Dec-12 2:35 
GeneralMy vote of 4 PinmemberGianmaria Gregori12-Dec-12 13:28 
GeneralRe: My vote of 4 PinmvpDave Kerr12-Dec-12 22:48 
QuestionMVC Twitter Pinmembermbowles20111-Dec-12 9:07 
AnswerRe: MVC Twitter PinmvpDave Kerr11-Dec-12 11:24 
Questionregarding Twitter-Bootstrap advantage PinmemberTridip Bhattacharjee10-Dec-12 20:44 
AnswerRe: regarding Twitter-Bootstrap advantage PinmvpDave Kerr10-Dec-12 22:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 10 Dec 2012
Article Copyright 2012 by Dave Kerr
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid