Chapter 5 Bootstrap
Chapter 5 Bootstrap
Bootstrap is a sleek, intuitive, and powerful mobile first front-end framework for faster and easier web
development. It uses HTML, CSS and JavaScript.
History:
Bootstrap was developed by Mark Otto and Jacob Thornton at Twitter. It was released as an open-source
product in August 2011 on GitHub.
➢ Mobile first approach: Since Bootstrap 3, the framework consists of Mobile first styles throughout
the entire library instead of in separate files.
➢ Browser Support: It is supported by all popular browsers.
➢ Easy to get started: With just the knowledge of HTML and CSS anyone can get started with Bootstrap.
Also, the Bootstrap official site has a good documentation.
➢ Responsive design: Bootstrap's responsive CSS adjusts to Desktops, Tablets and Mobiles. More about
responsive design in the chapter Bootstrap Responsive Design
• Provides a clean and uniform solution for building an interface for developers.
• It contains beautiful and functional built-in components which are easy to customize.
➢ Scaffolding: Bootstrap provides a basic structure with Grid System, link styles, background. This is
covered in detail in the section Bootstrap Basic Structure.
➢ CSS: Bootstrap comes with feature of global CSS settings, fundamental HTML elements styled and
enhanced with extensible classes, and an advanced grid system. This is covered in detail in the section
Bootstrap with CSS.
➢ Components: Bootstrap contains over a dozen reusable components built to provide iconography,
dropdowns, navigation, alerts, popovers, and much more. This is covered in detail in the section
Layout Components.
➢ JavaScript Plugins: Bootstrap contains over a dozen custom jQuery plugins. You can easily include
them all, or one by one. This is covered in details in the section Bootstrap Plugins.
➢ Customize: You can customize Bootstrap's components, LESS variables, and jQuery plugins to get your
very own version.
Download Bootstrap
You can download the latest version of Bootstrap from http://getbootstrap.com/. When you click on this link,
you will get to see a screen as below:
BOOTSTRAP LAYOUT:
1. Containers
Use class .container to wrap a page's content and easily center the content's as shown below.
<div class="container">
...
</div>
Take a look at the .container class in bootstrap.css file:
.container {padding-right: 15px;
padding-left: 15px;
margin-right: auto;
margin-left: auto;
}
Note that, due to padding and fixed widths, containers are not nestable by default. Take a look at
bootstrap.css file:
@media (min-width: 768px)
{
.container
width: 750px;
}
Here you can see that CSS has media-queries for containers with width. This helps for applying
responsiveness and within those the container class is modified accordingly to render the grid system
properly.
If you do not want to use all 12 columns individually, you can group the columns together to create wider
columns:
span 1 span 1 span 1 span 1 span 1 span 1 span 1 span 1 span 1 span 1 span 1 spa
span 4 span 8
span 6 span 6
span 12
Bootstrap's grid system is responsive, and the columns will re-arrange depending on the screen size: On a big
screen it might look better with the content organized in three columns, but on a small screen it would be
better if the content items were stacked on top of each other.
Grid Classes
The Bootstrap grid system has four classes:
The classes above can be combined to create more dynamic and flexible layouts.
Tip: Each class scales up, so if you wish to set the same widths for xs and sm, you only
need to specify xs.
ADVERTISEMENT
<div class="container">
<div class="row">
<div class="col-*-*"></div>
<div class="col-*-*"></div>
</div>
<div class="row">
<div class="col-*-*"></div>
<div class="col-*-*"></div>
<div class="col-*-*"></div>
</div>
<div class="row">
...
</div>
</div>
So, to create the layout you want, create a container (<div class="container">). Next, create
a row (<div class="row">). Then, add the desired number of columns (tags with
appropriate .col-*-* classes). Note that numbers in .col-*-* should always add up to 12 for
each row.
3. Responsive Classes
For faster mobile-friendly development, use these utility classes for showing and hiding content by device via
media query. Also included are utility classes for toggling content when printed.
Try to use these on a limited basis and avoid creating entirely different versions of the same site. Instead, use
them to complement each device's presentation.
Available classes
Use a single or combination of the available classes for toggling content across viewport breakpoints.
As of v3.2.0, the .visible-*-* classes for each breakpoint come in three variations, one for each
CSS display property value listed below.
So, for extra small (xs) screens for example, the available .visible-*-* classes are: .visible-xs-block, .visible-xs-
inline, and .visible-xs-inline-block.
The classes .visible-xs, .visible-sm, .visible-md, and .visible-lg also exist, but are deprecated as of v3.2.0. They
are approximately equivalent to .visible-*-block, except with additional special cases for toggling <table>-
related elements.
4. Offset columns
Offsets are a useful feature for more specialized layouts. They can be used to push columns over for more
spacing, for example. The .col-xs=* classes don’t support offsets, but they are easily replicated by using an
empty cell. To use offsets on large displays, use the .col-md-offset-* classes. These classes increase the left
margin of a column by * columns where * range from 1 to 11. In the following example we have <div
class="col-md-6">..</div>, we will center this using class .col-md-offset-3.
<div class="container">
5. Column Reordering
Another nice feature of Bootstrap grid system is that you can easily write the columns in an order,
and show them in another one. You can easily change the order of built-in grid columns with .col-md-
push-* and .col-md-pull-* modifier classes where * range from 1 to 11. In the following example we
have two-columns layout with left column being the narrowest and acting as sidebar. We will swap
the order of these columns using .col-md-push-* and .col-md-pull-* classes.
<div class="container">
<h1>Hello, world!</h1>
<div class="row">
<p>Before Ordering</p>
<div class="col-md-4" style="background-color: #dedef8; box-shadow: inset 1px -1px 1px #444,
inset -1px 1px 1px #444;">
I am on left
</div>
<div class="col-md-8" style="background-color: #dedef8; box-shadow: inset 1px -1px 1px #444,
inset -1px 1px 1px #444;">
I am on right
</div>
</div> <br>
<div class="row">
<p>After Ordering</p>
<div class="col-md-4 col-md-push-8" style="background-color: #dedef8; box-shadow:
inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
I was on left
</div>
<div class="col-md-8 col-md-pull-4" style="background-color: #dedef8; box-shadow:
inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
I was on right
</div>
</div>
</div>
OUTPUT:
BOOTSTRAP CONTENT
1. Typography:
Bootstrap uses Helvetica Neue, Helvetica, Arial, and sans-serif in its default font stack. Using typography
feature of Bootstrap you can create headings, paragraphs, lists and other inline elements. Let see each one
these in the following sections:
Headings :
All HTML headings (h1 to h6) are styled in Bootstrap. An example is as shown below:
<h1>I'm Heading1 h1</h1>
<h2>I'm Heading2 h2</h2>
<h3>I'm Heading3 h3</h3>
<h4>I'm Heading4 h4</h4>
<h5>I'm Heading5 h5</h5>
<h6>I'm Heading6 h6</h6>
Above code segment with Bootstrap will produce following result:
NOTE: All html tag used in typography and run it.
2. Tables:
Bootstrap provides a clean layout for building
tables. Some of the table elements supported
by Bootstrap are:
Tag Description
Basic Table
If you want a nice, basic table style with just some light padding and horizontal dividers, add the base class of
.table to any table as shown in the following example:
<table class="table">
<caption>Basic Table Layout</caption>
<thead>
<tr>
<th>Name</th>
<th>City</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tanmay</td>
<td>Bangalore</td>
</tr>
<tr>
<td>Sachin</td>
<td>Mumbai</td>
</tr>
</tbody>
</table>
OUTPUT:
2.Images
Bootstrap 3 allows to make the images responsive by adding a class .img-responsive to the <img>tag. This
class applies max-width: 100%; and height: auto; to the image so that it scales nicely to the parent element.
Example:
<img src="..." class="img-responsive" alt="Responsive image">
3.Forms
Bootstrap provides you with following types of form layouts:
• Vertical (default) form
• Inline form
• Horizontal form
<form role="form">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" placeholder="Enter Name">
</div>
<div class="form-group">
<label for="inputfile">File input</label>
<input type="file" id="inputfile">
<p class="help-block">Example block-level help text here.</p>
</div>
<div class="checkbox">
<label> <input type="checkbox"> Check me out
</label>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
Output
INLINE FORM
To create a form where all of the elements are inline, left aligned and labels are alongside, add the class
.form-inline to the <form> tag.
<form class="form-inline" role="form">
<div class="form-group">
<label class="sr-only" for="name">Name</label>
<input type="text" class="form-control" id="name" placeholder="Enter Name">
</div>
<div class="form-group">
<label class="sr-only" for="inputfile">File input</label>
<input type="file" id="inputfile">
</div>
<div class="checkbox">
<label> <input type="checkbox"> Check me out </label>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
Output
➢ INPUTS
The most common form text field is the input—this is where users will enter most of the essential form data.
Bootstrap offers support for all native HTML5 input types: text, password, datetime, datetime-local, date,
month, time, week, number, email, url, search, tel, and color. Proper type declaration is required to make
Inputs fully styled.
EXAMPLE:
<form role="form">
<div class="form-group">
<label for="name">Label</label>
<input type="text" class="form-control" placeholder="Text input">
</div>
</form>
OUTPUT:
Bootstrap Components
1. Navigation
The Navbar is a nice feature, and is one of the prominent features of Bootstrap sites. Navbars are responsive
meta components that serve as navigation headers for your application or site. Navbars collapse in mobile
views and become horizontal as the available viewport width increases. At its core, the navbar includes
styling for site names and basic navigation.
➢ Default navbar
To create a default navbar:
OUTPUT:
➢ Responsive navbar
To add the responsive features to the navbar, the content that you want to be collapsed needs to be wrapped
in a <div> with classes .collapse, .navbar-collapse. The collapsing nature is tripped by a button that has a the
class of .navbar-toggle and then features two data- elements. The first, data-toggle, is used to tell the
JavaScript what to do with the button, and the second, data-target, indicates which element to toggle. Three
with a class of .icon-bar create what I like to call the hamburger button. This will toggle the elements that are
in the .nav-collapse <div>. For this feature to work, you need to include the Bootstrap Collapse Plugin.
Following example demonstrates this:
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#example-navbar-
collapse">
<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="#">TutorialsPoint</a>
</div>
<div class="collapse navbar-collapse" id="example-navbar-collapse">
<ul class="nav navbar-nav">
<li class="active">
<a href="#">iOS</a>
</li>
<li><a href="#">SVN</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"> Java
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="#">jmeter</a></li>
<li><a href="#">EJB</a></li>
<li><a href="#">Jasper Report</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
<li class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
</div>
</nav>
OUTPUT:
Horizontal Navigation
To turn the tabs into pills, follow the same steps as above, use the class .nav-pills instead of .nav-tabs.
Following example demonstrates this: <p>Pills Example</p>
3. Dropdown
This chapter will highlight about Bootstrap dropdown menus. Dropdown menus are toggleable,
contextual menus for displaying links in a list format.
This can be made interactive with the dropdown JavaScript plugin. To use dropdown, just wrap the
dropdown menu within the class .dropdown.
Following example demonstrates a basic dropdown menu:
<div class="dropdown">
<button type="button" class="btn dropdown-toggle" id="dropdownMenu1" data-
toggle="dropdown"> Topics
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
<li role="presentation">
<a role="menuitem" tabindex="-1" href="#">Java</a> </li>
<li role="presentation">
<a role="menuitem" tabindex="-1" href="#">Data Mining</a> </li>
<li role="presentation">
<a role="menuitem" tabindex="-1" href="#"> Data Communication/Networking </a> </li>
<li role="presentation" class="divider"></li>
<li role="presentation">
<a role="menuitem" tabindex="-1" href="#">Separated link</a> </li>
</ul>
</div>
OUTPUT:
4. Buttons
This chapter will discuss about how to use Bootstrap button with examples. Anything that is given a
class of .btn will inherit the default look of a gray button with rounded corners. However Bootstrap
provides some options to style buttons, which are summarized in the following table:
Class Description
Btn Default/ Standard button.
btn-primary Provides extra visual weight and
identifies the primary action in a set of
buttons.
btn-success Indicates a successful or positive action.
btn-info Contextual button for informational
alert messages.
btn-warning Indicates caution should be taken with
this action.
btn-danger Indicates a dangerous or potentially
negative action.
btn-link Deemphasize a button by making it look
like a link while maintaining button
behavior
5. Button Groups:
Button groups allow multiple buttons to be stacked together on a single line. This is useful when you
want to place items like alignment buttons together. You can add on optional JavaScript radio and
checkbox style behavior with Bootstrap Button Plugin. Following table summarizes the important
classes Bootstrap provides to use button groups:
EXAMPLE:
OUTPUT:
6. Breadcrumb
Breadcrumbs are a great way to show hierarchy-based information for a site. In the case of blogs,
breadcrumbs can show the dates of publishing, categories, or tags. They indicate the current page's
location within a navigational hierarchy. A breadcrumb in Bootstrap is simply an unordered list with a
class of .breadcrumb. The separator is automatically added by CSS (bootstrap.min.css) through the
following class:
OUTPUT:
7. Pagination.
Pagination, an unordered list is handled by Bootstrap like a lot of other interface elements.
The following table lists the classes Bootstrap provides to handle pagination.
DEFAULT PAGINATION
Following example demonstrates the use of class .pagination discussed in the above table:
<ul class="pagination">
<li><a href="#">«</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="#">5</a></li>
<li><a href="#">»</a></li>
</ul>
OUTPUT:
8. Labels
Labels are great for offering counts, tips, or other markup for pages. Use class.
label to display labels as shown in the following example:
<h1>Example Heading <span class="label label-default">Label</span></h1>
<h2>Example Heading <span class="label label-default">Label</span></h2>
<h3>Example Heading <span class="label label-default">Label</span></h3>
<h4>Example Heading <span class="label label-default">Label</span></h4>
OUTPUT:
You can the appearance of the labels using the modifier classes label-default, label-primary,
label-success, label-info, label-warning, label-danger as shown in the following example:
<span class="label label-default">Default Label</span>
<span class="label label-primary">Primary Label</span>
<span class="label label-success">Success Label</span>
<span class="label label-info">Info Label</span>
<span class="label label-warning">Warning Label</span>
<span class="label label-danger">Danger Label</span>
OUTPUT:
9. Alerts
Alerts provide a way to style messages to the user. They provide contextual feedback messages
for typical user actions.
You can add an optional close icon to alert. For inline dismissal use the Alerts jQuery plugin. You
can add an basic alert by creating a wrapper <div> and adding a class of .alert and one of the
four contextual classes (e.g., .alert-success, .alert-info, .alert-warning, .alert-danger).
This will animate the stripes right to left. Let us see an example below:
<div class="progress progress-striped active">
<div class="progress-bar progress-bar-success" role="progressbar" aria-
valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width:
40%;">
<span class="sr-only">40% Complete</span>
</div> </div>
OUTPUT:
Bootstrap Utilities
1. Colors
As described in the Colors chapter, here is a list of all text and background color
classes:
The classes for text colors are: .text-muted, .text-primary, .text-success, .text-
info, .text-warning, .text-danger, .text-secondary, .text-white, .text-dark, .text-
body (default body color/often black) and .text-light:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet">
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"><
/script>
</head>
<body>
<h2>Contextual Colors</h2>
</div>
</body>
</html>
OUTPUT:
2.Background Colors
The classes for background colors are: .bg-primary, .bg-success, .bg-info, .bg-
warning, .bg-danger, .bg-secondary, .bg-dark and .bg-light.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
<h2>Contextual Backgrounds</h2>
<p>Use the contextual background classes to provide "meaning through colors".</p>
<div class="bg-primary p-3"></div>
<div class="bg-success p-3"></div>
<div class="bg-info p-3"></div>
<div class="bg-warning p-3"></div>
<div class="bg-danger p-3"></div>
<div class="bg-secondary p-3"></div>
<div class="bg-dark p-3"></div>
<div class="bg-light p-3"></div>
</div>
</body>
</html>
OUTPUT:
3.Borders
Use the border classes to add or remove borders from an element:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet">
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></scri
pt>
<style>
span {
display: inline-block;
width: 70px;
height: 70px;
margin: 6px;
background-color: #f9f9f9;
</style>
</head>
<body>
<h2>Borders</h2>
<span class="border"></span>
<span class="border-top"></span>
<span class="border-end"></span>
<span class="border-bottom"></span>
<span class="border-start"></span>
</div>
</body>
</html>
OUTPUT:
4.Overflow
Barebones overflow functionality is provided for two values by default, and they are not responsive.
<div class="overflow-auto">...</div>
<div class="overflow-hidden">...</div>
5. Position
Use these shorthand utilities for quickly configuring the position of an element.
Common values
Quick positioning classes are available, though they are not responsive.
<div class="position-static">...</div>
<div class="position-relative">...</div>
<div class="position-absolute">...</div>
<div class="position-fixed">...</div>
<div class="position-sticky">...</div>
Fixed top
Position an element at the top of the viewport, from edge to edge. Be sure you understand the
ramifications of fixed position in your project; you may need to add additional CSS.
<div class="fixed-top">...</div>
Fixed bottom
Position an element at the bottom of the viewport, from edge to edge. Be sure you understand the
ramifications of fixed position in your project; you may need to add additional CSS.
<div class="fixed-bottom">...</div>
6. Spacing
Bootstrap includes a wide range of shorthand responsive margin and padding utility classes to
modify an element’s appearance.
How it works
Assign responsive-friendly margin or padding values to an element or a subset of its sides with
shorthand classes. Includes support for individual properties, all properties, and vertical and
horizontal properties. Classes are built from a default Sass map ranging from .25rem to 3rem.
Notation
Spacing utilities that apply to all breakpoints, from xs to xl, have no breakpoint abbreviation in
them. This is because those classes are applied from min-width: 0 and up, and thus are not bound
by a media query. The remaining breakpoints, however, do include a breakpoint abbreviation.
The classes are named using the format {property}{sides}-{size} for xs and {property}{sides}-
{breakpoint}-{size} for sm, md, lg, and xl.
(You can add more sizes by adding entries to the $spacers Sass map variable.)
Examples
Here are some representative examples of these classes:
.mt-0 {
margin-top: 0 !important;
}
.ml-1 {
margin-left: ($spacer * .25) !important;
}
.px-2 {
padding-left: ($spacer * .5) !important;
padding-right: ($spacer * .5) !important;
}
.p-3 {
padding: $spacer !important;
}
7.Vertical Align
Use the align- classes to change the alignment of elements (only works on inline, inline-
block, inline-table and table cell elements):
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
</body>
</html>
OUTPUT: