Responsive Website Using BootStrap



Bootstrap 3 tutorial. If you have not used BootStrap so far, or more than that – have not heard about it – I would recommend you take a closer look to this framework. The fact is that this is really a great choice for those who appreciate the convenience and speed of website development. Today I will tell you about how to create a responsive template using BootStrap 3, which will include elements such as: two navigational menus, slider, promo block, a variety of content blocks, various form elements and footer. One more important moment – in this template, we do not use images, it means that our result is focused on speed.



What does the “Responsiveness” mean?

Responsive Web design is a Web design approach aimed at crafting sites to provide an optimal viewing experience – easy reading and navigation with a minimum of resizing, panning, and scrolling—across a wide range of devices (from mobile phones to desktop computer monitors).

A site designed with Responsive Web design adapts the layout to the viewing environment by using fluid, proportion-based grids, flexible images, CSS3 media queries, an extension of the @media rule.

The fluid grid concept calls for page element sizing to be in relative units like percentages, rather than absolute units like pixels or points.

Flexible images are also sized in relative units, so as to prevent them from displaying outside their containing element.

Media queries allow the page to use different CSS style rules based on characteristics of the device the site is being displayed on, most commonly the width of the browser.

Preview:

Step 1. HTML

Before you start experimenting with BootStrap framework, let’s prepare a basic html model:

index.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="author" content="Script Tutorials" /> <meta name="description" content="Responsive Websites Using BootStrap - demo page"> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> <title>Responsive Websites Using BootStrap | Script Tutorials</title> <!-- css stylesheets --> <link href="css/bootstrap.min.css" rel="stylesheet"> <link href="css/style.css" rel="stylesheet"> </head> <body> <!-- BODY PAGE CONTENT --> <!-- add javascripts --> <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script> <script src="js/bootstrap.min.js"></script> </body> </html>

This is the minimal layout which enables the responsive nature of Twitter Bootstrap. In the header we included all the important meta tags and two CSS files, before the closing body, we added both: jQuery and the minimized bootstrap core. This speeds up the loading of the page. Now we can begin with every element of the page.

Top navigation menu

This menu is in the very top: this is fixed navigation bar. Here is the markup:

<!-- fixed navigation bar --> <div class="navbar navbar-fixed-top navbar-inverse" role="navigation"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#b-menu-1"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">Bootstrap website</a> </div> <div class="collapse navbar-collapse" id="b-menu-1"> <ul class="nav navbar-nav navbar-right"> <li class="active"><a href="#">Menu 1</a></li> <li><a href="#">Menu 2</a></li> <li><a href="#">Menu 3</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-user"></span><b class="caret"></b></a> <ul class="dropdown-menu"> <li><a href="#">Option 1</a></li> <li><a href="#">Option 2</a></li> <li><a href="#">Option 3</a></li> </ul> </li> </ul> </div> <!-- /.nav-collapse --> </div> <!-- /.container --> </div> <!-- /.navbar -->

Generally, it consists of three parts: the hidden button (for mobile device to open the menu), navbar-brand (brand/title) element, and UL-LI-based drop-down menu.

Slider

Right after the top menu, there is the slider: the carousel which turns its items. Its layout is quite familiar:

<!-- slider --> <div id="slider" class="carousel slide" data-ride="carousel"> <!-- controls --> <ol class="carousel-indicators"> <li data-target="#slider" data-slide-to="0" class="active"></li> <li data-target="#slider" data-slide-to="1"></li> <li data-target="#slider" data-slide-to="2"></li> </ol> <div class="carousel-inner"> <!-- slides --> <div class="item active"> <div class="container"> <div class="carousel-caption"> <h1>Lorem ipsum - 1</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sit amet tempus massa. Nam quis purus sit amet augue iaculis dapibus non in nisi.<br />Sed sed volutpat neque. Nulla posuere.</p> <p><a class="btn btn-lg btn-default" href="#" role="button">Button 1</a></p> </div> </div> </div> <div class="item"> <div class="container"> <div class="carousel-caption"> <h1>Lorem ipsum - 2</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sit amet tempus massa. Nam quis purus sit amet augue iaculis dapibus non in nisi.<br />Sed sed volutpat neque. Nulla posuere.</p> <p><a class="btn btn-lg btn-primary" href="#" role="button">Button 2</a></p> </div> </div> </div> <div class="item"> <div class="container"> <div class="carousel-caption"> <h1>Lorem ipsum - 3</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sit amet tempus massa. Nam quis purus sit amet augue iaculis dapibus non in nisi.<br />Sed sed volutpat neque. Nulla posuere.</p> <p><a class="btn btn-lg btn-warning" href="#" role="button">Button 3</a></p> </div> </div> </div> </div> <!-- left-right controls --> <a class="left carousel-control" href="#slider" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span></a> <a class="right carousel-control" href="#slider" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span></a> </div><!-- /.carousel -->

The familiar – this is because it contains quite usual elements: the ability to switch slides (indicators), the buttons back and forth, and the slides themselves.

Modal box

Modal boxes are quite an important part of the user interface, so we’ll create one modal box:

<!-- modal box --> <div class="modal fade" id="my-modal-box" tabindex="-1" role="dialog" aria-labelledby="my-modal-box-l" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <div class="modal-title" id="my-modal-box-l"> <h3>Share it</h3> </div> </div><!-- /.modal-header --> <div class="modal-body"> <p>Share it box content</p> <!-- You may add AddThis code here --> </div><!-- /.modal-body --> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div><!-- /.modal -->

Second navigation menu

Finally we come to the main point – the main container which consists of the second navigation menu, footer and additional content blocks. The second menu is not fixed, this is a similar UL-LI-based menu, but the most right element calls the pre-made modal box:

<!-- second menu bar --> <nav class="navbar navbar-default navbar-static"> <div class="navbar-header"> <button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#b-menu-2"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="https://www.script-tutorials.com/responsive-website-using-bootstrap/">Bootstrap website</a> </div> <!-- submenu elements for #b-menu-2 --> <div class="collapse navbar-collapse" id="b-menu-2"> <ul class="nav navbar-nav"> <li class="active"><a href="#"><span class="glyphicon glyphicon-home"></span> Home</a></li> <li><a href="#"><span class="glyphicon glyphicon-question-sign"></span> Help</a></li> <li><a href="#"><span class="glyphicon glyphicon-exclamation-sign"></span> About</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-list"></span> Other <b class="caret"></b></a> <ul class="dropdown-menu"> <li><a href="#">Submenu 1</a></li> <li><a href="#">Submenu 2</a></li> <li><a href="#">Submenu 3</a></li> <li><a href="#">Submenu 4</a></li> <li class="divider"></li> <li><a href="#">Submenu 5</a></li> <li><a href="#">Submenu 6</a></li> <li class="divider"></li> <li><a href="#">Submenu 7</a></li> </ul> </li> </ul> <ul class="nav navbar-nav navbar-right"> <li data-toggle="modal" data-target="#my-modal-box"><a href="#"><span class="glyphicon glyphicon-share"></span> Share popup</a></li> </ul> </div><!-- /.nav-collapse --> </nav>

Two column layout with blocks

This section is quite huge, but it is rather simple: blocks are repeated. Just pay attention to my comments:

<!-- 2-column layout --> <div class="row row-offcanvas row-offcanvas-right"> <div class="col-xs-12 col-sm-9"> <!-- jumbotron --> <div class="jumbotron"> <h1>Lorem ipsum</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sit amet tempus massa. Nam quis purus sit amet augue iaculis dapibus non in nisi. Sed sed volutpat neque. Nulla posuere.</p> <p><a role="button" href="#" class="btn btn-lg btn-success">Sign up today</a></p> </div> <div class="row"> <!-- column 1 --> <div class="col-sm-6"> <!-- box 1 --> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">Panel 1 title</h3> </div> <div class="panel-body"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sit amet tempus massa. Nam quis purus sit amet augue iaculis dapibus non in nisi. Sed sed volutpat neque. Nulla posuere.</p> <p><a class="btn btn-default right" href="#" role="button">Continue »</a></p> </div> </div> <!-- box 2 --> <div class="panel panel-primary"> <div class="panel-heading"> <h3 class="panel-title">Panel 2 title</h3> </div> <div class="panel-body"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sit amet tempus massa. Nam quis purus sit amet augue iaculis dapibus non in nisi. Sed sed volutpat neque. Nulla posuere.</p> <p><a class="btn btn-default right" href="#" role="button">Continue »</a></p> </div> </div> </div> <!-- /column 1 --> <!-- column 2 --> <div class="col-sm-6"> <!-- box 3 --> <div class="panel panel-success"> <div class="panel-heading"> <h3 class="panel-title">Panel 3 title</h3> </div> <div class="panel-body"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sit amet tempus massa. Nam quis purus sit amet augue iaculis dapibus non in nisi. Sed sed volutpat neque. Nulla posuere.</p> <p><a class="btn btn-default right" href="#" role="button">Continue »</a></p> </div> </div> <!-- box 4 --> <div class="panel panel-warning"> <div class="panel-heading"> <h3 class="panel-title">Panel 4 title</h3> </div> <div class="panel-body"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sit amet tempus massa. Nam quis purus sit amet augue iaculis dapibus non in nisi. Sed sed volutpat neque. Nulla posuere.</p> <p><a class="btn btn-default right" href="#" role="button">Continue »</a></p> </div> </div> </div> <!-- /column 2 --> </div><!--/row--> </div><!--/span--> <!-- column 3 (sidebar) --> <div class="col-sm-3 sidebar-offcanvas" id="sidebar"> <div class="list-group" role="navigation"> <a href="#" class="list-group-item">Link</a> <a href="#" class="list-group-item">Link</a> <a href="#" class="list-group-item active">Link</a> <a href="#" class="list-group-item">Link</a> <a href="#" class="list-group-item">Link</a> </div> <!-- box 5 --> <div class="panel panel-danger"> <div class="panel-heading"> <h3 class="panel-title">Form elements</h3> </div> <div class="panel-body"> <form role="form"> <div class="form-group"> <label for="emailField">Email address</label> <input type="email" class="form-control" id="emailField" placeholder="Enter email"> </div> <div class="form-group"> <label for="selectField">Type</label> <select class="form-control" id="selectField"> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> </select> </div> <div class="progress progress-striped active"> <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width:30%"> <span class="sr-only">30% Complete</span></div> </div> <button type="submit" class="btn btn-default">Submit</button> </form> </div> </div> <!-- box 6 --> <div class="panel panel-info"> <div class="panel-heading"> <h3 class="panel-title">Panel 6 title</h3> </div> <div class="panel-body"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sit amet tempus massa. Nam quis purus sit amet augue iaculis dapibus non in nisi. Sed sed volutpat neque. Nulla posuere.</p> <p><a class="btn btn-default right" href="#" role="button">Continue »</a></p> </div> </div> </div><!-- /column 3 (sidebar) --> </div><!--/row-->

In the beginning, the whole layout is divided into two columns, but then, the first column is split to another two columns. In the first column, there is the ‘jumbotron’ element, which you can use to put some introduction in there. As I remarked before – all blocks are very similar, here is the markup of the basic block:

<div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">Panel 1 title</h3> </div> <div class="panel-body"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sit amet tempus massa. Nam quis purus sit amet augue iaculis dapibus non in nisi. Sed sed volutpat neque. Nulla posuere.</p> <p><a class="btn btn-default right" href="#" role="button">Continue »</a></p> </div> </div>

In order to change it’s color, you may change its class (panel-default) to one of the following: panel-primary, panel-success, panel-info, panel-warning or panel-danger.

Footer

There is only one unexplained element – footer

<footer> <nav class="navbar navbar-default navbar-static-bottom" role="navigation"> <p class="navbar-text">© Copyright 2013</p> <p class="navbar-text navbar-right"><a href="https://www.script-tutorials.com/">Script Tutorials</a></p> </nav> </footer>

Step 2. CSS

By and large – all the things that we’ve created – does not require special styles. Most of possible elements are already defined in BootStrap stylesheet file (bootstrap.min.css). However, there is only one customized element – the slider (carousel):

css/style.css

.carousel { height: 300px; margin-bottom: 50px; } .carousel-caption { z-index: 10; } .carousel .item { background-color: rgba(0, 0, 0, 0.8); height: 300px; }

[sociallocker]

[/sociallocker]

Conclusion

In the process of learning the BootStrap framework, we have come to an end. Basically, we only prepared the necessary layout of the page, all the rest has done by this framework. Wonderful, is not it? Thank you for reading this tutorial. Don’t hesitate to share it with your friends using the form below.