Bootstrap3.3.7 Tutorial
Bootstrap3.3.7 Tutorial
Bootstrap3.3.7 Tutorial
Bootstrap is the most popular and powerful front-end (HTML, CSS, and JS)
framework for faster and easier responsive web development.
Bootstrap tutorials will help you to learn the fundamentals as well as the advanced features of the
latest Bootstrap framework step-by-step, so that you can create responsive, interactive and
feature rich websites with much less effort.
Tutorials are broken down into sections where each section containing a number of related topics
that are packed with easy to understand explanations, real-life practice examples, smart
workarounds as well as useful tips and important notes.
You can save a lot of time and efforts with Bootstrap — So bookmark this website and continue
on.
If you're completely new to the Bootstrap we recommend you to start with the section
that covers Bootstrap Basics and gradually move forward, by learning a little bit every
day.
What is Bootstrap
Bootstrap is a powerful front-end framework for faster and easier web development. It
includes HTML and CSS based design templates for common user interface components
like Typography, Forms, Buttons, Tables, Navigations, Dropdowns, Alerts, Modals, Tabs,
Accordion, Carousel and many other as well as optional JavaScript extensions.
Bootstrap also gives you ability to create responsive layout with much less efforts.
Advantages of Bootstrap
1
The biggest advantage of using Bootstrap is that it comes with free set of tools for
creating flexible and responsive web layouts as well as common interface components.
Additionally, using the Bootstrap data APIs you can create advanced interface
components like Scrollspy and Typeaheads without writing a single line of JavaScript.
Here are some more advantages, why one should opt for Bootstrap:
Save lots of time — You can save lots of time and efforts using the Bootstrap predefined
design templates and classes and concentrate on other development work.
Responsive features — Using Bootstrap you can easily create responsive designs.
Bootstrap responsive features make your web pages to appear more appropriately on different
devices and screen resolutions without any change in markup.
Consistent design — All Bootstrap components share the same design templates and
styles through a central library, so that the designs and layouts of your web pages are consistent
throughout your development.
Easy to use — Bootstrap is very easy to use. Anybody with the basic working knowledge
of HTML and CSS can start development with Bootstrap.
Compatible with browsers — Bootstrap is created with modern browsers in mind and it
is compatible with all modern browsers such as Mozilla Firefox, Google Chrome, Safari, Internet
Explorer, and Opera.
Open Source — And the best part is, it is completely free to download and use.
2
Once downloaded the compiled Bootstrap, unzip the compressed folder to see the
structure. You'll find the following file structure and contents.
bootstrap/
|—— css/
| |—— bootstrap.css
| |—— bootstrap.min.css
| |—— bootstrap-theme.css
| |—— bootstrap-theme.min.css
|—— js/
| |—— bootstrap.js
| |—— bootstrap.min.js
|—— fonts/
| |—— glyphicons-halflings-regular.eot
| |—— glyphicons-halflings-regular.svg
| |—— glyphicons-halflings-regular.ttf
| |—— glyphicons-halflings-regular.woff
As you can see compiled version of Bootstrap provides compiled CSS and JS files
(bootstrap.*), as well as compiled and minified CSS and JS (bootstrap.min.*).
There are four font files (glyphicons-halflings-regular.*) inside the fonts folder. These
fonts file includes more than 250 icons from the Glyphicon Halflings set.
Tip:This is the most basic form of Bootstrap for quick usage in any web project. Please note that
Bootstrap's JavaScript plugins require jQuery to be included. You can download the latest
version of jQuery form here https://jquery.com/download/
Example
3
Try this code »
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Basic HTML File</title>
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html>
Tip:Always include the viewport <meta> tag inside the <head> section of your document to enable
touch zooming and ensure proper rendering on mobile devices. Also include the X-UA-
Compatible meta tag with edge mode, which tells the Internet Explorer to display the webpage in
the highest mode available.
Step 2: Making this HTML File a Bootstrap Template
To make this HTML file a Bootstrap template, just include the appropriate Bootstrap CSS
and JS files. You should include JavaScript files at the bottom of the page — before
closing of the <body>tag (i.e. </body>) to improve the performance of your web pages.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Basic Bootstrap Template</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<!-- Optional Bootstrap theme -->
<link rel="stylesheet" href="css/bootstrap-theme.min.css">
</head>
<body>
<h1>Hello, world!</h1>
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
And we're all set! after adding the Bootstrap's CSS and JS files and the required jQuery
library, we can begin to develop any site or application with Bootstrap framework.
Step 3: Saving the file
Now save the file on your desktop as "bootstrap-template.html".
Note:It is important that the extension .html is specified — some text editors, such as Notepad,
will automatically save it as .txt otherwise.
4
To open the file in a browser. Navigate to your file then double click on it. It will open in
your default Web browser. (If it does not, open your browser and drag the file to it.)
Example
Try this code »
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Basic Bootstrap Template</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
<!-- Optional Bootstrap theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-
theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
crossorigin="anonymous">
</head>
<body>
<h1>Hello, world!</h1>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-
Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
</body>
</html>
In the above example, we've included the compiled and minified version of Bootstrap's
CSS and JavaScript files as well as the necessary jQuery library using the CDN links.
You'll also find these CDN links in most of the practice examples code throughout this
site.
The attributes integrity and crossorigin have been added to Bootstrap CDN to
implement Subresource Integrity (SRI). It is a security feature that enables you to
5
mitigate the risk of attacks originating from compromised CDNs, by ensuring that the
files your website fetches (from a CDN or anywhere) have been delivered without
unexpected manipulation. It works by allowing you to provide a cryptographic hash that
a fetched file must match.
Features Extra small devices Small devices Medium devices Large devices
Bootstrap 3 Grid System Phones (<768px) Tablets (≥768px) Desktops (≥992px) Desktops (≥1200px)
6
Creating Two Column Layouts
The following example will show you how to create two column layouts for small,
medium and large devices like tables, laptops and desktops etc. However, on mobile
phones, the columns will automatically become horizontal as default.
Example
Try this code »
<div class="container">
<!--Row with two equal columns-->
<div class="row">
<div class="col-sm-6"><!--Column left--></div>
<div class="col-sm-6"><!--Column right--></div>
</div>
<!--Row with two columns divided in 1:2 ratio-->
<div class="row">
<div class="col-sm-4"><!--Column left--></div>
<div class="col-sm-8"><!--Column right--></div>
</div>
<!--Row with two columns divided in 1:3 ratio-->
<div class="row">
<div class="col-sm-3"><!--Column left--></div>
<div class="col-sm-9"><!--Column right--></div>
</div>
</div>
Tip:Open the output of all grid examples inside the CodeLab editor in a new blank tab (click
the link next to "Show Output" button) and resize the browser window to understand how
the Bootstrap responsive grid system works.
Since Bootstrap grid system is based on 12 columns, so to keep the columns in a one
line (i.e. side by side), the sum of the grid column numbers in each row should be equal
to 12. If you see the above example carefully you will find the numbers of grid columns
(i.e. col-sm-*) add up to twelve (6+6, 4+8 and 3+9) for every row.
Example
Try this code »
<div class="container">
<!--Row with three equal columns-->
7
<div class="row">
<div class="col-md-4"><!--Column left--></div>
<div class="col-md-4"><!--Column middle--></div>
<div class="col-md-4"><!--Column right--></div>
</div>
<!--Row with three columns divided in 1:4:1 ratio-->
<div class="row">
<div class="col-md-2"><!--Column left--></div>
<div class="col-md-8"><!--Column middle--></div>
<div class="col-md-2"><!--Column right--></div>
</div>
<!--Row with three columns divided unevenly-->
<div class="row">
<div class="col-md-3"><!--Column left--></div>
<div class="col-md-7"><!--Column middle--></div>
<div class="col-md-2"><!--Column right--></div>
</div>
</div>
Note:If more than 12 grid columns are placed within a single row, then each group of extra
columns, as a whole, will wrap onto a new line.
Bootstrap Layouts with Column Wrapping Feature
Now we are going to create more flexible layouts that changes the column orientation
based on the viewport size. The following example will create a three column layout on
medium devices like laptops and desktops, as well as on tablets (e.g. Apple iPad) in
landscape mode, but on small devices like tablets in portrait mode, it will change into a
two column layout where the third column moves at the bottom of the first two
columns.
Example
Try this code »
1. <div class="container">
2. <div class="row">
3. <div class="col-sm-3 col-md-2"><!--Column one--></div>
4. <div class="col-sm-9 col-md-8"><!--Column two--></div>
5. <div class="col-sm-12 col-md-2"><!--Column three--></div>
6. </div>
7. </div>
As you can see in the example above the sum of small grid column numbers (i.e. col-sm-
*) is 3 + 9 + 12 = 24 > 12, so the third <div> element with the class .col-sm-12 that is
adding the extra columns beyond the maximum 12 columns in a .row, gets wrapped
onto a new line as one contiguous unit on small devices having the viewport width less
than the 992 pixels.
8
Similarly, you can create even more adaptable layouts for your websites and
applications using the Bootstrap's grid column wrapping feature. In the next section,
we'll discuss the other aspect of this feature. Here're some ready to use Bootstrap grid
examples.
In the above illustration there are total 12 content boxes in all devices, but its placement
varies according to the device screen size, like in mobile device the layout is rendered as
one column grid layout which has 1 column and 12 rows placed above one another,
whereas in tablet it is rendered as two column grid layout which has 2 columns and 6
rows. Further, in medium screen size devices like laptops and desktops it is rendered as
three column grid layout which as 3 columns and 4 rows and finally in large screen
devices like large desktops it is rendered as four column grid layout which has 4
columns and 3 rows.
Now the question is how we can create such responsive layouts using this Bootstrap
new mobile first grid system. Let's start with the medium device that can be a laptop or
normal desktop. Since our medium device layout has 3 columns and 4 rows i.e. 3x4 grid
layout, so the HTML code for making such grid structure would be something like this.
Example
Try this code »
<div class="container">
<div class="row">
<div class="col-md-4"><p>Box 1</p></div>
<div class="col-md-4"><p>Box 2</p></div>
<div class="col-md-4"><p>Box 3</p></div>
<div class="col-md-4"><p>Box 4</p></div>
<div class="col-md-4"><p>Box 5</p></div>
<div class="col-md-4"><p>Box 6</p></div>
9
<div class="col-md-4"><p>Box 7</p></div>
<div class="col-md-4"><p>Box 8</p></div>
<div class="col-md-4"><p>Box 9</p></div>
<div class="col-md-4"><p>Box 10</p></div>
<div class="col-md-4"><p>Box 11</p></div>
<div class="col-md-4"><p>Box 12</p></div>
</div>
</div>
If you see the output of the above example in a laptop or desktop having screen or
viewport width greater than or equal to 992px and less than 1200px you will find it has 4
rows where each row has 3 equal columns resulting in 3x4 grid layout.
But just wait, the above example has a major alignment issue. If height of any column is
taller than the other it doesn't clear properly and break the layout. To fix this, use the
combination of a .clearfix class and the responsive utility classes.
Example
Try this code »
<div class="container">
<div class="row">
<div class="col-md-4"><p>Box 1</p></div>
<div class="col-md-4"><p>Box 2</p></div>
<div class="col-md-4"><p>Box 3</p></div>
<div class="clearfix visible-md-block"></div>
<div class="col-md-4"><p>Box 4</p></div>
<div class="col-md-4"><p>Box 5</p></div>
<div class="col-md-4"><p>Box 6</p></div>
<div class="clearfix visible-md-block"></div>
<div class="col-md-4"><p>Box 7</p></div>
<div class="col-md-4"><p>Box 8</p></div>
<div class="col-md-4"><p>Box 9</p></div>
<div class="clearfix visible-md-block"></div>
<div class="col-md-4"><p>Box 10</p></div>
<div class="col-md-4"><p>Box 11</p></div>
<div class="col-md-4"><p>Box 12</p></div>
</div>
</div>
Since the default grid system has 12 columns and in our layout sum of the every three
column number, i.e. col-md-* is equal to 12 that's why we cleared columns after every
third occurrence. In any other scenario where columns numbers are different for every
column you should use .clearfix after the column that makes the complete 12 column
grid.
Note:The responsive utility class .visible-md-block makes the .clearfix class effective only
on medium size devices and it is hidden on other devices.
10
Now it's time to customize our layout for other devices. First customize it for tablet.
Since inside the tablet our layout rendered as 2x6 grids (i.e. 2 columns and 6 rows). So,
go ahead and add the class .col-sm-6 on every column.
Example
Try this code »
<div class="container">
<div class="row">
<div class="col-sm-6 col-md-4"><p>Box 1</p></div>
<div class="col-sm-6 col-md-4"><p>Box 2</p></div>
<div class="col-sm-6 col-md-4"><p>Box 3</p></div>
<div class="clearfix visible-md-block"></div>
<div class="col-sm-6 col-md-4"><p>Box 4</p></div>
<div class="col-sm-6 col-md-4"><p>Box 5</p></div>
<div class="col-sm-6 col-md-4"><p>Box 6</p></div>
<div class="clearfix visible-md-block"></div>
<div class="col-sm-6 col-md-4"><p>Box 7</p></div>
<div class="col-sm-6 col-md-4"><p>Box 8</p></div>
<div class="col-sm-6 col-md-4"><p>Box 9</p></div>
<div class="clearfix visible-md-block"></div>
<div class="col-sm-6 col-md-4"><p>Box 10</p></div>
<div class="col-sm-6 col-md-4"><p>Box 11</p></div>
<div class="col-sm-6 col-md-4"><p>Box 12</p></div>
</div>
</div>
Now, since the sum of every two column number, i.e. col-sm-* is equal to 12, so clear
floats after every second occurrence of columns.
After clearing floats for small devices our final code would be:
Example
Try this code »
<div class="container">
<div class="row">
<div class="col-sm-6 col-md-4"><p>Box 1</p></div>
<div class="col-sm-6 col-md-4"><p>Box 2</p></div>
<div class="clearfix visible-sm-block"></div>
<div class="col-sm-6 col-md-4"><p>Box 3</p></div>
<div class="clearfix visible-md-block"></div>
<div class="col-sm-6 col-md-4"><p>Box 4</p></div>
<div class="clearfix visible-sm-block"></div>
<div class="col-sm-6 col-md-4"><p>Box 5</p></div>
<div class="col-sm-6 col-md-4"><p>Box 6</p></div>
<div class="clearfix visible-sm-block"></div>
<div class="clearfix visible-md-block"></div>
<div class="col-sm-6 col-md-4"><p>Box 7</p></div>
<div class="col-sm-6 col-md-4"><p>Box 8</p></div>
<div class="clearfix visible-sm-block"></div>
<div class="col-sm-6 col-md-4"><p>Box 9</p></div>
11
<div class="clearfix visible-md-block"></div>
<div class="col-sm-6 col-md-4"><p>Box 10</p></div>
<div class="clearfix visible-sm-block"></div>
<div class="col-sm-6 col-md-4"><p>Box 11</p></div>
<div class="col-sm-6 col-md-4"><p>Box 12</p></div>
</div>
</div>
Tip:For convenience choose your primary target device and create layout for that device first
after that add classes to make it responsive for other devices.
Similarly, you can customize the layout for larger devices like a large desktop screen.
Here's the final code after combining the whole process.
Example
Try this code »
<div class="container">
<div class="row">
<div class="col-sm-6 col-md-4 col-lg-3"><p>Box 1</p></div>
<div class="col-sm-6 col-md-4 col-lg-3"><p>Box 2</p></div>
<div class="clearfix visible-sm-block"></div>
<div class="col-sm-6 col-md-4 col-lg-3"><p>Box 3</p></div>
<div class="clearfix visible-md-block"></div>
<div class="col-sm-6 col-md-4 col-lg-3"><p>Box 4</p></div>
<div class="clearfix visible-sm-block"></div>
<div class="clearfix visible-lg-block"></div>
<div class="col-sm-6 col-md-4 col-lg-3"><p>Box 5</p></div>
<div class="col-sm-6 col-md-4 col-lg-3"><p>Box 6</p></div>
<div class="clearfix visible-sm-block"></div>
<div class="clearfix visible-md-block"></div>
<div class="col-sm-6 col-md-4 col-lg-3"><p>Box 7</p></div>
<div class="col-sm-6 col-md-4 col-lg-3"><p>Box 8</p></div>
<div class="clearfix visible-sm-block"></div>
<div class="clearfix visible-lg-block"></div>
<div class="col-sm-6 col-md-4 col-lg-3"><p>Box 9</p></div>
<div class="clearfix visible-md-block"></div>
<div class="col-sm-6 col-md-4 col-lg-3"><p>Box 10</p></div>
<div class="clearfix visible-sm-block"></div>
<div class="col-sm-6 col-md-4 col-lg-3"><p>Box 11</p></div>
<div class="col-sm-6 col-md-4 col-lg-3"><p>Box 12</p></div>
</div>
</div>
Tip:According to the above illustration there is no need to customize the layout for extra small
devices like mobile phones; since columns on extra small devices are always horizontal and
rendered as one column grid layout by default.
12
Offsetting the Grid Columns
You can also move grid columns to the right for alignment purpose using the column
offset classes like .col-md-offset-*, .col-sm-offset-*, etc.
These classes offset the columns by simply increasing its left margin by specified
number of columns. For example, the class .col-sm-offset-4 on the column .col-sm-
8 moves it to the right over four columns from its original position.
Example
Try this code »
<div class="container">
<div class="row">
<div class="col-sm-4"></div>
<div class="col-sm-8"></div>
</div>
<div class="row">
<div class="col-sm-8 col-sm-offset-4"><!--Column with 4 columns offset--></div>
</div>
</div>
Example
Try this code »
<div class="container">
<div class="row">
<div class="col-xs-8"><!--Column left--></div>
<div class="col-xs-4">
<!--Column right with nested rows and columns-->
<div class="row">
<div class="col-xs-12"></div>
</div>
<div class="row">
<div class="col-xs-12"></div>
</div>
</div>
</div>
</div>
13
Bootstrap Responsive Utilities Classes
You can use the following responsive classes to enable the visibility of elements on
certain devices that screen sizes falls with the specific range.
As of v3.2.0, the .visible-*-* classes for each breakpoint come in three variations, one
for each CSS display property value: inline, block and inline-block.
Class Description
.visible- Makes the element visible only on extra small devices having screen width less than
xs-* 768px. Hidden on others.
.visible- Makes the element visible only on small devices having screen width greater than or equal
sm-* to 768px (i.e. ≥768px) but less than 992px. Hidden on others.
.visible- Makes the element visible only on medium devices having screen width greater than or
md-* equal to 992px (i.e. ≥992px) but less than 1200px. Hidden on others.
.visible- Makes the element visible only on large devices having screen width greater than or equal
lg-* to 1200px (i.e. ≥1200px). Hidden on others.
Tip:You can also mix these classes to make the elements visible on multiple devices. For
example, you can apply the class .visible-xs-* and .visible-md-* on any element to make it
visible on extra small and medium devices.
Example
Try this code »
1. <p class="visible-xs-block">This paragraph is visible only on extra small devices.</p>
2. <p class="visible-sm-block">This paragraph is visible only on small devices.</p>
3. <p class="visible-md-block">This paragraph is visible only on medium devices.</p>
4. <p class="visible-lg-block">This paragraph is visible only on large devices.</p>
Similarly you can use these hidden utility classes to hide the elements on certain
devices.
Class Description
.hidden- Hide the elements only on extra small devices having screen width less than 768px. Visible
xs on others.
.hidden- Hide the elements only on small devices having screen width greater than or equal to 768px
sm (i.e. ≥768px) but less than 992px. Visible on others.
.hidden- Hide the elements only on medium devices having screen width greater than or equal to
md 992px (i.e. ≥992px) but less than 1200px. Visible on others.
.hidden- Hide the elements only on large devices having screen width greater than or equal to 1200px
lg (i.e. ≥1200px). Visible on others.
14
Tip:You can also mix these classes to make the elements hidden on multiple devices. For
example you can apply the class .hidden-xs and .hidden-md on any element to make it hidden
on extra small and medium devices.
Example
Try this code »
1. <p class="hidden-xs">This paragraph is hidden only on extra small devices.</p>
2. <p class="hidden-sm">This paragraph is hidden only on small devices.</p>
3. <p class="hidden-md">This paragraph is hidden only on medium devices.</p>
4. <p class="hidden-lg">This paragraph is hidden only on large devices.</p>
Similar to the regular responsive classes, you can use the following utility classes to
show or hide certain elements for printing purpose or devices.
Class Description
.visible-print-block Hide block elements for browser rendering while visible for print.
.visible-print-inline Hide inline elements for browser rendering while visible for print.
.visible-print-inline-block Hide inline-block elements for browser rendering while visible for print.
.hidden-print Hide elements for printing while visible on browser.
Example
15
Try this code »
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 3 Fixed Layout Example</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<nav id="myNavbar" class="navbar navbar-default navbar-inverse navbar-fixed-top" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-
target="#navbarCollapse">
<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="#">Tutorial Republic</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="nav navbar-nav">
<li class="active"><a href="https://www.tutorialrepublic.com"
target="_blank">Home</a></li>
<li><a href="https://www.tutorialrepublic.com/about-us.php"
target="_blank">About</a></li>
<li><a href="https://www.tutorialrepublic.com/contact-us.php"
target="_blank">Contact</a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>Learn to Create Websites</h1>
<p>In today's world internet is the most popular way of connecting
with the people. At <a href="https://www.tutorialrepublic.com" target="_blank">tutorialrepublic.com</a>
you will learn the essential of web development technologies along with real life
practice example, so that you can create your own website to connect with the people
around the world.</p>
<p><a href="https://www.tutorialrepublic.com" target="_blank" class="btn btn-success btn-
lg">Get started today</a></p>
</div>
16
<div class="row">
<div class="col-xs-4">
<h2>HTML</h2>
<p>HTML is a markup language that is used for creating web pages.
The HTML tutorial section will help you understand the basics of HTML, so that you can
create your own web pages or website.</p>
<p><a href="https://www.tutorialrepublic.com/html-tutorial/" target="_blank"
class="btn btn-success">Learn More »</a></p>
</div>
<div class="col-xs-4">
<h2>CSS</h2>
<p>CSS is used for describing the presentation of web pages. The
CSS tutorial section will help you learn the essentials of CSS, so that you can fine
control the style and layout of your HTML document.</p>
<p><a href="https://www.tutorialrepublic.com/css-tutorial/" target="_blank" class="btn
btn-success">Learn More »</a></p>
</div>
<div class="col-xs-4">
<h2>Bootstrap</h2>
<p>Bootstrap is a powerful front-end framework for faster and
easier web development. The Bootstrap tutorial section will help you learn the techniques
of Bootstrap so that you can quickly create your own website.</p>
<p><a href="https://www.tutorialrepublic.com/twitter-bootstrap-tutorial/"
target="_blank" class="btn btn-success">Learn More »</a></p>
</div>
</div>
<hr>
<div class="row">
<div class="col-xs-12">
<footer>
<p>© Copyright 2013 Tutorial Republic</p>
</footer>
</div>
</div>
</div>
</body>
</html>
— The output of the above example will look something like this:
17
Creating Fluid Layout with Bootstrap
In Bootstrap (version 3.2+), you can use the class .container-fluid to create the fluid
layouts in order to utilize the 100% width of the viewport.
The class .container-fluid simply applies the horizontal margin with the value auto and
left and right padding of 15px on element to offset the left and right margin of -15px
(i.e. margin: 0 -15px;) used on the .row class.
The following code creates a fluid layout that covers 100% width of the screen.
Example
Try this code »
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 3 Fluid Layout Example</title>
18
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<nav id="myNavbar" class="navbar navbar-default navbar-inverse navbar-fixed-top" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-
target="#navbarCollapse">
<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="#">Tutorial Republic</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="nav navbar-nav">
<li class="active"><a href="https://www.tutorialrepublic.com"
target="_blank">Home</a></li>
<li><a href="https://www.tutorialrepublic.com/about-us.php"
target="_blank">About</a></li>
<li><a href="https://www.tutorialrepublic.com/contact-us.php"
target="_blank">Contact</a></li>
</ul>
</div>
</div>
</nav>
<div class="jumbotron">
<div class="container-fluid">
<h1>Learn to Create Websites</h1>
<p>In today's world internet is the most popular way of connecting
with the people. At <a href="https://www.tutorialrepublic.com" target="_blank">tutorialrepublic.com</a>
you will learn the essential of web development technologies along with real life
practice example, so that you can create your own website to connect with the people
around the world.</p>
<p><a href="https://www.tutorialrepublic.com" target="_blank" class="btn btn-success btn-
lg">Get started today</a></p>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-xs-4">
<h2>HTML</h2>
<p>HTML is a markup language that is used for creating web pages.
The HTML tutorial section will help you understand the basics of HTML, so that you can
create your own web pages or website.</p>
19
<p><a href="https://www.tutorialrepublic.com/html-tutorial/" target="_blank"
class="btn btn-success">Learn More »</a></p>
</div>
<div class="col-xs-4">
<h2>CSS</h2>
<p>CSS is used for describing the presentation of web pages. The
CSS tutorial section will help you learn the essentials of CSS, so that you can fine
control the style and layout of your HTML document.</p>
<p><a href="https://www.tutorialrepublic.com/css-tutorial/" target="_blank" class="btn
btn-success">Learn More »</a></p>
</div>
<div class="col-xs-4">
<h2>Bootstrap</h2>
<p>Bootstrap is a powerful front-end framework for faster and
easier web development. The Bootstrap tutorial section will help you learn the techniques
of Bootstrap so that you can quickly create your own website.</p>
<p><a href="https://www.tutorialrepublic.com/twitter-bootstrap-tutorial/"
target="_blank" class="btn btn-success">Learn More »</a></p>
</div>
</div>
<hr>
<div class="row">
<div class="col-xs-12">
<footer>
<p>© Copyright 2013 Tutorial Republic</p>
</footer>
</div>
</div>
</div>
</body>
</html>
— The output of the above example will look something like this:
20
What is Responsive Web Design or Layout
Responsive web design is a process of designing and building websites to provide
better accessibility and optimal viewing experience to the user.
With the growing trend of smart phones and tablets, it has become almost unavoidable
to ignore the optimization of sites for mobile devices. Responsive web design is a
preferable alternative and an efficient way to target a wide range of devices with much
less efforts.
Responsive layouts automatically adjust and adapts to any device screen size, whether it
is a desktop, a laptop, a tablet, or a mobile phone.
21
Creating Responsive Layout with Bootstrap
With the new Bootstrap 3 mobile first grid system creating the responsive and mobile
friendly websites has become much easier. As opposed to previous version you don't
need to include any additional style sheet to enable responsive feature. Bootstrap 3 is
responsive and mobile friendly from the start. Its four tiers grids classes provides better
control over the layout as well as how it will be rendered on different types of devices
like cell phones, tablets, desktop and laptops, large screen devices etc.
Example
Try this code »
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bootstrap 3 Responsive Layout Example</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<nav id="myNavbar" class="navbar navbar-default navbar-inverse navbar-fixed-top" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-
target="#navbarCollapse">
<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="#">Tutorial Republic</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
22
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="nav navbar-nav">
<li class="active"><a href="https://www.tutorialrepublic.com"
target="_blank">Home</a></li>
<li><a href="https://www.tutorialrepublic.com/about-us.php"
target="_blank">About</a></li>
<li><a href="https://www.tutorialrepublic.com/contact-us.php"
target="_blank">Contact</a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>Learn to Create Websites</h1>
<p>In today's world internet is the most popular way of connecting
with the people. At <a href="https://www.tutorialrepublic.com" target="_blank">tutorialrepublic.com</a>
you will learn the essential of web development technologies along with real life
practice example, so that you can create your own website to connect with the people
around the world.</p>
<p><a href="https://www.tutorialrepublic.com" target="_blank" class="btn btn-success btn-
lg">Get started today</a></p>
</div>
<div class="row">
<div class="col-sm-6 col-md-4 col-lg-2">
<h2>HTML</h2>
<p>HTML is a markup language that is used for creating web pages.
The HTML tutorial section will help you understand the basics of HTML, so that you can
create your own web pages or website.</p>
<p><a href="https://www.tutorialrepublic.com/html-tutorial/" target="_blank"
class="btn btn-success">Learn More »</a></p>
</div>
<div class="col-sm-6 col-md-4 col-lg-2">
<h2>CSS</h2>
<p>CSS is used for describing the presentation of web pages. The
CSS tutorial section will help you learn the essentials of CSS, so that you can fine
control the style and layout of your HTML document.</p>
<p><a href="https://www.tutorialrepublic.com/css-tutorial/" target="_blank" class="btn
btn-success">Learn More »</a></p>
</div>
<div class="clearfix visible-sm-block"></div>
<div class="col-sm-6 col-md-4 col-lg-2">
<h2>Bootstrap</h2>
<p>Bootstrap is a powerful front-end framework for faster and
easier web development. The Bootstrap tutorial section will help you learn the techniques
of Bootstrap so that you can create web your own website with much less efforts.</p>
<p><a href="https://www.tutorialrepublic.com/twitter-bootstrap-tutorial/"
target="_blank" class="btn btn-success">Learn More »</a></p>
</div>
<div class="clearfix visible-md-block"></div>
23
<div class="col-sm-6 col-md-4 col-lg-2">
<h2>References</h2>
<p>The references section outlines all the standard HTML tags and
CSS properties along with other useful references such as color names and values, symbols
and character entities, web safe fonts, language codes, HTTP messages and much more.</p>
<p><a href="https://www.tutorialrepublic.com/twitter-bootstrap-tutorial/"
target="_blank" class="btn btn-success">Learn More »</a></p>
</div>
<div class="clearfix visible-sm-block"></div>
<div class="col-sm-6 col-md-4 col-lg-2">
<h2>Examples</h2>
<p>The examples section encloses an extensive collection of
examples on various topic that you can try and test yourself using online HTML editor.</p>
<p><a href="https://www.tutorialrepublic.com/twitter-bootstrap-tutorial/"
target="_blank" class="btn btn-success">Learn More »</a></p>
</div>
<div class="col-sm-6 col-md-4 col-lg-2">
<h2>FAQ</h2>
<p>The collection of Frequently Asked Questions (FAQ) provides
brief answers to many common questions related to web design and development.</p>
<p><a href="https://www.tutorialrepublic.com/twitter-bootstrap-tutorial/"
target="_blank" class="btn btn-success">Learn More »</a></p>
</div>
</div>
<hr>
<div class="row">
<div class="col-sm-12">
<footer>
<p>© Copyright 2013 Tutorial Republic</p>
</footer>
</div>
</div>
</div>
</body>
</html>
Tip:Open the output of this example in new window and resize the screen you will see the
orientation of content boxes changes when viewport width crossing or approaching the certain
limit (i.e. breakpoints).
Example
Try this code »
1. <h1>h1. Bootstrap heading</h1>
24
2. <h2>h2. Bootstrap heading</h2>
3. <h3>h3. Bootstrap heading</h3>
4. <h4>h4. Bootstrap heading</h4>
5. <h5>h5. Bootstrap heading</h5>
6. <h6>h6. Bootstrap heading</h6>
— The output of the above example will look something like this:
Example
Try this code »
1. <h1>h1. Bootstrap heading <small>Secondary text</small></h1>
2. <h2>h2. Bootstrap heading <small>Secondary text</small></h2>
3. <h3>h3. Bootstrap heading <small>Secondary text</small></h3>
4. <h4>h4. Bootstrap heading <small>Secondary text</small></h4>
5. <h5>h5. Bootstrap heading <small>Secondary text</small></h5>
6. <h6>h6. Bootstrap heading <small>Secondary text</small></h6>
— The output of the above example will look something like this:
25
Creating Page Headers
You can make your <h1> heading appear differently than rest of the headings on a page
using the page header component. You can also utilize <small> tag to mark header
subtext.
Example
Try this code »
1. <div class="page-header">
2. <h1>Bootstrap <small>An intuitive front-end framework</small></h1>
3. </div>
— The output of the above example will look something like this:
Example
Try this code »
1. <p>This is how a normal paragraph looks like in Bootstrap.</p>
2. <p class="lead">This is how a paragraph stands out in Bootstrap.</p>
— The HTML code in the above examples will produce the following result:
You can easily align text inside a paragraph and other elements using text alignment
classes.
Example
Try this code »
1. <p class="text-left">Left aligned text.</p>
2. <p class="text-center">Center aligned text.</p>
3. <p class="text-right">Right aligned text.</p>
4. <p class="text-justify">Justified text.</p>
5. <p class="text-nowrap">No wrap text.</p>
26
Bootstrap Text Formatting
You are free to use text formatting tags like <strong>, <i>, <small> to make your text bold,
italic, small and so on, in the same way you do in simple HTML.
Example
Try this code »
<p><b>This is bold text</b></p>
<p><big>This is big text</big></p>
<p><code>This is computer code</code></p>
<p><em>This is emphasized text</em></p>
<p><i>This is italic text</i></p>
<p><mark>This is highlighted text</mark></p>
<p><small>This is small text</small></p>
<p><strong>This is strongly emphasized text</strong></p>
<p>This is <sub>subscript</sub> and <sup>superscript</sup></p>
<p><ins>This text is inserted to the document</ins></p>
<p><del>This text is deleted from the document</del></p>
— The output of the above example will look something like this:
27
Example
Try this code »
1. <p class="text-lowercase">The quick brown fox jumps over the lazy dog.</p>
2. <p class="text-uppercase">The quick brown fox jumps over the lazy dog.</p>
3. <p class="text-capitalize">The quick brown fox jumps over the lazy dog.</p>
— The output of the above example will look something like this:
Example
Try this code »
1. <p class="text-muted">Muted: This text is grayed out.</p>
2. <p class="text-primary">Important: Please read the instructions carefully before
proceeding.</p>
3. <p class="text-success">Success: Your message has been sent successfully.</p>
4. <p class="text-info">Note: You must agree with the terms and conditions to complete the
sign up process.</p>
5. <p class="text-warning">Warning: There was a problem with your network connection.</p>
6. <p class="text-danger">Error: An error has been occurred while submitting your
data.</p>
— The output of the above example will look something like this:
28
Styling Blockquotes
You can also give pretty look to your blockquotes — Just define the blockquotes using
the standard <blockquote> element and bootstrap's style sheet will do the rest.
Example
Try this code »
1. <blockquote>
2. <p>The world is a dangerous place to live; not because of the people who are
evil, but because of the people who don't do anything about it.</p>
3. <small>by <cite>Albert Einstein</cite></small>
4. </blockquote>
— The output of the above example will look something like this:
Alternatively, you can right-align the blockquote through floating it to right by simply
applying the class .pull-right on the <blockquote> element.
Example
Try this code »
1. <blockquote class="pull-right">
2. <p>The world is a dangerous place to live; not because of the people who are
evil, but because of the people who don't do anything about it.</p>
3. <small>by <cite>Albert Einstein</cite></small>
4. </blockquote>
— The output of the above example will look something like this:
What is Table
The HTML tables are used to present data in grid manner like row and columns. Using
Bootstrap you can greatly improve the appearance of table in a simple way.
See the tutorial on HTML Tables to learn more about tables.
29
Example
Try this code »
<table class="table">
<thead>
<tr>
<th>Row</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John</td>
<td>Carter</td>
<td>[email protected]</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Parker</td>
<td>[email protected]</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
<td>Rambo</td>
<td>[email protected]</td>
</tr>
</tbody>
</table>
— The output of the above example will look something like this:
Example
Try this code »
<table class="table table-striped">
<thead>
<tr>
<th>Row</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John</td>
<td>Carter</td>
<td>[email protected]</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Parker</td>
<td>[email protected]</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
<td>Rambo</td>
<td>[email protected]</td>
</tr>
</tbody>
</table>
— The output of the above example will look something like this:
31
Table with Borders on All Sides
You can also add borders to the all table cells by adding an extra Bootstrap's
class .table-bordered to the .table base class.
Example
Try this code »
<table class="table table-bordered">
<thead>
<tr>
<th>Row</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John</td>
<td>Carter</td>
<td>[email protected]</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Parker</td>
<td>[email protected]</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
<td>Rambo</td>
<td>[email protected]</td>
</tr>
</tbody>
</table>
— The output of the above example will look something like this:
32
Enable Hover State on Table Rows
You can also enable a hover state on table rows within a <tbody> element by adding the
Bootstrap's class .table-hover to the .table base class.
Example
Try this code »
<table class="table table-hover">
<thead>
<tr>
<th>Row</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John</td>
<td>Carter</td>
<td>[email protected]</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Parker</td>
<td>[email protected]</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
<td>Rambo</td>
<td>[email protected]</td>
</tr>
</tbody>
33
</table>
— The output of the above example will look something like this:
Example
Try this code »
<table class="table table-condensed">
<thead>
<tr>
<th>Row</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John</td>
<td>Carter</td>
<td>[email protected]</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Parker</td>
<td>[email protected]</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
34
<td>Rambo</td>
<td>[email protected]</td>
</tr>
</tbody>
</table>
— The output of the above example will look something like this:
Example
Try this code »
<table class="table">
<thead>
<tr>
<th>Row</th>
<th>Bill</th>
<th>Payment Date</th>
<th>Payment Status</th>
</tr>
</thead>
<tbody>
<tr class="active">
<td>1</td>
<td>Credit Card</td>
<td>04/07/2014</td>
<td>Call in to confirm</td>
</tr>
<tr class="success">
<td>2</td>
<td>Water</td>
<td>01/07/2014</td>
<td>Paid</td>
</tr>
<tr class="info">
<td>3</td>
35
<td>Internet</td>
<td>05/07/2014</td>
<td>Change plan</td>
</tr>
<tr class="warning">
<td>4</td>
<td>Electricity</td>
<td>03/07/2014</td>
<td>Pending</td>
</tr>
<tr class="danger">
<td>5</td>
<td>Telephone</td>
<td>06/07/2014</td>
<td>Due</td>
</tr>
</tbody>
</table>
— The output of the above example will look something like this:
Check out the snippets section for examples of some beautifully designed Bootstrap tables.
Example
Try this code »
36
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Row</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Biography</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John</td>
<td>Carter</td>
<td>[email protected]</td>
<td>Lorem ipsum dolor sit amet…</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Parker</td>
<td>[email protected]</td>
<td>Vestibulum consectetur scelerisque…</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
<td>Rambo</td>
<td>[email protected]</td>
<td>Integer pulvinar leo id risus…</td>
</tr>
</tbody>
</table>
</div>
37
Tag Description
<thead> Container element for table header rows (<tr>) that defines headings for table columns.
<tbody> Container element for table rows (<tr>) that defines the body of a table.
<tr> Container element for a set of table cells (<td> or <th>) that appears in a single row.
<th> Special table cell for column headings.
<td> Default table cell for placing data.
Example
Try this code »
<ul class="list-unstyled">
<li>Home</li>
<li>Products
<ul>
<li>Gadgets</li>
<li>Accessories</li>
</ul>
</li>
<li>About Us</li>
<li>Contact</li>
</ul>
— The output of the above example will look something like this:
38
Note:The .list-unstyled class removes the default list-style and left padding only from the list
items which are immediate children of the <ul> or <ol> element.
Example
Try this code »
1. <ul class="list-inline">
2. <li>Home</li>
3. <li>Products</li>
4. <li>About Us</li>
5. <li>Contact</li>
6. </ul>
— The output of the above example will look something like this:
Example
Try this code »
39
<dl class="dl-horizontal">
<dt>User Agent</dt>
<dd>An HTML user agent is any device that interprets HTML documents.</dd>
<dt>Client-side Scripting</dt>
<dd>Client-side scripting generally refers to the category of computer
programs on the web that are executed by the user's web browser.</dd>
<dt>Document Tree</dt>
<dd>The tree of elements encoded in the source document.</dd>
</dl>
— The output of the above example will look something like this:
Note:The terms clipped in horizontal definition lists will be indicated by an ellipsis (…) using
the text-overflow CSS property.
Example
Try this code »
1. <ul class="list-group">
2. <li class="list-group-item">Pictures</li>
3. <li class="list-group-item">Documents</li>
4. <li class="list-group-item">Music</li>
5. <li class="list-group-item">Videos</li>
6. </ul>
— The output of the above example will look something like this:
40
List Group with Linked Items
You can also hyperlink list group items with the little change in HTML markup.
Just replace the <li> with <a> tag and use <div> element as a parent instead of <ul>. You
can also add icons and badges to this list group to make it more elegant. The badges
component will automatically be positioned on the right.
Example
Try this code »
<div class="list-group">
<a href="#" class="list-group-item active">
<span class="glyphicon glyphicon-camera"></span> Pictures <span class="badge">25</span>
</a>
<a href="#" class="list-group-item">
<span class="glyphicon glyphicon-file"></span> Documents <span class="badge">145</span>
</a>
<a href="#" class="list-group-item">
<span class="glyphicon glyphicon-music"></span> Music <span class="badge">50</span>
</a>
<a href="#" class="list-group-item">
<span class="glyphicon glyphicon-film"></span> Videos <span class="badge">8</span>
</a>
</div>
— The output of the above example will look something like this:
Tip:You can use the Bootstrap list group component for creating the sidebar navigation menu,
e.g. displaying the list of products or categories on your website.
You can also add other HTML elements like headings and paragraph within these list
groups.
Example
Try this code »
<div class="list-group">
<a href="#" class="list-group-item">
<h4 class="list-group-item-heading">What is HTML?</h4>
<p class="list-group-item-text">HTML stands for HyperText Markup Language. HTML is
the standard markup language for describing the structure of web pages.</p>
</a>
41
<a href="#" class="list-group-item active">
<h4 class="list-group-item-heading">What is Bootstrap?</h4>
<p class="list-group-item-text">Bootstrap is a sleek, intuitive, and powerful
front-end framework for faster and easier web development. It is a collection of CSS and
HTML conventions.</p>
</a>
<a href="#" class="list-group-item">
<h4 class="list-group-item-heading">What is CSS?</h4>
<p class="list-group-item-text">CSS stands for Cascading Style Sheet. CSS allows
you to specify various style properties for a given HTML element such as colors,
backgrounds, fonts etc.</p>
</a>
</div>
— The output of the above example will look something like this:
Example
Try this code »
1. <ul class="list-group">
2. <li class="list-group-item list-group-item-success">200 OK: The server successfully
processed the request.</li>
3. <li class="list-group-item list-group-item-info">100 Continue: The client should continue
with its request.</li>
4. <li class="list-group-item list-group-item-warning">503 Service Unavailable: The server is
temporarily unable to handle the request.</li>
5. <li class="list-group-item list-group-item-danger">400 Bad Request: The request cannot be
fulfilled due to bad syntax.</li>
42
6. </ul>
— The output of the above example will look something like this:
Similarly, you can use these contextual classes to the linked list group items. You can
also use the class .active to specify the active list group item.
Example
Try this code »
1. <div class="list-group">
2. <a href="#" class="list-group-item list-group-item-success">200 OK: The server successfully
processed the request.</a>
3. <a href="#" class="list-group-item list-group-item-info active">100 Continue: The client
should continue with its request.</a>
4. <a href="#" class="list-group-item list-group-item-warning">503 Service Unavailable: The
server is temporarily unable to handle the request.</a>
5. <a href="#" class="list-group-item list-group-item-danger">400 Bad Request: The request
cannot be fulfilled due to bad syntax.</a>
6. </div>
43
Example
Try this code »
<form>
<div class="form-group">
<label for="inputEmail">Email</label>
<input type="email" class="form-control" id="inputEmail" placeholder="Email">
</div>
<div class="form-group">
<label for="inputPassword">Password</label>
<input type="password" class="form-control" id="inputPassword" placeholder="Password">
</div>
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
— The output of the above example will look something like this:
Note:In Bootstrap 3 all textual elements like <input>, <textarea>, and <select> with the class .form-
control are 100% wide by default. To use them inline, you'll have to set a width on the element
the form controls used within.
44
Use Bootstrap's predefined grid classes to align labels and form controls.
Add the class .control-label to the <label> element.
Example
Try this code »
<form class="form-horizontal">
<div class="form-group">
<label for="inputEmail" class="control-label col-xs-2">Email</label>
<div class="col-xs-10">
<input type="email" class="form-control" id="inputEmail" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="control-label col-xs-2">Password</label>
<div class="col-xs-10">
<input type="password" class="form-control" id="inputPassword" placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-2 col-xs-10">
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-2 col-xs-10">
<button type="submit" class="btn btn-primary">Login</button>
</div>
</div>
</form>
— The output of the above example will look something like this:
Example
Try this code »
<form class="form-inline">
<div class="form-group">
<label class="sr-only" for="inputEmail">Email</label>
<input type="email" class="form-control" id="inputEmail" placeholder="Email">
</div>
<div class="form-group">
<label class="sr-only" for="inputPassword">Password</label>
<input type="password" class="form-control" id="inputPassword" placeholder="Password">
</div>
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
— The output of the above example will look something like this:
Note:It is recommended to include a label for every form inputs otherwise screen readers will
have trouble with your forms. However in case of inline form layout you can hide these labels
using the .sr-only class.
Check out the snippets section for examples of some beautifully designed Bootstrap forms.
Example
Try this code »
<form class="form-horizontal">
<div class="form-group">
<label for="inputEmail" class="control-label col-xs-2">Email</label>
<div class="col-xs-10">
<p class="form-control-static">[email protected]</p>
</div>
46
</div>
<div class="form-group">
<label for="inputPassword" class="control-label col-xs-2">Password</label>
<div class="col-xs-10">
<input type="password" class="form-control" id="inputPassword" placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-2 col-xs-10">
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-2 col-xs-10">
<button type="submit" class="btn btn-primary">Login</button>
</div>
</div>
</form>
— The output of the above example will look something like this:
Example
Try this code »
<form>
<!-- Vertically stacked checkboxes -->
<div class="checkbox">
<label><input type="checkbox" name="sports"> Cricket</label>
47
</div>
<div class="checkbox">
<label><input type="checkbox" name="sports"> Football</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="sports"> Tennis</label>
</div>
<!-- Vertically stacked radios -->
<div class="radio">
<label><input type="radio" name="gender"> Male</label>
</div>
<div class="radio">
<label><input type="radio" name="gender"> Female</label>
</div>
</form>
Inline Checkboxes and Radios
However, to place them inline i.e. side-by-side just use the class .checkbox-
inline or .radio-inline on the <label> element, as shown in the following example. No
need to wrap the controls in a <div> element unlike stacked version.
Example
Try this code »
<form>
<!-- Inline checkboxes -->
<div>
<label class="checkbox-inline"><input type="checkbox" name="sports"> Cricket</label>
<label class="checkbox-inline"><input type="checkbox" name="sports"> Football</label>
<label class="checkbox-inline"><input type="checkbox" name="sports"> Tennis</label>
</div>
<!-- Inline radios -->
<label class="radio-inline"><input type="radio" name="gender"> Male</label>
<label class="radio-inline"><input type="radio" name="gender"> Female</label>
</form>
Example
Try this code »
48
<form>
<div class="row">
<div class="col-xs-6">
<input type="text" class="form-control input-lg" placeholder="Large input">
</div>
<div class="col-xs-6">
<select class="form-control input-lg">
<option>Large select</option>
</select>
</div>
</div>
<br>
<div class="row">
<div class="col-xs-6">
<input type="text" class="form-control" placeholder="Default input">
</div>
<div class="col-xs-6">
<select class="form-control">
<option>Default select</option>
</select>
</div>
</div>
<br>
<div class="row">
<div class="col-xs-6">
<input type="text" class="form-control input-sm" placeholder="Small input">
</div>
<div class="col-xs-6">
<select class="form-control input-sm">
<option>Small select</option>
</select>
</div>
</div>
</form>
Example
Try this code »
<form>
<div class="row">
49
<div class="col-xs-3">
<input type="text" class="form-control">
</div>
<div class="col-xs-4">
<input type="text" class="form-control">
</div>
<div class="col-xs-5">
<input type="text" class="form-control">
</div>
</div>
<br>
<div class="row">
<div class="col-xs-3">
<textarea class="form-control"></textarea>
</div>
<div class="col-xs-4">
<textarea class="form-control"></textarea>
</div>
<div class="col-xs-5">
<textarea class="form-control"></textarea>
</div>
</div>
<br>
<div class="row">
<div class="col-xs-3">
<select class="form-control">
<option>Select</option>
</select>
</div>
<div class="col-xs-4">
<select class="form-control">
<option>Select</option>
</select>
</div>
<div class="col-xs-5">
<select class="form-control">
<option>Select</option>
</select>
</div>
</div>
</form>
50
You can add the relative form sizing classes such as .form-group-lg or .form-group-sm to
the .form-group itself to make both the labels and form controls larger or smaller at the
same time within the horizontal form layouts.
Example
Try this code »
<form class="form-horizontal">
<div class="form-group form-group-lg">
<label class="col-sm-3 control-label" for="inputLarge">Large label</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputLarge" placeholder="Large input">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="inputDefault">Default label</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputDefault" placeholder="Default input">
</div>
</div>
<div class="form-group form-group-sm">
<label class="col-sm-3 control-label" for="inputSmall">Small label</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputSmall" placeholder="Small input">
</div>
</div>
</form>
Example
Try this code »
1. <form>
2. <input type="text" class="form-control" placeholder="Disabled input" disabled="disabled">
3. </form>
— The output of the above example will look something like this:
51
browser will not show the "not-allowed" cursor when you put the mouse pointer on label
text.
Example
Try this code »
<form>
<!-- Disabled stacked checkboxes -->
<div class="checkbox disabled">
<label><input type="checkbox" disabled="disabled"> Disabled checkbox</label>
</div>
<div class="checkbox disabled">
<label><input type="checkbox" disabled="disabled"> Another disabled checkbox</label>
</div>
<!-- Disabled stacked radios -->
<div class="radio disabled">
<label><input type="radio" disabled="disabled"> Disabled radio</label>
</div>
<div class="radio disabled">
<label><input type="radio" disabled="disabled"> Another disabled radio</label>
</div>
<!-- Disabled inline checkboxes -->
<p>
<label class="checkbox-inline disabled"><input type="checkbox" disabled="disabled">
Disabled checkbox</label>
<label class="checkbox-inline disabled"><input type="checkbox" disabled="disabled"> Another
disabled checkbox</label>
</p>
<!-- Disabled inline radios -->
<p>
<label class="radio-inline disabled"><input type="radio" disabled="disabled"> Disabled
radio</label>
<label class="radio-inline disabled"><input type="radio" disabled="disabled"> Another
disabled radio</label>
</p>
</form>
Similarly, you can use the readonly attribute to create read only text based input controls
that prevent user inputs and give the style same as disabled.
Example
Try this code »
1. <form>
2. <input type="text" class="form-control" placeholder="Readonly input" readonly="readonly">
3. </form>
52
Creating Disabled Fieldsets
Rather than disabling the form controls individually, you can also disable all form
controls within a fieldset at once by adding the disabled attribute to
the <fieldset> element.
Example
Try this code »
<form class="form-horizontal">
<fieldset disabled="disabled">
<div class="form-group">
<label for="inputEmail" class="control-label col-xs-2">Email</label>
<div class="col-xs-10">
<input type="email" class="form-control" id="inputEmail" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="control-label col-xs-2">Password</label>
<div class="col-xs-10">
<input type="password" class="form-control" id="inputPassword"
placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-2 col-xs-10">
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-2 col-xs-10">
<button type="submit" class="btn btn-primary">Login</button>
</div>
</div>
</fieldset>
</form>
— The output of the above example will look something like this:
53
Placing Help Text around Form Controls
Placing help text for the form controls in an efficient way to guide users to enter the
correct data in a form. You can place block level help text for the form controls using the
class .help-block. The help text is typically displayed at the bottom of the control.
Example
Try this code »
1. <form>
2. <input type="text" class="form-control">
3. <span class="help-block">A block of help text that breaks onto a new line and may
extend beyond one line.</span>
4. </form>
— The output of the above example will look something like this:
Example
Try this code »
54
<form class="form-horizontal">
<div class="form-group has-success">
<label class="col-xs-2 control-label" for="inputSuccess">Username</label>
<div class="col-xs-10">
<input type="text" id="inputSuccess" class="form-control" placeholder="Input with
success">
<span class="help-block">Username is available</span>
</div>
</div>
<div class="form-group has-warning">
<label class="col-xs-2 control-label" for="inputWarning">Password</label>
<div class="col-xs-10">
<input type="password" id="inputWarning" class="form-control" placeholder="Input with
warning">
<span class="help-block">Password strength: Weak</span>
</div>
</div>
<div class="form-group has-error">
<label class="col-xs-2 control-label" for="inputError">Email</label>
<div class="col-xs-10">
<input type="email" id="inputError" class="form-control" placeholder="Input with error">
<span class="help-block">Please enter a valid email address</span>
</div>
</div>
</form>
— The output of the above example will look something like this:
You can also add optional feedback icons to your inputs using the class .has-
feedback on .form-group and the right glyphicon, like this:
Example
Try this code »
<form class="form-horizontal">
<div class="form-group has-success has-feedback">
<label class="col-xs-2 control-label" for="inputSuccess">Username</label>
<div class="col-xs-10">
55
<input type="text" id="inputSuccess" class="form-control" placeholder="Input with
success">
<span class="glyphicon glyphicon-ok form-control-feedback"></span>
<span class="help-block">Username is available</span>
</div>
</div>
<div class="form-group has-warning has-feedback">
<label class="col-xs-2 control-label" for="inputWarning">Password</label>
<div class="col-xs-10">
<input type="password" id="inputWarning" class="form-control" placeholder="Input with
warning">
<span class="glyphicon glyphicon-warning-sign form-control-feedback"></span>
<span class="help-block">Password strength: Weak</span>
</div>
</div>
<div class="form-group has-error has-feedback">
<label class="col-xs-2 control-label" for="inputError">Email</label>
<div class="col-xs-10">
<input type="email" id="inputError" class="form-control" placeholder="Input with error">
<span class="glyphicon glyphicon-remove form-control-feedback"></span>
<span class="help-block">Please enter a valid email address</span>
</div>
</div>
</form>
Example
Try this code »
<form class="form-horizontal">
<div class="form-group">
<label class="control-label col-xs-3" for="firstName">First Name:</label>
<div class="col-xs-9">
<input type="text" class="form-control" id="firstName" placeholder="First Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-3" for="lastName">Last Name:</label>
<div class="col-xs-9">
<input type="text" class="form-control" id="lastName" placeholder="Last Name">
</div>
56
</div>
<div class="form-group">
<label class="control-label col-xs-3" for="inputEmail">Email Address:</label>
<div class="col-xs-9">
<input type="email" class="form-control" id="inputEmail" placeholder="Email Address">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-3" for="phoneNumber">Phone Number:</label>
<div class="col-xs-9">
<input type="tel" class="form-control" id="phoneNumber" placeholder="Phone Number">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-3">Date of Birth:</label>
<div class="col-xs-3">
<select class="form-control">
<option>Date</option>
</select>
</div>
<div class="col-xs-3">
<select class="form-control">
<option>Month</option>
</select>
</div>
<div class="col-xs-3">
<select class="form-control">
<option>Year</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-3" for="postalAddress">Postal Address:</label>
<div class="col-xs-9">
<textarea rows="3" class="form-control" id="postalAddress" placeholder="Postal
Address"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-3" for="ZipCode">Zip Code:</label>
<div class="col-xs-9">
<input type="text" class="form-control" id="ZipCode" placeholder="Zip Code">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-3">Gender:</label>
<div class="col-xs-2">
<label class="radio-inline">
<input type="radio" name="genderRadios" value="male"> Male
</label>
57
</div>
<div class="col-xs-2">
<label class="radio-inline">
<input type="radio" name="genderRadios" value="female"> Female
</label>
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-3 col-xs-9">
<label class="checkbox-inline">
<input type="checkbox" value="news"> Send me latest news and updates.
</label>
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-3 col-xs-9">
<label class="checkbox-inline">
<input type="checkbox" value="agree"> I agree to the <a href="#">Terms
and Conditions</a>.
</label>
</div>
</div>
<br>
<div class="form-group">
<div class="col-xs-offset-3 col-xs-9">
<input type="submit" class="btn btn-primary" value="Submit">
<input type="reset" class="btn btn-default" value="Reset">
</div>
</div>
</form>
In the next chapter you will learn how to create interactive text-based input controls for
your forms using the Bootstrap input group component.
58
Note:Bootstrap's prepending or appending feature is only available to text-based inputs. It does
not support <select> or <textarea> elements.
Example
Try this code »
<form>
<div class="row">
<div class="col-xs-4">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-
user"></span></span>
<input type="text" class="form-control" placeholder="Username">
</div>
</div>
<div class="col-xs-4">
<div class="input-group">
<input type="text" class="form-control" placeholder="Amount">
<span class="input-group-addon">.00</span>
</div>
</div>
<div class="col-xs-4">
<div class="input-group">
<span class="input-group-addon">$</span>
<input type="text" class="form-control" placeholder="US Dollar">
<span class="input-group-addon">.00</span>
</div>
</div>
</div>
</form>
— The output of the above example will look something like this:
Example
Try this code »
<form>
<div class="row">
<div class="col-xs-6">
<div class="input-group">
59
<span class="input-group-addon">
<input type="checkbox">
</span>
<input type="text" class="form-control">
</div>
</div>
<div class="col-xs-6">
<div class="input-group">
<span class="input-group-addon">
<input type="radio">
</span>
<input type="text" class="form-control">
</div>
</div>
</div>
</form>
— The output of the above example will look something like this:
Example
Try this code »
<form>
<div class="row">
<div class="col-xs-5">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search…">
<span class="input-group-btn">
<button type="button" class="btn btn-default">Go</button>
</span>
</div>
</div>
<div class="col-xs-7">
<div class="input-group">
<input type="text" class="form-control" placeholder="Type something…">
<span class="input-group-btn">
<button type="button" class="btn btn-default">Action</button>
<button type="button" class="btn btn-default">Options</button>
</span>
</div>
60
</div>
</div>
</form>
— The output of the above example will look something like this:
Example
Try this code »
<form>
<div class="row">
<div class="col-xs-6">
<div class="input-group">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-
toggle="dropdown">Action <span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
<input type="text" class="form-control">
</div>
</div>
<div class="col-xs-6">
<div class="input-group">
<input type="text" class="form-control">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-
toggle="dropdown">Action <span class="caret"></span></button>
<ul class="dropdown-menu pull-right">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
</div>
61
</div>
</div>
</form>
— The output of the above example will look something like this:
Example
Try this code »
<form>
<div class="row">
<div class="col-xs-6">
<div class="input-group">
<div class="input-group-btn">
<button tabindex="-1" class="btn btn-default" type="button">Action</button>
<button tabindex="-1" data-toggle="dropdown" class="btn btn-default dropdown-
toggle" type="button">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
<input type="text" class="form-control">
</div>
</div>
<div class="col-xs-6">
<div class="input-group">
<input type="text" class="form-control">
<div class="input-group-btn">
<button tabindex="-1" class="btn btn-default" type="button">Action</button>
<button tabindex="-1" data-toggle="dropdown" class="btn btn-default dropdown-
toggle" type="button">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
62
<ul class="dropdown-menu pull-right">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
</div>
</div>
</div>
</form>
— The output of the above example will look something like this:
Example
Try this code »
< <form>
<div class="row">
<div class="col-xs-4">
<div class="input-group input-group-lg">
<span class="input-group-addon"><span class="glyphicon glyphicon-
user"></span></span>
<input type="text" class="form-control">
</div>
</div>
<div class="col-xs-4">
<div class="input-group input-group-lg">
<span class="input-group-addon">
<input type="checkbox">
</span>
<input type="text" class="form-control">
</div>
</div>
<div class="col-xs-4">
<div class="input-group input-group-lg">
<div class="input-group-btn">
<button tabindex="-1" class="btn btn-default" type="button">Action</button>
63
<button tabindex="-1" data-toggle="dropdown" class="btn btn-default dropdown-
toggle" type="button">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
<input type="text" class="form-control">
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-xs-4">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-
user"></span></span>
<input type="text" class="form-control">
</div>
</div>
<div class="col-xs-4">
<div class="input-group">
<span class="input-group-addon">
<input type="checkbox">
</span>
<input type="text" class="form-control">
</div>
</div>
<div class="col-xs-4">
<div class="input-group">
<div class="input-group-btn">
<button tabindex="-1" class="btn btn-default" type="button">Action</button>
<button tabindex="-1" data-toggle="dropdown" class="btn btn-default dropdown-
toggle" type="button">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
<input type="text" class="form-control">
64
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-xs-4">
<div class="input-group input-group-sm">
<span class="input-group-addon"><span class="glyphicon glyphicon-
user"></span></span>
<input type="text" class="form-control">
</div>
</div>
<div class="col-xs-4">
<div class="input-group input-group-sm">
<span class="input-group-addon">
<input type="checkbox">
</span>
<input type="text" class="form-control">
</div>
</div>
<div class="col-xs-4">
<div class="input-group input-group-sm">
<div class="input-group-btn">
<button tabindex="-1" class="btn btn-default" type="button">Action</button>
<button tabindex="-1" data-toggle="dropdown" class="btn btn-default dropdown-
toggle" type="button">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
<input type="text" class="form-control">
</div>
</div>
</div>
</form>
65
Bootstrap Button Styles
Different classes are available in Bootstrap for styling the buttons as well as to indicate
the different states. Button styles can be applied to any element. However, it is applied
normally to the <a>, <input>, and <button> elements for the best rendering.
The following table lists the different buttons which are available in the Bootstrap:
Button Class Description
btn btn- Provides extra visual weight to indicate primary action button in a set of
primary buttons.
btn btn-info Can be used as an alternative to the default button.
Example
Try this code »
1. <button type="button" class="btn btn-default">Default</button>
2. <button type="button" class="btn btn-primary">Primary</button>
3. <button type="button" class="btn btn-info">Info</button>
4. <button type="button" class="btn btn-success">Success</button>
5. <button type="button" class="btn btn-warning">Warning</button>
6. <button type="button" class="btn btn-danger">Danger</button>
7. <button type="button" class="btn btn-link">Link</button>
Warning:Internet Explorer 9 doesn't crop background gradients on rounded corners, so gradient
is removed from buttons.
66
Bootstrap gives you option further to scaling a button up or down. You can make
buttons larger or smaller through adding an extra class .btn-lg, .btn-sm, or .btn-xs.
Example
Try this code »
<p>
<button type="button" class="btn btn-primary btn-lg">Large button</button>
<button type="button" class="btn btn-default btn-lg">Large button</button>
</p>
<p>
<button type="button" class="btn btn-primary">Default button</button>
<button type="button" class="btn btn-default">Default button</button>
</p>
<p>
<button type="button" class="btn btn-primary btn-sm">Small button</button>
<button type="button" class="btn btn-default btn-sm">Small button</button>
</p>
<p>
<button type="button" class="btn btn-primary btn-xs">Extra small button</button>
<button type="button" class="btn btn-default btn-xs">Extra small button</button>
</p>
— The output of the above example will look something like this:
You can also create block level buttons (buttons that covers the full width of the parent
elements) by adding an extra class .btn-block.
Example
Try this code »
1. <button type="button" class="btn btn-primary btn-lg btn-block">Block level button</button>
2. <button type="button" class="btn btn-default btn-lg btn-block">Block level button</button>
— The output of the above example will look something like this:
67
Bootstrap Disabled Buttons
Sometimes we need to disable a button for certain reasons like, a user in case is not
eligible to perform this particular action, or we want to ensure that user should
performed all other required actions before proceed to this particular action.
Example
Try this code »
1. <a href="#" class="btn btn-primary btn-lg disabled">Primary link</a>
2. <a href="#" class="btn btn-default btn-lg disabled">Link</a>
— The output of the above example will look something like this:
Note:The .disabled class only changes the visual appearance of the link by making it gray and
removing the hover effect, however the link will remain clickable unless you remove
the href attribute. Alternatively, you could implement custom JavaScript to prevent those clicks.
Creating Disabled Buttons Using Button and Input Element
Buttons created through <button> or <input> tag can be disabled by adding
the disabledattribute to the respective element.
Example
Try this code »
1. <button type="button" class="btn btn-primary btn-lg disabled" disabled="disabled">Primary
button</button>
2. <button type="button" class="btn btn-default btn-lg" disabled="disabled">Button</button>
— The output of the above example will look something like this:
68
Example
Try this code »
1. <div class="btn-group">
2. <button type="button" class="btn btn-primary">Left</button>
3. <button type="button" class="btn btn-primary">Middle</button>
4. <button type="button" class="btn btn-primary">Right</button>
5. </div>
— The output of the above example will look something like this:
You can also make the button groups appear vertically stacked rather than horizontally.
To do this just replace the class .btn-group with the .btn-group-vertical.
Example
Try this code »
1. <div class="btn-group-vertical">
2. <button type="button" class="btn btn-primary">Top</button>
3. <button type="button" class="btn btn-primary">Middle</button>
4. <button type="button" class="btn btn-primary">Bottom</button>
5. </div>
Example
Try this code »
<div class="btn-toolbar">
<div class="btn-group">
<button type="button" class="btn btn-primary">1</button>
<button type="button" class="btn btn-primary">2</button>
<button type="button" class="btn btn-primary">3</button>
<button type="button" class="btn btn-primary">4</button>
</div>
<div class="btn-group">
<button type="button" class="btn btn-primary">5</button>
<button type="button" class="btn btn-primary">6</button>
<button type="button" class="btn btn-primary">7</button>
</div>
<div class="btn-group">
69
<button type="button" class="btn btn-primary">8</button>
</div>
</div>
— The output of the above example will look something like this:
Example
Try this code »
<div class="btn-toolbar">
<div class="btn-group btn-group-lg">
<button type="button" class="btn btn-primary">Left</button>
<button type="button" class="btn btn-primary">Middle</button>
<button type="button" class="btn btn-primary">Right</button>
</div>
</div>
<div class="btn-toolbar">
<div class="btn-group">
<button type="button" class="btn btn-primary">Left</button>
<button type="button" class="btn btn-primary">Middle</button>
<button type="button" class="btn btn-primary">Right</button>
</div>
</div>
<div class="btn-toolbar">
<div class="btn-group btn-group-sm">
<button type="button" class="btn btn-primary">Left</button>
<button type="button" class="btn btn-primary">Middle</button>
<button type="button" class="btn btn-primary">Right</button>
</div>
</div>
<div class="btn-toolbar">
<div class="btn-group btn-group-xs">
<button type="button" class="btn btn-primary">Left</button>
<button type="button" class="btn btn-primary">Middle</button>
<button type="button" class="btn btn-primary">Right</button>
</div>
</div>
— The output of the above example will look something like this:
70
Creating Justified Button Groups
You can also make a group of buttons stretch at the same size to span the entire width
of its parent by appling an extra class .btn-group-justified to the .btn-group base class.
The following example will create a justified button group using the <a> element.
Example
Try this code »
1. <div class="btn-group btn-group-justified">
2. <a href="#" class="btn btn-primary">Left</a>
3. <a href="#" class="btn btn-primary">Middle</a>
4. <a href="#" class="btn btn-primary">Right</a>
5. </div>
— The output of the above example will look something like this:
However, to create justified button groups using the <button> elements, you must wrap
each button individually in a .btn-group class, otherwise it will not work.
Example
Try this code »
<div class="btn-group btn-group-justified">
<div class="btn-group">
<button type="button" class="btn btn-primary">Left</button>
</div>
<div class="btn-group">
<button type="button" class="btn btn-primary">Middle</button>
</div>
<div class="btn-group">
<button type="button" class="btn btn-primary">Right</button>
</div>
</div>
71
The story doesn't ends here; in the advanced section you'll learn how to
create button dropdownsas well as stateful buttons through activating the toggling and
loading state on simple buttons and the button groups component.
Example
Try this code »
1. <img src="125x125.jpg" class="img-rounded" alt="Rounded Image">
2. <img src="125x125.jpg" class="img-circle" alt="Circular Image">
3. <img src="125x125.jpg" class="img-thumbnail" alt="Thumbnail Image">
— The output of the above example will look something like this:
Warning:The classes .img-rounded and .img-circle do not work in IE8 and lower versions due
to lack of support for CSS border-radius property.
Example
Try this code »
1. <img src="kites.jpg" class="img-responsive" alt="Flying Kites">
2. <img src="sky.jpg" class="img-responsive" alt="Cloudy Sky">
3. <img src="balloons.jpg" class="img-responsive" alt="Hot Air Balloons">
72
Note:When making the responsive layouts consider making your images responsive too,
otherwise if an image width is larger than the parent element's width in any case it will overflow
and may break your layout.
You can also make the video or slideshow embedded in a web page responsive without
affecting its original aspect ratio. The Bootstrap responsive embed classes can be
applied directly to the <iframe>, <embed>, <video>, and <object> elements.
Example
Try this code »
1. <!-- 16:9 aspect ratio -->
2. <div class="embed-responsive embed-responsive-16by9">
3. <iframe class="embed-responsive-item" src="//www.youtube.com/embed/YE7VzlLtp-4"></iframe>
4. </div>
5.
6. <!-- 4:3 aspect ratio -->
7. <div class="embed-responsive embed-responsive-4by3">
8. <iframe class="embed-responsive-item" src="//www.youtube.com/embed/YE7VzlLtp-4"></iframe>
9. </div>
In the above example, we've created the two responsive videos with two different aspect
ratios (16:9 and 4:3) by adding the classes .embed-responsive-16by9 and .embed-
responsive-4by3 to their containing blocks respectively and the class .embed-responsive-
item to the descendant <iframe> element.
Tip:The aspect ratio of an image describes the proportional relationship between its width and
its height. Two common videographic aspect ratios are 16:9 and 4:3.
Bootstrap Thumbnails
The Bootstrap thumbnail component is very useful for creating grids of images or
videos, lists of products, portfolios, and much more. The following example will show
you how to create thumbnails to showcase linked images.
Example
Try this code »
<div class="container">
<div class="row">
<div class="col-xs-3">
<a href="#" class="thumbnail">
<img src="125x125.jpg" alt="125x125">
</a>
</div>
73
<div class="col-xs-3">
<a href="#" class="thumbnail">
<img src="125x125.jpg" alt="125x125">
</a>
</div>
<div class="col-xs-3">
<a href="#" class="thumbnail">
<img src="125x125.jpg" alt="125x125">
</a>
</div>
<div class="col-xs-3">
<a href="#" class="thumbnail">
<img src="125x125.jpg" alt="125x125">
</a>
</div>
</div>
</div>
— The output of the above example will look something like this:
You can also insert HTML content like headings, paragraphs, or buttons into thumbnails.
Example
Try this code »
<div class="container">
<div class="row">
<div class="col-xs-6">
<div class="thumbnail">
<img src="avatar.jpg" alt="Sample Image">
<div class="caption">
<h3>Thumbnail label</h3>
<p>Thumbnail description…</p>
<p><a href="#" class="btn btn-primary">Share</a> <a href="#" class="btn btn-
default">Download</a></p>
</div>
</div>
</div>
<div class="col-xs-6">
<div class="thumbnail">
<img src="avatar.jpg" alt="Sample Image">
<div class="caption">
<h3>Thumbnail label</h3>
<p>Thumbnail description…</p>
74
<p><a href="#" class="btn btn-primary">Share</a> <a href="#" class="btn btn-
default">Download</a></p>
</div>
</div>
</div>
</div>
</div>
Tip:The thumbnails component uses existing grid classes like .col-xs-*, .col-sm-*, .col-md-
*, .col-lg-*, etc. for controlling the dimensions of thumbnails.
Example
Try this code »
<div class="media">
<div class="media-left">
<a href="#">
<img src="avatar-tiny.jpg" class="media-object" alt="Sample Image">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">Jhon Carter <small><i>Posted on January 10,
2014</i></small></h4>
<p>Excellent feature! I love it. One day I'm definitely going to put this
Bootstrap component into use and I'll let you know once I do.</p>
</div>
</div>
— The output of the above example will look something like this:
You can also apply image modifier classes like .img-rounded, .img-circle etc. to the embedded
image to create other variation of the media component.
Example
Try this code »
<div class="media">
<div class="media-left">
<a href="#">
<img src="avatar-tiny.jpg" class="media-object img-circle" alt="Sample Image">
</a>
</div>
75
<div class="media-body">
<h4 class="media-heading">Jhon Carter <small><i>Posted on January 10,
2014</i></small></h4>
<p>Excellent feature! I love it. One day I'm definitely going to put this
Bootstrap component into use and I'll let you know once I do.</p>
</div>
</div>
— The output of the above example will look something like this:
Example
Try this code »
<!--Top aligned media-->
<div class="media">
<div class="media-left">
<a href="#">
<img src="../images/avatar-tiny.jpg" class="media-object" alt="Sample Image">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">Top aligned media <small><i>This is
Default</i></small></h4>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit…</p>
</div>
</div>
<!--Middle aligned media-->
<div class="media">
<div class="media-left media-middle">
<a href="#">
<img src="../images/avatar-tiny.jpg" class="media-object" alt="Sample Image">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">Middle Aligned Media</h4>
<p>Vestibulum quis quam ut magna consequat faucibus…</p>
</div>
</div>
76
<!--Bottom aligned media-->
<div class="media">
<div class="media-left media-bottom">
<a href="#">
<img src="../images/avatar-tiny.jpg" class="media-object" alt="Sample Image">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">Bottom Aligned Media</h4>
<p>Amet nibh libero, in gravida nulla. Nulla vel metus…</p>
</div>
</div>
Example
Try this code »
<ul class="media-list">
<li class="media">
<div class="media-left">
<a href="#">
<img src="avatar-tiny.jpg" class="media-object" alt="Sample Image">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">Media Heading</h4>
<p>Lorem ipsum dolor sit amet, consectetur adipis elit…</p>
<!-- Nested media object -->
<div class="media">
<div class="media-left">
<a href="#">
<img src="avatar-tiny.jpg" class="media-object" alt="Sample Image">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">Nested Media Heading</h4>
<p>Vestibulum consectetur scelerisque faucibus…</p>
<!-- Nested media object -->
<div class="media">
<div class="media-left">
<a href="#">
77
<img src="avatar-tiny.jpg" class="media-object" alt="Sample
Image">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">Nested Media Heading</h4>
<p>Integer pulvinar leo id risus tempor…</p>
</div>
</div>
</div>
</div>
<!-- Nested media object -->
<div class="media">
<div class="media-left">
<a href="#">
<img src="avatar-tiny.jpg" class="media-object" alt="Sample Image">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">Nested Media Heading</h4>
<p>Phasellus vitae suscipit justo mauris…</p>
</div>
</div>
</div>
</li>
<li class="media">
<div class="media-left">
<a href="#">
<img src="avatar-tiny.jpg" class="media-object" alt="Sample Image">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">Media Heading</h4>
<p>Quisque pharetra velit id velit iaculis pretium…</p>
</div>
</li>
</ul>
Icons by Glyphicons
Bootstrap 3.3.7 includes more than 260 Glyphicons. As opposed to Bootstrap 2.x sprite
versionthese icons are available in font format for better usability and scalability.
Since these Glyphicons are font based, so you can create icons of any color just through
applying the CSS color property on the specific element.
glyphicon glyphicon-asterisk
glyphicon glyphicon-plus
78
glyphicon glyphicon-euro
glyphicon glyphicon-eur
glyphicon glyphicon-minus
glyphicon glyphicon-cloud
glyphicon glyphicon-envelope
glyphicon glyphicon-pencil
glyphicon glyphicon-glass
glyphicon glyphicon-music
glyphicon glyphicon-search
glyphicon glyphicon-heart
glyphicon glyphicon-star
glyphicon glyphicon-star-empty
glyphicon glyphicon-user
glyphicon glyphicon-film
glyphicon glyphicon-th-large
glyphicon glyphicon-th
glyphicon glyphicon-th-list
glyphicon glyphicon-ok
glyphicon glyphicon-remove
glyphicon glyphicon-zoom-in
glyphicon glyphicon-zoom-out
glyphicon glyphicon-off
glyphicon glyphicon-signal
glyphicon glyphicon-cog
glyphicon glyphicon-trash
glyphicon glyphicon-home
glyphicon glyphicon-file
glyphicon glyphicon-time
glyphicon glyphicon-road
glyphicon glyphicon-download-alt
glyphicon glyphicon-download
glyphicon glyphicon-upload
glyphicon glyphicon-inbox
glyphicon glyphicon-play-circle
glyphicon glyphicon-repeat
glyphicon glyphicon-refresh
glyphicon glyphicon-list-alt
glyphicon glyphicon-lock
glyphicon glyphicon-flag
glyphicon glyphicon-headphones
glyphicon glyphicon-volume-off
glyphicon glyphicon-volume-down
glyphicon glyphicon-volume-up
glyphicon glyphicon-qrcode
glyphicon glyphicon-barcode
glyphicon glyphicon-tag
glyphicon glyphicon-tags
glyphicon glyphicon-book
glyphicon glyphicon-bookmark
glyphicon glyphicon-print
glyphicon glyphicon-camera
79
glyphicon glyphicon-font
glyphicon glyphicon-bold
glyphicon glyphicon-italic
glyphicon glyphicon-text-height
glyphicon glyphicon-text-width
glyphicon glyphicon-align-left
glyphicon glyphicon-align-center
glyphicon glyphicon-align-right
glyphicon glyphicon-align-justify
glyphicon glyphicon-list
glyphicon glyphicon-indent-left
glyphicon glyphicon-indent-right
glyphicon glyphicon-facetime-video
glyphicon glyphicon-picture
glyphicon glyphicon-map-marker
glyphicon glyphicon-adjust
glyphicon glyphicon-tint
glyphicon glyphicon-edit
glyphicon glyphicon-share
glyphicon glyphicon-check
glyphicon glyphicon-move
glyphicon glyphicon-step-backward
glyphicon glyphicon-fast-backward
glyphicon glyphicon-backward
glyphicon glyphicon-play
glyphicon glyphicon-pause
glyphicon glyphicon-stop
glyphicon glyphicon-forward
glyphicon glyphicon-fast-forward
glyphicon glyphicon-step-forward
glyphicon glyphicon-eject
glyphicon glyphicon-chevron-left
glyphicon glyphicon-chevron-right
glyphicon glyphicon-plus-sign
glyphicon glyphicon-minus-sign
glyphicon glyphicon-remove-sign
glyphicon glyphicon-ok-sign
glyphicon glyphicon-question-sign
glyphicon glyphicon-info-sign
glyphicon glyphicon-screenshot
glyphicon glyphicon-remove-circle
glyphicon glyphicon-ok-circle
glyphicon glyphicon-ban-circle
glyphicon glyphicon-arrow-left
glyphicon glyphicon-arrow-right
glyphicon glyphicon-arrow-up
glyphicon glyphicon-arrow-down
glyphicon glyphicon-share-alt
glyphicon glyphicon-resize-full
glyphicon glyphicon-resize-small
glyphicon glyphicon-exclamation-sign
80
glyphicon glyphicon-gift
glyphicon glyphicon-leaf
glyphicon glyphicon-fire
glyphicon glyphicon-eye-open
glyphicon glyphicon-eye-close
glyphicon glyphicon-warning-sign
glyphicon glyphicon-plane
glyphicon glyphicon-calendar
glyphicon glyphicon-random
glyphicon glyphicon-comment
glyphicon glyphicon-magnet
glyphicon glyphicon-chevron-up
glyphicon glyphicon-chevron-down
glyphicon glyphicon-retweet
glyphicon glyphicon-shopping-cart
glyphicon glyphicon-folder-close
glyphicon glyphicon-folder-open
glyphicon glyphicon-resize-vertical
glyphicon glyphicon-resize-horizontal
glyphicon glyphicon-hdd
glyphicon glyphicon-bullhorn
glyphicon glyphicon-bell
glyphicon glyphicon-certificate
glyphicon glyphicon-thumbs-up
glyphicon glyphicon-thumbs-down
glyphicon glyphicon-hand-right
glyphicon glyphicon-hand-left
glyphicon glyphicon-hand-up
glyphicon glyphicon-hand-down
glyphicon glyphicon-circle-arrow-right
glyphicon glyphicon-circle-arrow-left
glyphicon glyphicon-circle-arrow-up
glyphicon glyphicon-circle-arrow-down
glyphicon glyphicon-globe
glyphicon glyphicon-wrench
glyphicon glyphicon-tasks
glyphicon glyphicon-filter
glyphicon glyphicon-briefcase
glyphicon glyphicon-fullscreen
glyphicon glyphicon-dashboard
glyphicon glyphicon-paperclip
glyphicon glyphicon-heart-empty
glyphicon glyphicon-link
glyphicon glyphicon-phone
glyphicon glyphicon-pushpin
glyphicon glyphicon-usd
glyphicon glyphicon-gbp
glyphicon glyphicon-sort
glyphicon glyphicon-sort-by-alphabet
glyphicon glyphicon-sort-by-alphabet-alt
glyphicon glyphicon-sort-by-order
81
glyphicon glyphicon-sort-by-order-alt
glyphicon glyphicon-sort-by-attributes
glyphicon glyphicon-sort-by-attributes-alt
glyphicon glyphicon-unchecked
glyphicon glyphicon-expand
glyphicon glyphicon-collapse-down
glyphicon glyphicon-collapse-up
glyphicon glyphicon-log-in
glyphicon glyphicon-flash
glyphicon glyphicon-log-out
glyphicon glyphicon-new-window
glyphicon glyphicon-record
glyphicon glyphicon-save
glyphicon glyphicon-open
glyphicon glyphicon-saved
glyphicon glyphicon-import
glyphicon glyphicon-export
glyphicon glyphicon-send
glyphicon glyphicon-floppy-disk
glyphicon glyphicon-floppy-saved
glyphicon glyphicon-floppy-remove
glyphicon glyphicon-floppy-save
glyphicon glyphicon-floppy-open
glyphicon glyphicon-credit-card
glyphicon glyphicon-transfer
glyphicon glyphicon-cutlery
glyphicon glyphicon-header
glyphicon glyphicon-compressed
glyphicon glyphicon-earphone
glyphicon glyphicon-phone-alt
glyphicon glyphicon-tower
glyphicon glyphicon-stats
glyphicon glyphicon-sd-video
glyphicon glyphicon-hd-video
glyphicon glyphicon-subtitles
glyphicon glyphicon-sound-stereo
glyphicon glyphicon-sound-dolby
glyphicon glyphicon-sound-5-1
glyphicon glyphicon-sound-6-1
glyphicon glyphicon-sound-7-1
glyphicon glyphicon-copyright-mark
glyphicon glyphicon-registration-mark
glyphicon glyphicon-cloud-download
glyphicon glyphicon-cloud-upload
glyphicon glyphicon-tree-conifer
glyphicon glyphicon-tree-deciduous
glyphicon glyphicon-cd
glyphicon glyphicon-save-file
glyphicon glyphicon-open-file
glyphicon glyphicon-level-up
glyphicon glyphicon-copy
82
glyphicon glyphicon-paste
glyphicon glyphicon-alert
glyphicon glyphicon-equalizer
glyphicon glyphicon-king
glyphicon glyphicon-queen
glyphicon glyphicon-pawn
glyphicon glyphicon-bishop
glyphicon glyphicon-knight
glyphicon glyphicon-baby-formula
glyphicon glyphicon-tent
glyphicon glyphicon-blackboard
glyphicon glyphicon-bed
glyphicon glyphicon-apple
glyphicon glyphicon-erase
glyphicon glyphicon-hourglass
glyphicon glyphicon-lamp
glyphicon glyphicon-duplicate
glyphicon glyphicon-piggy-bank
glyphicon glyphicon-scissors
glyphicon glyphicon-bitcoin
glyphicon glyphicon-btc
glyphicon glyphicon-xbt
glyphicon glyphicon-yen
glyphicon glyphicon-jpy
glyphicon glyphicon-ruble
glyphicon glyphicon-rub
glyphicon glyphicon-scale
glyphicon glyphicon-ice-lolly
glyphicon glyphicon-ice-lolly-tasted
glyphicon glyphicon-education
glyphicon glyphicon-option-horizontal
glyphicon glyphicon-option-vertical
glyphicon glyphicon-menu-hamburger
glyphicon glyphicon-modal-window
glyphicon glyphicon-oil
glyphicon glyphicon-grain
glyphicon glyphicon-sunglasses
glyphicon glyphicon-text-size
glyphicon glyphicon-text-color
glyphicon glyphicon-text-background
glyphicon glyphicon-object-align-top
glyphicon glyphicon-object-align-bottom
glyphicon glyphicon-object-align-horizontal
glyphicon glyphicon-object-align-left
glyphicon glyphicon-object-align-vertical
glyphicon glyphicon-object-align-right
glyphicon glyphicon-triangle-right
glyphicon glyphicon-triangle-left
glyphicon glyphicon-triangle-bottom
glyphicon glyphicon-triangle-top
glyphicon glyphicon-console
83
glyphicon glyphicon-superscript
glyphicon glyphicon-subscript
glyphicon glyphicon-menu-left
glyphicon glyphicon-menu-right
glyphicon glyphicon-menu-down
glyphicon glyphicon-menu-up
Tip:Bootstrap icons are provided by Glyphicons. However you are free to use these icons in
your project but as a courtesy you can consider to include a link back to Glyphicons whenever
practical.
Example
Try this code »
1. <button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-search"></span>
Search</button>
2. <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span>
Search</button>
— The output of the above example will look something like this:
Use the Bootstrap button generator tool to create buttons with different icons and styles in
a second. In the next chapter you will see how to use these icons in Bootstrap nav
components.
Note:Remember to leave a space after the closing tag of icon element
(i.e. after </span> tag) when using icons along with the strings of text such as inside buttons or
navigation links, to ensure proper spacing.
84
Bootstrap provides an easy and quick way to create basic nav components like tabs and
pills which are very flexible and elegant. All the Bootstrap's nav components—tabs and
pills—share the same base markup and styles through the base .nav class.
Example
Try this code »
1. <ul class="nav nav-tabs">
2. <li class="active"><a href="#">Home</a></li>
3. <li><a href="#">Profile</a></li>
4. <li><a href="#">Messages</a></li>
5. </ul>
— The output of the above example will look something like this:
Example
Try this code »
1. <ul class="nav nav-tabs">
2. <li class="active"><a href="#"><span class="glyphicon glyphicon-home"></span> Home</a></li>
3. <li><a href="#"><span class="glyphicon glyphicon-user"></span> Profile</a></li>
4. <li><a href="#"><span class="glyphicon glyphicon-envelope"></span> Messages</a></li>
5. </ul>
— The output of the above example will look something like this:
Example
Try this code »
85
1. <ul class="nav nav-pills">
2. <li class="active"><a href="#">Home</a></li>
3. <li><a href="#">Profile</a></li>
4. <li><a href="#">Messages</a></li>
5. </ul>
— The output of the above example will look something like this:
Example
Try this code »
1. <ul class="nav nav-pills">
2. <li class="active"><a href="#"><span class="glyphicon glyphicon-home"></span> Home</a></li>
3. <li><a href="#"><span class="glyphicon glyphicon-user"></span> Profile</a></li>
4. <li><a href="#"><span class="glyphicon glyphicon-envelope"></span> Messages</a></li>
5. </ul>
— The output of the above example will look something like this:
Example
Try this code »
1. <ul class="nav nav-pills nav-stacked">
2. <li class="active"><a href="#">Home</a></li>
3. <li><a href="#">Profile</a></li>
4. <li><a href="#">Messages</a></li>
5. </ul>
— The output of the above example will look something like this:
86
Bootstrap Tabs and Pills Nav with Dropdown Menus
You can add dropdown menus to a link inside tabs and pills nav with a little extra
markup.
The four CSS classes .dropdown, .dropdown-toggle, .dropdown-menu and .caret are required
in addition to the .nav, .nav-tabs, .nav-pills to create a simple dropdown menu.
Example
Try this code »
<ul class="nav nav-tabs">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Profile</a></li>
<li class="dropdown">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">Messages <b
class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Inbox</a></li>
<li><a href="#">Drafts</a></li>
<li><a href="#">Sent Items</a></li>
<li class="divider"></li>
<li><a href="#">Trash</a></li>
</ul>
</li>
</ul>
— The output of the above example will look something like this:
Example
Try this code »
<ul class="nav nav-pills">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Profile</a></li>
87
<li class="dropdown">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">Messages <b
class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Inbox</a></li>
<li><a href="#">Drafts</a></li>
<li><a href="#">Sent Items</a></li>
<li class="divider"></li>
<li><a href="#">Trash</a></li>
</ul>
</li>
</ul>
— The output of the above example will look something like this:
Example
Try this code »
<!-- Justified tabs -->
<ul class="nav nav-tabs nav-justified">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Profile</a></li>
<li><a href="#">Messages</a></li>
</ul>
<!-- Justified pills -->
<ul class="nav nav-pills nav-justified">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Profile</a></li>
88
<li><a href="#">Messages</a></li>
</ul>
Example
Try this code »
1. <ul class="nav">
2. <li class="active"><a href="#">Home</a></li>
3. <li><a href="#">Profile</a></li>
4. <li><a href="#">Messages</a></li>
5. <li class="disabled"><a href="#">Admin</a></li>
6. </ul>
Note:The Bootstrap .disabled class only changes the visual appearance of the link by making it
gray and removing the hover effect, however the link will remain clickable unless you remove
the "href" attribute.
Example
Try this code »
<nav class="navbar navbar-default">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-
toggle">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
89
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="#" class="navbar-brand">Brand</a>
</div>
<!-- Collection of nav links and other content for toggling -->
<div id="navbarCollapse" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Profile</a></li>
<li><a href="#">Messages</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Login</a></li>
</ul>
</div>
</nav>
— The output of the above example will look something like this:
Note:Use the classes .navbar-left or .navbar-right instead of .pull-left or .pull-right to
align the nav links, forms, buttons or text inside the navbar.
You can also include dropdowns and search box within the navbars.
Example
Try this code »
<nav class="navbar navbar-default">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-
toggle">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="#" class="navbar-brand">Brand</a>
</div>
<!-- Collection of nav links, forms, and other content for toggling -->
<div id="navbarCollapse" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Profile</a></li>
<li class="dropdown">
<a data-toggle="dropdown" class="dropdown-toggle" href="#">Messages <b
class="caret"></b></a>
90
<ul class="dropdown-menu">
<li><a href="#">Inbox</a></li>
<li><a href="#">Drafts</a></li>
<li><a href="#">Sent Items</a></li>
<li class="divider"></li>
<li><a href="#">Trash</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search">
<span class="input-group-btn">
<button type="button" class="btn btn-default"><span class="glyphicon glyphicon-
search"></span></button>
</span>
</div>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Login</a></li>
</ul>
</div>
</nav>
— The output of the above example will look something like this:
Tip:To create navbars that is not fixed on the top or bottom, place it anywhere within
a .container, which sets the width of your site and content.
Check out the snippets section for examples of some beautifully designed Bootstrap
navbars.
Example
Try this code »
91
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-
toggle">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="#" class="navbar-brand">Brand</a>
</div>
<!-- Collection of nav links and other content for toggling -->
<div id="navbarCollapse" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Profile</a></li>
<li><a href="#">Messages</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Login</a></li>
</ul>
</div>
</div>
</nav>
Tip:Place the fixed .navbar content inside the .container or .container-fluid for proper
padding and alignment with the rest of the content.
Creating Navbar Fixed to Bottom
Similarly to create navbars that is fixed at the bottom add the class .navbar-fixed-bottom.
Example
Try this code »
<nav class="navbar navbar-default navbar-fixed-bottom">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-
toggle">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="#" class="navbar-brand">Brand</a>
</div>
<!-- Collection of nav links and other content for toggling -->
<div id="navbarCollapse" class="collapse navbar-collapse">
92
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Profile</a></li>
<li><a href="#">Messages</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Login</a></li>
</ul>
</div>
</div>
</nav>
Note:Remember to add padding (at least 70px) to the top or bottom of the <body>element to avoid
the content to go underneath the navbar while implementing fixed top or bottom navbar. Also be
sure to add your custom style sheet after the core Bootstrap CSS file, otherwise it may not work.
Example
Try this code »
<nav class="navbar navbar-default navbar-static-top">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-
toggle">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="#" class="navbar-brand">Brand</a>
</div>
<!-- Collection of nav links and other content for toggling -->
<div id="navbarCollapse" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Profile</a></li>
<li><a href="#">Messages</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
93
<li><a href="#">Login</a></li>
</ul>
</div>
</div>
</nav>
Example
Try this code »
<nav class="navbar navbar-default">
<div class="navbar-header">
<button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-
toggle">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="#" class="navbar-brand">Brand</a>
</div>
<div id="navbarCollapse" class="collapse navbar-collapse">
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" placeholder="Search" class="form-control">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</nav>
Example
94
Try this code »
<nav class="navbar navbar-inverse">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-
toggle">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="#" class="navbar-brand">Brand</a>
</div>
<!-- Collection of nav links, forms, and other content for toggling -->
<div id="navbarCollapse" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Profile</a></li>
<li class="dropdown">
<a data-toggle="dropdown" class="dropdown-toggle" href="#">Messages <b
class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Inbox</a></li>
<li><a href="#">Drafts</a></li>
<li><a href="#">Sent Items</a></li>
<li class="divider"></li>
<li><a href="#">Trash</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search">
<span class="input-group-btn">
<button type="button" class="btn btn-default"><span class="glyphicon glyphicon-
search"></span></button>
</span>
</div>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Login</a></li>
</ul>
</div>
</nav>
— The output of the above example will look something like this:
95
Introducing Bootstrap 3 Panels
Sometimes you might require to place your content in box for better presentation. In
such situation the Bootstrap panel component can be very useful. In most basic form
the panel component applies some border and padding around the content.
Example
Try this code »
1. <div class="panel panel-default">
2. <div class="panel-body">Look, I'm in a panel!</div>
3. </div>
— The output of the above example will look something like this:
Example
Try this code »
1. <div class="panel panel-default">
2. <div class="panel-heading">This Page is Disabled</div>
3. <div class="panel-body">This page is temporarily disabled by the site administrator
for some reason.<br> <a href="#">Click here</a> to enable the page.</div>
4. </div>
— The output of the above example will look something like this:
Example
Try this code »
1. <div class="panel panel-default">
2. <div class="panel-heading">
3. <h1 class="panel-title">Panel Title</h1>
4. </div>
96
5. <div class="panel-body">Panel content…</div>
6. </div>
Example
Try this code »
1. <div class="panel panel-default">
2. <div class="panel-body">This page is temporarily disabled by the site administrator
for some reason.</div>
3. <div class="panel-footer clearfix">
4. <div class="pull-right">
5. <a href="#" class="btn btn-primary">Learn More</a>
6. <a href="#" class="btn btn-default">Go Back</a>
7. </div>
8. </div>
9. </div>
— The output of the above example will look something like this:
Example
Try this code »
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">301 Moved Permanently</h3>
</div>
97
<div class="panel-body">The requested page has been permanently moved to a new
location.</div>
</div>
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">200 OK</h3>
</div>
<div class="panel-body">The server successfully processed the request.</div>
</div>
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">100 Continue</h3>
</div>
<div class="panel-body">The client should continue with its request.</div>
</div>
<div class="panel panel-warning">
<div class="panel-heading">
<h3 class="panel-title">400 Bad Request</h3>
</div>
<div class="panel-body">The request cannot be fulfilled due to bad syntax.</div>
</div>
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title">503 Service Unavailable</h3>
</div>
<div class="panel-body">The server is temporarily unable to handle the
request.</div>
</div>
— The output of the above example will look something like this:
98
Placing Tables and List Groups inside Panels
You can add any non-bordered table within a panel to create more informative tables.
Example
Try this code »
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">User Information</div>
<div class="panel-body">
<p>The following table contains some personal information about users.</p>
</div>
<!-- Table -->
<table class="table">
99
<thead>
<tr>
<th>Row</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John</td>
<td>Carter</td>
<td>[email protected]</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Parker</td>
<td>[email protected]</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
<td>Rambo</td>
<td>[email protected]</td>
</tr>
</tbody>
</table>
</div>
Similarly you can include full-width list groups within any panel.
Example
Try this code »
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">Products</div>
<div class="panel-body">
<p>The following products are currently available on our store.</p>
</div>
<!-- List group -->
<div class="list-group">
<a href="#" class="list-group-item">Mobile Phones <span class="badge">50</span>
</a>
<a href="#" class="list-group-item">Laptops & Desktops <span
class="badge">145</span>
</a>
<a href="#" class="list-group-item">Tablets <span class="badge">30</span>
</a>
100
<a href="#" class="list-group-item">Audio & Video Players <span
class="badge">65</span>
</a>
<a href="#" class="list-group-item">Camera <span class="badge">8</span>
</a>
</div>
</div>
Example
Try this code »
1. <ul class="breadcrumb">
2. <li><a href="#">Home</a></li>
3. <li><a href="#">Products</a></li>
4. <li class="active">Accessories</li>
5. </ul>
— The output of the above example will look something like this:
Tip:Separators inside the breadcrumbs components are added automatically via CSS
through ::before pseudo-element and content property.
Example
Try this code »
<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>
101
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a href="#">»</a></li>
</ul>
— The output of the above example will look something like this:
Example
Try this code »
<ul class="pagination">
<li class="disabled"><a href="#">«</a></li>
<li class="active"><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>
— The output of the above example will look something like this:
Note:The .disabled class only displays links as it disabled it doesn't remove the click
functionality, to do this you can swap active or disabled anchors with spans.
Example
Try this code »
<ul class="pagination">
<li class="disabled"><span>«</span></li>
<li class="active"><span>1</span></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>
102
</ul>
Example
Try this code »
<!-- Larger pagination -->
<ul class="pagination pagination-lg">
<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>
<!-- Default pagination -->
<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>
<!-- Smaller pagination -->
<ul class="pagination pagination-sm">
<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>
— The output of the above example will look something like this:
103
Bootstrap Pager
Sometimes you may simply require previous and next links on your website to provide
simple and quick navigation to the user instead of the complex pagination as we
discussed above.
This can be done using the Bootstrap class .pager.
Example
Try this code »
1. <ul class="pager">
2. <li><a href="#">← Previous</a></li>
3. <li><a href="#">Next →</a></li>
4. </ul>
— The output of the above example will look something like this:
Alignment of Pager
By default pager are aligned horizontally center but you align previous link to left and
next link right using the class .previous and .next respectively.
Example
Try this code »
1. <ul class="pager">
2. <li class="previous"><a href="#">← Previous</a></li>
3. <li class="next"><a href="#">Next →</a></li>
4. </ul>
— The output of the above example will look something like this:
104
You can also apply the same pagination classes .disabled and .active to the pager.
Example
Try this code »
1. <ul class="pager">
2. <li class="previous disabled"><a href="#">← Previous</a></li>
3. <li class="next"><a href="#">Next →</a></li>
4. </ul>
— The output of the above example will look something like this:
Example
Try this code »
1. <h1>Bootstrap heading <span class="label label-default">New</span></h1>
2. <h2>Bootstrap heading <span class="label label-default">New</span></h2>
3. <h3>Bootstrap heading <span class="label label-default">New</span></h3>
4. <h4>Bootstrap heading <span class="label label-default">New</span></h4>
5. <h5>Bootstrap heading <span class="label label-default">New</span></h5>
6. <h6>Bootstrap heading <span class="label label-default">New</span></h6>
— The output of the above example will look something like this:
Example
105
Try this code »
1. <span class="label label-default">Default</span>
2. <span class="label label-primary">Primary</span>
3. <span class="label label-success">Success</span>
4. <span class="label label-info">Info</span>
5. <span class="label label-warning">Warning</span>
6. <span class="label label-danger">Danger</span>
— The output of the above example will look something like this:
Example
Try this code »
1. <ul class="nav nav-pills">
2. <li><a href="#">Home</a></li>
3. <li><a href="#">Profile</a></li>
4. <li class="active"><a href="#">Messages <span class="badge">24</span></a></li>
5.
6. <li><a href="#">Notification <span class="badge">5</span></a></li>
7. </ul>
— The output of the above example will look something like this:
Example
Try this code »
1. <div class="progress">
2. <div class="progress-bar" style="width: 60%;">
3. <span class="sr-only">60% Complete</span>
4. </div>
5. </div>
— The output of the above example will look something like this:
106
Creating Progress Bar with Label
To show to the progress status as a percentage label just remove the <span> with .sr-
only class from within the progress bar as demonstrated in example above.
Example
Try this code »
1. <div class="progress">
2. <div class="progress-bar" style="width: 60%;">
3. 60%
4. </div>
5. </div>
— The output of the above example will look something like this:
If you are showing percentage label you should also add a min-width to the progress bar
to ensure that the label text remains readable even for low percentage, like this.
Example
Try this code »
<div class="progress">
<div class="progress-bar" style="min-width: 20px;">
0%
</div>
</div>
<div class="progress">
<div class="progress-bar" style="min-width: 20px; width: 2%;">
2%
</div>
</div>
Example
Try this code »
1. <div class="progress progress-striped">
2. <div class="progress-bar" style="width: 60%;">
3. <span class="sr-only">60% Complete</span>
107
4. </div>
5. </div>
— The output of the above example will look something like this:
Similarly you can create the animated progress bar — just add the .active class
to .progress-stripped. The .active class animates the stripes from right to left.
Example
Try this code »
1. <div class="progress progress-striped active">
2. <div class="progress-bar" style="width: 60%;">
3. <span class="sr-only">60% Complete</span>
4. </div>
5. </div>
Warning:Due to lack of support for CSS3 animation properties the animated progress bar is not
supported in any versions of IE upto IE9. The stripped progress bar uses CSS3 gradient to create
the striped effect which is not supported in IE7-8.
Example
Try this code »
<!-- Progress bar HTML -->
<div class="progress progress-striped active">
<div class="progress-bar"></div>
</div>
<!-- jQuery Script -->
<script type="text/javascript">
var i = 0;
function makeProgress(){
if(i < 100){
i = i + 1;
$(".progress-bar").css("width", i + "%").text(i + " %");
}
// Wait for sometime before running this script again
setTimeout("makeProgress()", 100);
}
makeProgress();
108
</script>
Example
Try this code »
<div class="progress">
<div class="progress-bar progress-bar-success" style="width: 40%">
<span class="sr-only">Program Files (40%)</span>
</div>
<div class="progress-bar progress-bar-warning" style="width: 25%">
<span class="sr-only">Residual Files (25%)</span>
</div>
<div class="progress-bar progress-bar-danger" style="width: 15%">
<span class="sr-only">Junk Files (15%)</span>
</div>
</div>
— The output of the above example will look something like this:
Example
Try this code »
<div class="progress">
<div class="progress-bar progress-bar-info" style="width: 20%">
<span class="sr-only">20% Used</span>
</div>
</div>
<div class="progress">
<div class="progress-bar progress-bar-success" style="width: 40%">
<span class="sr-only">40% Used</span>
</div>
</div>
<div class="progress">
<div class="progress-bar progress-bar-warning" style="width: 80%">
109
<span class="sr-only">80% Used</span>
</div>
</div>
<div class="progress">
<div class="progress-bar progress-bar-danger" style="width: 90%">
<span class="sr-only">90% Used</span>
</div>
</div>
— The output of the above example will look something like this:
Example
Try this code »
<div class="progress progress-striped">
<div class="progress-bar progress-bar-info" style="width: 20%">
<span class="sr-only">20% Used</span>
</div>
</div>
<div class="progress progress-striped">
<div class="progress-bar progress-bar-success" style="width: 40%">
<span class="sr-only">40% Used</span>
</div>
</div>
<div class="progress progress-striped">
<div class="progress-bar progress-bar-warning" style="width: 80%">
<span class="sr-only">80% Used</span>
</div>
</div>
<div class="progress progress-striped">
<div class="progress-bar progress-bar-danger" style="width: 90%">
<span class="sr-only">90% Used</span>
</div>
</div>
— The output of the above example will look something like this:
110
Showcasing Contents with Jumbotron
The Bootstrap jumbotron component provides an excellent way to showcase the key
content or information on a web page. Just wrap your featured content like heading,
descriptions etc. in a <div> element and apply the class .jumbotron on it.
Example
Try this code »
1. <div class="jumbotron">
2. <h1>Learn to Create Websites</h1>
3. <p>In today's world internet is the most popular way…</p>
4. <p><a href="#" class="btn btn-primary btn-lg">Learn more</a></p>
5. </div>
— The output of the above example will look something like this:
111
To create a jumbotron without rounded corners and that covers the full width of the
viewport, place it outside all the containers and add the .container within like this.
Example
Try this code »
1. <div class="jumbotron">
2. <div class="container">
3. <h1>Learn to Create Websites</h1>
4. <p>In today's world internet is the most popular way…</p>
5. <p><a href="#" class="btn btn-primary btn-lg">Learn more</a></p>
6. </div>
7. </div>
Example
Try this code »
1. <div class="well">
2. Look, I'm in a well!
3. </div>
— The output of the above example will look something like this:
Example
Try this code »
1. <div class="well well-lg">
2. Look, I'm in a large well!
3. </div>
4. <div class="well well-sm">
5. Look, I'm in a small well!
112
6. </div>
Tip:If you're going to place the floated content inside a well, make sure to add the
class .clearfix to the .well element to prevent parent collapsing.
Contextual Colors
You can use the contextual color classes like .text-success, .text-info, .text-
warning etc. to emphasize the text. See the tutorial on Bootstrap typography for more
detail.
Contextual Backgrounds
Similar to the contextual text color classes, you can use the contextual background color
classes to set the background-color of an element to apply extra emphasis on them.
Example
Try this code »
1. <p class="bg-primary">Important: Please read the instructions carefully before
proceeding.</p>
2. <p class="bg-success">Success: Your message has been sent successfully.</p>
3. <p class="bg-info">Note: You must agree with the terms and conditions to complete the
sign up process.</p>
4. <p class="bg-warning">Warning: There was a problem with your network connection.</p>
5. <p class="bg-danger">Error: An error has been occurred while submitting your data.</p>
— The output of the above example will look something like this:
113
Note:The contextual background classes only apply the CSS background-color property on the
element. To adjust the space between content and border-box you have to set the
CSS padding property yourself.
Close Icon
Bootstrap provides a generic close icon that can be used for dismissing modals and
alerts.
Example
Try this code »
1. <div class="alert alert-warning">
2. <a href="#" class="close" data-dismiss="alert">×</a>
3. <strong>Warning!</strong> There was a problem with your network connection.
4. </div>
— The output of the above example will look something like this:
Caret Icon
Bootstrap provides a generic caret icon to indicate the dropdown functionality. The
direction of the caret icon will reverse automatically in dropup menus.
Example
Try this code »
<ul class="nav nav-pills">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li class="dropdown">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">Services <span
class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Design</a></li>
<li><a href="#">Development</a></li>
</ul>
</li>
</ul>
114
— The output of the above example will look something like this:
Example
Try this code »
1. <div class="wrapper">
2. <div class="center-block">Center Aligned DIV Box</div>
3. </div>
See the tutorial on CSS alignment to learn more about aligning the elements.
Quick Floats
You can quickly float an element to the left or right using the .pull-left and .pull-
rightclasses. These classes included the CSS !important declaration to avoid specificity
issues.
Example
Try this code »
1. <div class="pull-left">Floated to left.</div>
2. <div class="pull-right">Floated to right.</div>
Clearfix
The .clearfix class clears the float on any element. This class is widely used for fixing
the collapsing parent issue, where parent element contains floated boxes.
Example
115
Try this code »
1. <div class="wrapper clearfix">
2. <div class="pull-left">Float to left</div>
3. <div class="pull-right">Float to right</div>
4. </div>
See the tutorial on CSS alignment to learn more about clearing floats on an element.
Example
Try this code »
1. <div class="show">This is visible to the user.</div>
2. <div class="hidden">This is not visible to the user.</div>
3. <div class="invisible">This is not visible but affects the layout.</div>
Example
Try this code »
1. <p>This paragraph is visible to all devices.</p>
2. <p class="sr-only">This paragraph is only visible to screen readers.</p>
Example
116
Try this code »
1. <h1 class="text-hide">The text of this heading is not visible</h1>
2. <p class="text-hide">The text of this paragraph is not visible.</p>
Warning:Think twice before using this class, because it uses the styles
color: transparent and font-size: 0px to hide the text. Search engines consider such
techniques as spam that may result in penalty.
Example
Try this code »
<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-
hidden="true">×</button>
<h4 class="modal-title">Confirmation</h4>
</div>
<div class="modal-body">
<p>Do you want to save changes you made to document before
closing?</p>
<p class="text-warning"><small>If you don't save, your changes will be
lost.</small></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
— The above example launches the modal window automatically when the DOM is fully
loaded via JavaScript. The output will look something like this:
117
Tip:Always try to place your modal HTML in a top-level position in your document, preferably
before closing of the <body> tag (i.e. </body>) to avoid interference from other elements,
otherwise it may affect modal's appearance or functionality.
Check out the snippets section for examples of some beautifully designed Bootstrap
modals.
Example
Try this code »
<!-- Button HTML (to Trigger Modal) -->
<a href="#myModal" role="button" class="btn btn-large btn-primary" data-toggle="modal">Launch Demo
Modal</a>
<!-- Modal HTML -->
<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-
hidden="true">×</button>
<h4 class="modal-title">Confirmation</h4>
</div>
<div class="modal-body">
<p>Do you want to save changes you made to document before
closing?</p>
118
<p class="text-warning"><small>If you don't save, your changes will be
lost.</small></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
The above example launches the modal window on click of the "Launch Demo Modal"
button. Let's go through each part of this modal code one by one for a better
understanding.
Explanation of Code
To activate a Bootstrap modal via data attributes we basically need two components —
the controller element like a button or link, and the modal element itself.
The outermost container of every modal in a document must have a unique id (in this
case id="myModal", line no-5), so that it can be targeted via data-target (for buttons) or href(for
hyperlinks) attribute of the controller element (line no-2).
The attribute data-toggle="modal" is required to add on the controller element (line no-
2), like a button or an anchor, along with a attribute data-
target="#myModal" or href="#myModal" to target a specific modal to toggle.
The .modal-dialog class (line no-6) sets the width as well as horizontal and vertical
alignment of the modal box. Whereas the class .modal-content sets the styles like text and
background color, borders, rounded corners etc.
Rest of the thing is self explanatory, such as the .modal-header element defines a header
for the modal that usually contains a modal title and a close button, whereas the .modal-
body element contains the actual content like text, images, forms etc. and the .modal-
footer element defines the footer that typically contains action buttons for the user.
Note:The .fade class on the .modal element adds a fading and sliding animation effect while
showing and hiding the modal window. If you want the modal that simply appear without any
effect you can just remove this class.
119
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".btn").click(function(){
4. $("#myModal").modal('show');
5. });
6. });
7. </script>
Example
Try this code »
<!-- Large modal -->
<button class="btn btn-primary" data-toggle="modal" data-target="#largeModal">Large modal</button>
<div id="largeModal" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-
hidden="true">×</button>
<h4 class="modal-title">Large Modal</h4>
</div>
<div class="modal-body">
<p>Add the <code>.modal-lg</code> class on <code>.modal-dialog</code>
to create this large modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">OK</button>
</div>
</div>
</div>
</div>
<!-- Small modal -->
<button class="btn btn-primary" data-toggle="modal" data-target="#smallModal">Small modal</button>
120
<div id="smallModal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-
hidden="true">×</button>
<h4 class="modal-title">Small Modal</h4>
</div>
<div class="modal-body">
<p>Add the <code>.modal-sm</code> class on <code>.modal-dialog</code>
to create this small modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">OK</button>
</div>
</div>
</div>
</div>
Example
Try this code »
<script type="text/javascript">
$(document).ready(function(){
$("#myModal").on('show.bs.modal', function(event){
// Get button that triggered the modal
var button = $(event.relatedTarget);
// Extract value from data-* attributes
var titleData = button.data('title');
$(this).find('.modal-title').text(titleData + ' Form');
});
});
</script>
121
Options
There are certain options which can be passed to modal() Bootstrap method to
customize the functionality of a modal. Options can be passed via data attributes or
JavaScript.
For setting the modals options via data attributes, just append the option name
to data-, like data-backdrop="static", data-keyboard="false" etc.
Name Type Default Value Description
Example
Try this code »
<!-- Button HTML (to Trigger Modal) -->
<a href="remote.html" role="button" class="btn btn-large btn-primary" data-toggle="modal" data-
target="#myModal">Launch Demo Modal</a>
<!-- Modal HTML -->
<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<!-- Content will be loaded here from "remote.php" file -->
</div>
</div>
</div>
Note:The remote option for the Bootstrap modals is deprecated since v3.3.0 and will be
removed in upcoming v4. Use the client-side templating or a data binding framework instead, or
call the jQuery load() method yourself.
122
Methods
These are the standard bootstrap's modals methods:
.modal(options)
This method activates the content as a modal. It also allows you to set options for them.
The jQuery code in the following example will prevent the modal from closing when a
user clicks on the backdrop i.e. black overlay area behind the modal.
Example
Try this code »
<script type="text/javascript">
$(document).ready(function(){
$(".launch-modal").click(function(){
$("#myModal").modal({
backdrop: 'static'
});
});
});
</script>
The following jQuery code will prevent the modal from closing on press of the escape
key.
Example
Try this code »
<script type="text/javascript">
$(document).ready(function(){
$(".launch-modal").click(function(){
$("#myModal").modal({
keyboard: false
});
});
});
</script>
The jQuery code in the following example will create a modal in which content of the
modal will be inserted from a remote file upon activation.
Example
Try this code »
<script type="text/javascript">
$(document).ready(function(){
$(".launch-modal").click(function(){
$("#myModal").modal({
123
remote: '../remote.php'
});
});
});
</script>
.modal('toggle')
This method toggles a modal window manually.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".toggle-modal").click(function(){
4. $("#myModal").modal('toggle');
5. });
6. });
7. </script>
.modal('show')
This method can be used to open a modal window manually.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".open-modal").click(function(){
4. $("#myModal").modal('show');
5. });
6. });
7. </script>
.modal('hide')
This method can be used to hide a modal window manually.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".hide-modal").click(function(){
4. $("#myModal").modal('hide');
5. });
6. });
7. </script>
.modal('handleUpdate')
This method readjusts the modal's position to counter the jerk that is occurring due to
the appearance of the viewport scrollbar in case if the modal height changes in such a
way that it becomes higher than the viewport height while it is open.
124
A common example of this scenario is showing the hidden elements inside the modal
via JavaScript or loading content inside the modal using Ajax after activation.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".show-text").click(function(){
4. $('#myModal').find(".lots-of-text").toggle();
5. $('#myModal').modal('handleUpdate')
6. });
7. });
8. </script>
Events
Bootstrap's modal class includes few events for hooking into modal functionality.
Event Description
show.bs.modal This event fires immediately when the show instance method is called.
shown.bs.modal This event is fired when the modal has been made visible to the user. It will wait until
the CSS transition process has been fully completed before getting fired.
hide.bs.modal This event is fired immediately when the hide instance method has been called.
hidden.bs.modal This event is fired when the modal has finished being hidden from the user. It will
wait until the CSS transition process has been fully completed before getting fired.
loaded.bs.modal This event is fired when the modal has loaded content using the remoteoption.
The following example displays an alert message to the user when fade out transition of
the modal window has been fully completed.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myModal").on('hidden.bs.modal', function(){
4. alert("Modal window has been completely closed.");
5. });
6. });
7. </script>
125
Tip:See also the Bootstrap FAQ section for more examples on modals, like setting vertical
alignment, changing default width, embedding video, etc.
Example
Try this code »
1. <div class="dropdown">
2. <a href="#" data-toggle="dropdown" class="dropdown-toggle">Dropdown <b class="caret"></b></a>
3. <ul class="dropdown-menu">
4. <li><a href="#">Action</a></li>
5. <li><a href="#">Another action</a></li>
6. </ul>
7. </div>
The above example demonstrates the most basic form of the Bootstrap dropdowns.
Let's understand each part of the Bootstrap dropdown component one by one.
Explanation of Code
The Bootstrap dropdown has basically two components — the dropdown trigger
element which can be a hyperlink or button, and the dropdown menu itself.
The .dropdown class specifies a dropdown menu.
The .dropdown-toggle class defines the trigger element, which is a hyperlink in our case,
whereas the attribute data-toggle="dropdown" is required on the trigger element to toggle the
dropdown menu.
The .caret element inside the trigger anchor element creates a small down triangle icon
which indicates that the link contains a dropdown menu.
The unordered list with the class .dropdown-menu is actually building the dropdown menu
that typically contains the related links or actions.
The previous example code has one small problem. If you click the dropdown link it will
add a #character to the URL while showing the dropdowns. If you want to keep your
URLs intact use the data-target attribute instead of href="#", like this:
126
Example
Try this code »
1. <div class="dropdown">
2. <a data-target="#" href="page.html" data-toggle="dropdown" class="dropdown-toggle">Dropdown <b
class="caret"></b></a>
3. <ul class="dropdown-menu">
4. <li><a href="#">Action</a></li>
5. <li><a href="#">Another action</a></li>
6. </ul>
7. </div>
Similarly, you can add the dropdowns to the buttons and nav components. The
following section will show you some common implementation of the Bootstrap
dropdown.
Example
Try this code »
<div class="navbar navbar-static">
<div class="navbar-inner">
<a href="#" class="brand">Brand</a>
<ul role="navigation" class="nav">
<li><a href="#">Home</a></li>
<li><a href="#">Profile</a></li>
<li class="dropdown">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">Messages <b
class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Inbox</a></li>
<li><a href="#">Drafts</a></li>
<li><a href="#">Sent Items</a></li>
<li class="divider"></li>
<li><a href="#">Trash</a></li>
</ul>
</li>
</ul>
<ul class="nav pull-right">
<li class="dropdown">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">Admin <b
class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
127
<li><a href="#">Another action</a></li>
<li class="divider"></li>
<li><a href="#">Settings</a></li>
</ul>
</li>
</ul>
</div>
</div>
— The output of the above example will look something like this:
Tip:You can draw a divider line to separate the links inside a dropdown menu by adding the
class .divider on a blank list element.
Example
Try this code »
<ul class="nav nav-pills">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Profile</a></li>
<li class="dropdown">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">Messages <b
class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Inbox</a></li>
<li><a href="#">Drafts</a></li>
<li><a href="#">Sent Items</a></li>
<li class="divider"></li>
<li><a href="#">Trash</a></li>
</ul>
</li>
<li class="dropdown pull-right">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">Admin <b
class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li class="divider"></li>
<li><a href="#">Settings</a></li>
</ul>
128
</li>
</ul>
— The output of the above example will look something like this:
You can simply convert the above example to a tab dropdown by replacing the
class .nav-pillswith the .nav-tabs, without any further change in markup.
Example
Try this code »
1. <div class="btn-group">
2. <button type="button" data-toggle="dropdown" class="btn btn-default dropdown-toggle">Action
<span class="caret"></span></button>
3. <ul class="dropdown-menu">
4. <li><a href="#">Action</a></li>
5. <li><a href="#">Another action</a></li>
6. <li class="divider"></li>
7. <li><a href="#">Separated link</a></li>
8. </ul>
9. </div>
— The output of the above example will look something like this:
Example
Try this code »
<div class="btn-group">
<button type="button" class="btn btn-default">Action</button>
<button type="button" data-toggle="dropdown" class="btn btn-default dropdown-toggle"><span
class="caret"></span></button>
<ul class="dropdown-menu">
129
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
— The output of the above example will look something like this:
Tip:You can use the Bootstrap's button relative sizing classes like .btn-lg, .btn-sm and .btn-
xs to further resizing the buttons dropdowns.
Example
Try this code »
<div class="btn-group">
<button type="button" class="btn btn-primary">Button</button>
<button type="button" class="btn btn-primary">Another Button</button>
<div class="btn-group">
<button type="button" data-toggle="dropdown" class="btn btn-primary dropdown-
toggle">Dropdown <span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
</div>
— The output of the above example will look something like this:
Similarly, you can crate dropdown inside vertically stacked button groups, like this:
Example
Try this code »
<div class="btn-group-vertical">
<button type="button" class="btn btn-primary">Button</button>
130
<button type="button" class="btn btn-primary">Another Button</button>
<div class="btn-group">
<button type="button" data-toggle="dropdown" class="btn btn-primary dropdown-
toggle">Dropdown <span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
</div>
Example
Try this code »
<div class="btn-group dropup">
<button type="button" class="btn btn-primary">Button</button>
<button type="button" class="btn btn-primary">Another Button</button>
<div class="btn-group">
<button type="button" data-toggle="dropdown" class="btn btn-primary dropdown-
toggle">Dropdown <span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
</div>
— The output of the above example will look something like this:
131
By default, the top-left corner of the dropdown menu is positioned at the bottom-left
corner of its parent element i.e. 100% from the top and along the left side. To right align
the dropdown menu just add an extra class .dropdown-menu-right to the .dropdown-
menu base class.
Example
Try this code »
<div class="btn-group">
<button type="button" data-toggle="dropdown" class="btn btn-primary dropdown-toggle">Dropdown
<span class="caret"></span></button>
<ul class="dropdown-menu dropdown-menu-right">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
Example
Try this code »
<div class="btn-group">
<button type="button" data-toggle="dropdown" class="btn btn-default dropdown-toggle">Products
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li class="dropdown-header">ELECTRONICS</li>
<li><a href="#">Mobiles</a></li>
<li><a href="#">Tablets</a></li>
<li><a href="#">Laptops</a></li>
<li class="dropdown-header">FASHION</li>
<li><a href="#">Clothing</a></li>
<li><a href="#">Sunglasses</a></li>
</ul>
</div>
132
Disable Items within a Dropdown
You can apply the class .disabled on a list element to make the menu item look like
disabled. However, the link is still clickable, to disable this you can typically remove the
anchor's hrefattribute either using the JavaScript or manually.
Example
Try this code »
1. <div class="btn-group">
2. <button type="button" data-toggle="dropdown" class="btn btn-default dropdown-toggle">Report
<span class="caret"></span></button>
3. <ul class="dropdown-menu">
4. <li><a href="#">View</a></li>
5. <li><a href="#">Download</a></li>
6. <li class="disabled"><a href="#">Edit / Delete</a></li>
7. </ul>
8. </div>
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".dropdown-toggle").dropdown();
4. });
5. </script>
Note:The data-toggle="dropdown" is still required for the dropdown's trigger element
regardless of whether you call the dropdown via JavaScript or data-api.
Options
None
133
Methods
This is the standard bootstrap's dropdown method:
$().dropdown('toggle')
A programmatic api for toggling menus for a given navbar or tabbed navigation.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".dropdown-toggle").dropdown('toggle');
4. });
5. </script>
Events
These are the standard Bootstrap events to enhance the dropdown functionality. All
dropdown events are fired at the .dropdown-menu's parent element.
Event Description
show.bs.dropdown This event fires immediately when the show instance method is called. You can
use the event.relatedTarget to target the toggling anchor element.
shown.bs.dropdown This event is fired when the dropdown has been made visible to the user. It will
wait for CSS transitions, to complete. You can use the event.relatedTarget to
target the toggling anchor element.
hide.bs.dropdown This event is fired immediately when the hide instance method has been called.
You can use the event.relatedTarget to target the toggling anchor element.
hidden.bs.dropdow This event is fired when the dropdown has finished being hidden from the user. It
n will wait for CSS transitions, to complete. You can use
the event.relatedTarget to target the toggling anchor element.
The following example displays the text content of dropdown link when the users click
on it.
Example
Try this code »
1. <script type="text/javascript">
134
2. $(document).ready(function(){
3. $(".dropdown").on("show.bs.dropdown", function(e){
4. var linkText = $(e.relatedTarget).text(); // Get the link text
5. alert("Click on OK button to view the dropdown menu for - " + linkText);
6. });
7. });
8. </script>
Example
Try this code »
1. <ul class="nav nav-tabs">
2. <li class="active"><a href="#">Home</a></li>
3. <li><a href="#">Profile</a></li>
4. <li><a href="#">Messages</a></li>
5. </ul>
— The output of the above example will look something like this:
Example
Try this code »
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#sectionA">Section A</a></li>
<li><a data-toggle="tab" href="#sectionB">Section B</a></li>
<li class="dropdown">
135
<a data-toggle="dropdown" class="dropdown-toggle" href="#">Dropdown <b
class="caret"></b></a>
<ul class="dropdown-menu">
<li><a data-toggle="tab" href="#dropdown1">Dropdown 1</a></li>
<li><a data-toggle="tab" href="#dropdown2">Dropdown 2</a></li>
</ul>
</li>
</ul>
<div class="tab-content">
<div id="sectionA" class="tab-pane fade in active">
<p>Section A content…</p>
</div>
<div id="sectionB" class="tab-pane fade">
<p>Section B content…</p>
</div>
<div id="dropdown1" class="tab-pane fade">
<p>Dropdown 1 content…</p>
</div>
<div id="dropdown2" class="tab-pane fade">
<p>Dropdown 2 content…</p>
</div>
</div>
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myTab a").click(function(e){
4. e.preventDefault();
5. $(this).tab('show');
6. });
7. });
8. </script>
You can activate individual tabs in several ways:
Example
Try this code »
1. $('#myTab a[href="#profile"]').tab('show'); // show tab targeted by the selector
2. $("#myTab a:first").tab('show'); // show first tab
3. $("#myTab a:last").tab('show'); // show last tab
4. $("#myTab li:eq(2) a").tab('show'); // show third tab (0-indexed, like an array)
136
Methods
This is the standard bootstrap's tab method:
$().tab('show')
Activates a tab element and the related content container. Tab should have either a
data-target or an href for targeting a container node in the DOM.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myTab li:eq(1) a").tab('show');
4. })
5. </script>
Events
These are the standard Bootstrap events to enhance the tab functionality.
Event Description
show.bs.tab This event fires on tab show, but before the new tab has been shown. You can use
the event.target and event.relatedTarget to target the active tab and the previous active
tab (if available) respectively.
shown.bs.tab This event fires on tab show after a tab has been shown. You can use
the event.target and event.relatedTarget to target the active tab and the previous active
tab (if available) respectively.
hide.bs.tab This event fires when the current active tab is to be hidden and thus a new tab is to be
shown. You can use the event.target and event.relatedTarget to target the current
active tab and the new tab which is going to be active very soon, respectively.
hidden.bs.ta This event fires after the previous active tab is hidden and a new tab is shown. You can
b use the event.target and event.relatedTarget to target the previous active tab and the
new active tab, respectively.
The following example displays the names of active tab and previous tab to the user
when transition of a tab has been fully completed.
Example
137
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $('a[data-toggle="tab"]').on('shown', function(e){
4. e.target // active tab
5. e.relatedTarget // previous tab
6. })
7. });
8. </script>
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $('[data-toggle="tooltip"]').tooltip();
4. });
5. </script>
— The output of the above example will look something like this:
138
Setting the Directions of Tooltips
You can set tooltips to appear on top, right, bottom and left sides of an element.
Positioning of Tooltips via Data Attributes
The following example will show you how to set the direction of tooltips via data
attributes.
Example
Try this code »
1. <a href="#" data-toggle="tooltip" data-placement="top" title="Default tooltip">Tooltip</a>
2. <a href="#" data-toggle="tooltip" data-placement="right" title="Another tooltip">Another tooltip</a>
3. <a href="#" data-toggle="tooltip" data-placement="bottom" title="A large tooltip.">Large tooltip</a>
4. <a href="#" data-toggle="tooltip" data-placement="left" title="The last tip!">Last tooltip</a>
Positioning of Tooltips via JavaScript
The following example will show you how to set the direction of tooltips via JavaScript.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".tip-top").tooltip({placement : 'top'});
4. $(".tip-right").tooltip({placement : 'right'});
5. $(".tip-bottom").tooltip({placement : 'bottom'});
6. $(".tip-left").tooltip({ placement : 'left'});
7. });
8. </script>
Options
There are certain options which may be passed to tooltip() Bootstrap method to
customize the functionality of the tooltip plugin.
Name Type Default Value Description
139
Name Type Default Value Description
140
Name Type Default Value Description
You may set these options either through the use of data attributes or JavaScript. For
setting the tooltips options via data attributes, just append the option name
to data- along with the correct value, like data-animation="false", data-
placement="bottom" etc.
However, JavaScript is the more preferable way for setting these options as it prevents
you from repetitive work. See the tooltip's method $().tooltip(options) in the section below
to know how to set the options for tooltips using the JavaScript.
Methods
These are the standard Bootstrap's tooltip methods:
$().tooltip(options)
This method attaches the tooltip handler to a group of elements. It also allows you to
set the options for the tooltips, so that you can customize them according to your
needs.
The following example will insert the specified text inside the tooltips if the value of
the titleattribute is omitted or missing from the selected elements:
Example
Try this code »
141
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myTooltips a").tooltip({
4. title: 'It works in absence of title attribute.'
5. });
6. });
7. </script>
The following example will show you how to place the HTML content inside a tooltip:
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myTooltip").tooltip({
4. title: "<h4><img src='images/smiley.png' alt='Smiley'> Hello, <b>I'm</b> <i>Smiley!</i></h4>",
5. html: true
6. });
7. });
8. </script>
The following example will show you how to control the timing of showing and hiding
of the tooltip using the tooltip's delay option via JavaScript.
Example
Try this code »
<script type="text/javascript">
$(document).ready(function(){
// Showing and hiding tooltip with same speed
$(".tooltip-tiny").tooltip({
delay: 100
});
// Showing and hiding tooltip with different speed
$(".tooltip-large").tooltip({
delay: {show: 0, hide: 500}
});
});
</script>
The following example will show you how you can create your own custom template for
the Bootstrap tooltips instead of using the default one.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myTooltips a").tooltip({
4. template: '<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-
head"><h3><span class="glyphicon glyphicon-info-sign"></span> Tool Info</h3></div><div class="tooltip-
inner"></div></div>'
142
5. });
6. });
7. </script>
The following example will insert the dynamically generated HTML code of the tooltip at
the end of a .wrapper element instead of the <body> element.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. // Append tooltip HTML to wrapper element
4. $("#myTooltips a").tooltip({container: ".wrapper"});
5. });
6. </script>
Note:Overriding the tooltip's default container option value does not produce any visible
difference on the page. To see the actual result you need inspect the resulting DOM using the
Firebug or Developer tools.
Similarly, you can set other options for the tooltips using the $
().tooltip(options) method. Let's check out the other methods of the Bootstrap tooltip
plugin.
.tooltip('show')
This method reveals an element's tooltip.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".show-tooltip").click(function(){
4. $("#myTooltip").tooltip('show');
5. });
6. });
7. </script>
.tooltip('hide')
This method hides an element's tooltip.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".hide-tooltip").click(function(){
4. $("#myTooltip").tooltip('hide');
5. });
6. });
7. </script>
143
.tooltip('toggle')
This method toggles an element's tooltip.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".toggle-tooltip").click(function(){
4. $("#myTooltip").tooltip('toggle');
5. });
6. });
7. </script>
.tooltip('destroy')
This method hides and destroys an element's tooltip.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".destroy-tooltip").click(function(){
4. $("#myTooltip").tooltip('destroy');
5. });
6. });
7. </script>
Events
Bootstrap's tooltip class includes few events for hooking into tooltip functionality.
Event Description
show.bs.tooltip This event fires immediately when the show instance method is called.
shown.bs.tooltip This event is fired when the tooltip has been made visible to the user. It will wait until
the CSS transition process has been fully completed before getting fired.
hide.bs.tooltip This event is fired immediately when the hide instance method has been called.
hidden.bs.tooltip This event is fired when the tooltip has finished being hidden from the user. It will
wait until the CSS transition process has been fully completed before getting fired.
inserted.bs.toolti This event is fired after the show.bs.tooltip event when the tooltip template has
p been added to the DOM.
The following example will display an alert message to the user when the fade out
transition of the tooltip has been fully completed.
144
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $('[data-toggle="tooltip"]').on('hidden.bs.tooltip', function(){
4. alert("Tooltip has been completely closed.");
5. });
6. });
7. </script>
Similarly, you can add popovers to the other elements such as links, icons, etc.
Note:For performance reasons the popovers data-apis are opt in like tooltips, means to use
popovers you must initialize them yourself with popover() method.
Step 2: Triggering the Popovers
Popovers can be triggered via JavaScript — just call the popover() Bootstrap method
with the id, class or any CSS selector of the required element in your JavaScript code.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $('[data-toggle="popover"]').popover();
4. });
5. </script>
— The output of the above example will look something like this:
145
Setting the Directions of Popovers
You can set popovers to appear on top, right, bottom and left sides of an element.
Positioning of Popovers via Data Attributes
The following example will show you how to set the direction of popovers via data
attributes.
Example
Try this code »
1. <button type="button" class="btn btn-primary" data-toggle="popover" data-placement="top" title="Popover
title" data-content="Default popover">Popover on top</button>
2. <button type="button" class="btn btn-success" data-toggle="popover" data-placement="right" title="Popover
title" data-content="Popover on right.">Popover on right</button>
3. <button type="button" class="btn btn-info" data-toggle="popover" data-placement="bottom" title="Popover
title" data-content="Popover on bottom.">Popover on bottom</button>
4. <button type="button" class="btn btn-warning" data-toggle="popover" data-placement="left" title="Popover
title" data-content="Popover on left.">Popover on left</button>
Positioning of Popovers via JavaScript
The following example will show you how to set the direction of popovers via JavaScript.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".pop-top").popover({placement : 'top'});
4. $(".pop-right").popover({placement : 'right'});
5. $(".pop-bottom").popover({placement : 'bottom'});
6. $(".pop-left").popover({ placement : 'left'});
7. });
8. </script>
146
Hiding the Popovers on Next Click
By default popovers are not hiding until you click the trigger element once again. You
can use the focus trigger to hide the popovers when the user makes the next click.
Example
Try this code »
1. <a href="#" class="btn btn-primary btn-lg" data-toggle="popover" tabindex="0" data-trigger="focus"
title="Popover title" data-content="Here's some amazing content.">Dismissible popover</a>
Note:To make this feature work properly across the browsers, you must use the <a> tag, not
the <button> tag, and you also must include a tabindex attribute.
Options
There are certain options which may be passed to popover() Bootstrap method to
customize the functionality of the tooltip plugin.
Name Type Default Value Description
147
Name Type Default Value Description
148
Name Type Default Value Description
You may set these options either through the use of data attributes or JavaScript. For
setting the popovers options via data attributes, just append the option name
to data- along with the correct value, like data-animation="false", data-
placement="bottom" etc.
However, JavaScript is the more preferable way for setting these options as it saves you
from doing the repetitive work. See the popover's method $().popover(options) in the
section below to know how to set the options for popovers using the JavaScript.
Methods
These are the standard Bootstrap's popover methods:
$().popover(options)
This method attaches the popover handler to a group of elements. It also allows you to
set the options for the popovers so that you can customize them according to your
needs.
The following example will insert the specified text inside the popovers title if the value
of the title attribute is omitted or missing from the selected elements:
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myPopovers a").popover({
4. title: 'Default title value'
5. });
6. });
7. </script>
The following jQuery code will trigger the popovers on mouse hover instead of click:
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
149
3. $('[data-toggle="popover"]').popover({
4. trigger: 'hover'
5. });
6. });
7. </script>
The following example will show you how to place the HTML content inside a popover:
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myPopover").popover({
4. title: '<h3 class="custom-title"><span class="glyphicon glyphicon-info-sign"></span> Popover
Info</h3>',
5. content: '<p>This is a <em>simple example</em> demonstrating how to insert HTML code inside
<mark><strong>Bootstrap popover</strong></mark>.</p>',
6. html: true
7. });
8. });
9. </script>
The following example will show you how to control the timing of showing and hiding
of the popover using the popover's delay option via JavaScript.
Example
Try this code »
<script type="text/javascript">
$(document).ready(function(){
// Showing and hiding popover with same speed
$(".popover-tiny").tooltip({
delay: 500
});
// Showing and hiding popover with different speed
$(".popover-large").tooltip({
delay: {show: 0, hide: 2000}
});
});
</script>
The following example will show you how you can create your own custom template for
the Bootstrap popovers instead of using the default one.
Example
Try this code »
<script type="text/javascript">
$(document).ready(function(){
$('[data-toggle="popover"]').popover({
html: true,
150
template: '<div class="popover"><div class="arrow"></div><h3 class="popover-title"></h3><div
class="popover-content"></div><div class="popover-footer"><a href="#" class="btn btn-info btn-
sm">Close</a></div></div>'
});
// Custom jQuery to hide popover on click of the close button
$(document).on("click", ".popover-footer .btn" , function(){
$(this).parents(".popover").popover('hide');
});
});
</script>
The following example will insert the dynamically generated HTML code of the popover
at the end of a .wrapper element instead of the <body> element.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. // Append popover HTML to wrapper element
4. $("#myPopovers a").popover({container: '.wrapper'});
5. });
6. </script>
Note:Overriding the popover's default container option value does not produce any visible
difference on the page. To see the actual result you need inspect the resulting DOM using the
Firebug or Developer tools.
Similarly, you can set other options for the popovers using the $
().popover(options) method. Let's check out the other methods of the Bootstrap
popover plugin.
.popover('show')
This method reveals an element's popover.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".show-popover").click(function(){
4. $("#myPopover").popover('show');
5. });
6. });
7. </script>
.popover('hide')
This method hides an element's popover.
Example
151
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".hide-popover").click(function(){
4. $("#myPopover").popover('hide');
5. });
6. });
7. </script>
.popover('toggle')
This method toggles an element's popover.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".toggle-popover").click(function(){
4. $("#myPopover").popover('toggle');
5. });
6. });
7. </script>
.popover('destroy')
This method hides and destroys an element's popover.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".destroy-popover").click(function(){
4. $("#myPopover").popover('destroy');
5. });
6. });
7. </script>
Events
Bootstrap's popover class includes few events for hooking into popover functionality.
Event Description
show.bs.popover This event fires immediately when the show instance method is called.
shown.bs.popover This event is fired when the popover has been made visible to the user. It will wait
until the CSS transition process has been fully completed before getting fired.
hide.bs.popover This event is fired immediately when the hide instance method has been called.
152
Event Description
hidden.bs.popover This event is fired when the popover has finished being hidden from the user. It will
wait until the CSS transition process has been fully completed before getting fired.
inserted.bs.popove This event is fired after the show.bs.popover event when the popover template has
r been added to the DOM.
The following example displays an alert message to the user when fade out transition of
the popover has been fully completed.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $('[data-toggle="popover"]').on('hidden.bs.popover', function(){
4. alert("Popover has been completely closed.");
5. });
6. });
7. </script>
Warning Alerts
You can create a simple Bootstrap warning alert message box by adding the contextual
class .alert-warning to the .alert base class, like this:
Example
Try this code »
1. <div class="alert alert-warning">
2. <a href="#" class="close" data-dismiss="alert">×</a>
3. <strong>Warning!</strong> There was a problem with your network connection.
4. </div>
— The output of the above example will look something like this:
Tip:If you want to enable the fading transition effect while closing the alert boxes, apply the
classes .fade and .in to them along with the contextual class.
153
Example
Try this code »
1. <div class="alert alert-warning fade in">
2. <a href="#" class="close" data-dismiss="alert">×</a>
3. <strong>Warning!</strong> There was a problem with your network connection.
4. </div>
Similarly you can create other variant of the alert messages, as follow:
Example
Try this code »
1. <div class="alert alert-danger fade in">
2. <a href="#" class="close" data-dismiss="alert">×</a>
3. <strong>Error!</strong> A problem has been occurred while submitting your data.
4. </div>
— The output of the above example will look something like this:
Example
Try this code »
1. <div class="alert alert-success fade in">
2. <a href="#" class="close" data-dismiss="alert">×</a>
3. <strong>Success!</strong> Your message has been sent successfully.
4. </div>
— The output of the above example will look something like this:
154
Information Alerts
For information alert messages add the class .alert-info to the .alert base class.
Example
Try this code »
1. <div class="alert alert-info fade in">
2. <a href="#" class="close" data-dismiss="alert">×</a>
3. <strong>Note!</strong> Please read the comments carefully.
4. </div>
— The output of the above example will look something like this:
Example
Try this code »
<div class="alert alert-warning fade in">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>Warning!</strong> There was a problem with your <a href="#" class="alert-
link">network connection</a>.
</div>
<div class="alert alert-danger fade in">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>Error!</strong> A <a href="#" class="alert-link">problem</a> has been occurred
while submitting your data.
</div>
<div class="alert alert-success fade in">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>Success!</strong> Your <a href="#" class="alert-link">message</a> has been sent
successfully.
</div>
<div class="alert alert-info fade in">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>Note!</strong> Please read the <a href="#" class="alert-link">comments</a>
carefully.
</div>
— The output of the above example will look something like this:
155
Closing Alerts via Data Attribute
Data attributes provides an easy way to add close functionality to the alert messages
box. Just add the data-dismiss="alert" to the close button and it will automatically
enable the dismissal of the containing alert message box.
Example
Try this code »
1. <a href="#" class="close" data-dismiss="alert">×</a><br>
2. <button type="button" class="close" data-dismiss="alert">×</button>
3. <span class="close" data-dismiss="alert">×</span>
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".close").click(function(){
4. $("#myAlert").alert();
5. });
6. });
156
7. </script>
Methods
These are the standard bootstrap's alerts methods:
$().alert()
This method wraps all alerts with close functionality.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".close").click(function(){
4. $(".alert").alert();
5. });
6. });
7. </script>
$().alert('close')
This method closes an alert message box.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".close").click(function(){
4. $("#myAlert").alert('close');
5. });
6. });
7. </script>
Events
Bootstrap's alert class includes few events for hooking into alert functionality.
Event Description
close.bs.alert This event fires immediately when the close instance method is called.
closed.bs.aler This event is fired when the alert message box has been closed. It will wait until the CSS
157
Event Description
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myAlert").on('closed.bs.alert', function () {
4. alert("Alert message box has been closed.");
5. });
6. });
7. </script>
Example
Try this code »
1. <button type="button" class="btn btn-primary" data-toggle="button">Single Toggle Button</button>
— The toggle button upon click will look something like this:
Example
Try this code »
158
1. <button type="button" class="btn btn-primary" data-loading-text="Loading...">Loading state</button>
— The loading button upon click will look something like this:
Note:Mozilla firefox persists the disabled state across page loads, to prevent this behavior, you
may simply set autocomplete="off" on the form containing the buttons, or directly to the input
or button element.
Example
Try this code »
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary">
<input type="checkbox" name="options"> Option 1
</label>
<label class="btn btn-primary">
<input type="checkbox" name="options"> Option 2
</label>
<label class="btn btn-primary">
<input type="checkbox" name="options"> Option 3
</label>
</div>
— The output of the above example will look something like this:
Add the class .active on input's label element if you want options pre-checked by
default.
Example
Try this code »
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="checkbox" name="options"> Option 1
</label>
<label class="btn btn-primary">
<input type="checkbox" name="options"> Option 2
</label>
<label class="btn btn-primary active">
159
<input type="checkbox" name="options"> Option 3
</label>
</div>
— The output of the above example will look something like this:
Note:The .active class only changes the visual appearance of buttons radio and checkboxes to
make them look like selected. To actually preselect them you need to apply the checked attribute
on the input element yourself.
Example
Try this code »
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary">
<input type="radio" name="options"> Option 1
</label>
<label class="btn btn-primary">
<input type="radio" name="options"> Option 2
</label>
<label class="btn btn-primary">
<input type="radio" name="options"> Option 3
</label>
</div>
— The output of the above example will look something like this:
Add the class .active on input's label element if you want an option pre-selected by
default.
Example
Try this code »
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" name="options"> Option 1
</label>
<label class="btn btn-primary">
<input type="radio" name="options"> Option 2
160
</label>
<label class="btn btn-primary">
<input type="radio" name="options"> Option 3
</label>
</div>
— The output of the above example will look something like this:
Example
Try this code »
<script type="text/javascript">
$(document).ready(function(){
$(".nav-tabs a").click(function(){
$(this).button('loading').delay(500).queue(function(){
$(this).button('reset');
$(this).dequeue();
});
});
});
</script>
Options
None
Methods
These are the standard bootstrap's buttons methods:
$().button('toggle')
161
This method toggles push state of the button. It changes the appearance of the button,
and makes it look like that it has been activated. You can also enable auto toggling of a
button by using the data-toggle attribute.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myButton").click(function(){
4. $(this).button('toggle');
5. });
6. });
7. </script>
$().button('loading')
This method sets the button state to loading. When loading, the button is disabled and
the text is swapped with the value of data-loading-text attribute of button element.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myButton").click(function(){
4. $(this).button('loading');
5. });
6. });
7. </script>
Note:The use of data-loading-text and $().button('loading') has been deprecated since
Bootstrap v3.3.6 and will be removed from v4. You should better use $
().button(string) method to ensure future compatibility.
$().button('reset')
This method resets button state by swapping text to original text.
Example
Try this code »
<script type="text/javascript">
$(document).ready(function(){
$("#myButton").click(function(){
$(this).button('loading').delay(1000).queue(function(){
$(this).button('reset');
$(this).dequeue();
});
});
});
</script>
$().button(string)
162
This method resets button state by swapping text to any data defined text state.
Example
Try this code »
<script type="text/javascript">
$(document).ready(function(){
$("#myButton").click(function(){
$(this).button('loading').delay(1000).queue(function() {
$(this).button('complete');
$(this).dequeue();
});
});
});
</script>
Example
Try this code »
<div id="accordion" class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">1. What is
HTML?</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse">
<div class="panel-body">
<p>HTML stands for HyperText Markup Language. HTML is the standard
markup language for describing the structure of web pages. <a
href="https://www.tutorialrepublic.com/html-tutorial/" target="_blank">Learn more.</a></p>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">2. What
is Bootstrap?</a>
</h4>
163
</div>
<div id="collapseTwo" class="panel-collapse collapse in">
<div class="panel-body">
<p>Bootstrap is a sleek, intuitive, and powerful front-end
framework for faster and easier web development. It is a collection of CSS and HTML
conventions. <a href="https://www.tutorialrepublic.com/twitter-bootstrap-tutorial/" target="_blank">Learn
more.</a></p>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseThree">3. What
is CSS?</a>
</h4>
</div>
<div id="collapseThree" class="panel-collapse collapse">
<div class="panel-body">
<p>CSS stands for Cascading Style Sheet. CSS allows you to specify
various style properties for a given HTML element such as colors, backgrounds, fonts etc.
<a href="https://www.tutorialrepublic.com/css-tutorial/" target="_blank">Learn more.</a></p>
</div>
</div>
</div>
</div>
— The output of the above example will look something like this:
164
Example
Try this code »
<script type="text/javascript">
$(document).ready(function(){
// Add minus icon for collapse element which is open by default
$(".collapse.in").each(function(){
$(this).siblings(".panel-heading").find(".glyphicon").addClass("glyphicon-
minus").removeClass("glyphicon-plus");
});
// Toggle plus minus icon on show hide of collapse element
$(".collapse").on('show.bs.collapse', function(){
$(this).parent().find(".glyphicon").removeClass("glyphicon-plus").addClass("glyphicon-
minus");
}).on('hide.bs.collapse', function(){
$(this).parent().find(".glyphicon").removeClass("glyphicon-minus").addClass("glyphicon-
plus");
});
});
</script>
— The output of the above example will look something like this:
Similarly, you can add arrow icon to a accordion utilizing the jQuery and CSS transition
effect.
The show.bs.collapse and hide.bs.collapse in the example above are collapse events,
you'll learn about the events little later in this chapter.
165
Example
Try this code »
1. <!-- Trigger Button HTML -->
2. <input type="button" class="btn" data-toggle="collapse" data-target="#toggleDemo" value="Toggle
Button">
3.
4. <!-- Collapsible Element HTML -->
5. <div id="toggleDemo" class="collapse in"><p>This is a simple example of expanding and
collapsing individual element via data attribute. Click on the <b>Toggle Button</b> button
to see the effect.</p></div>
We've just created a collapsible control without writing any JavaScript code. Well, let's
go through each part of this code one by one for a better understanding.
Explanation of Code
The Bootstrap collapse plugin basically requires the two elements to work properly —
the controller element such as a button or hyperlink by clicking on which you want to
collapse the other element, and the collapsible element itself.
The data-toggle="collapse" attribute (line no-2) is added to the controller element
along with a attribute data-target (for buttons) or href (for anchors) to automatically assign the
control of a collapsible element.
The data-target or href attribute accepts a CSS selector to apply the collapse to a
specific element. Be sure to add the class .collapse to the collapsible element.
You can optionally add the class .in (line no-4) to the collapsible element in addition to
the class .collapse to make it open by default.
To make the collapsible controls to work in group like accordion menu, you can utilize
the Bootstrap panel component as demonstrated in the previous example.
Example
Try this code »
1. <!-- Trigger Button HTML -->
2. <input type="button" class="btn" value="Toggle Button">
3.
4. <!-- Collapsible Element HTML -->
166
5. <div id="toggleDemo" class="collapse in"><p>This is a simple example of expanding and
collapsing individual element via JavaScript. Click on the <b>Simple Collapsible</b>
button to see the effect.</p></div>
Options
There are certain options which may be passed to collapse() Bootstrap method to
customize the functionality of a collapsible element.
Name Type Default Value Description
paren selector false All other collapsible elements under the specified parent will be closed
t while this collapsible item is shown on invocation.
toggl boolea true Toggles the collapsible element on invocation.
e n
You can also set these options using the data attributes on accordion — just append the
option name to data-, like data-parent="#myAccordion", data-toggle="false" etc. as
demonstrated in the basic implementation.
Methods
These are the standard bootstrap's collapse methods:
.collapse(options)
This method activates your content as a collapsible element.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".btn").click(function(){
4. $("#myCollapsible").collapse({
5. toggle: false
6. });
7. });
8. });
9. </script>
.collapse('toggle')
This method toggles (show or hide) a collapsible element.
167
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".toggle-btn").click(function(){
4. $("#myCollapsible").collapse('toggle');
5. });
6. });
7. </script>
.collapse('show')
This method shows a collapsible element.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".show-btn").click(function(){
4. $("#myCollapsible").collapse('show');
5. });
6. });
7. </script>
.collapse('hide')
This method hides a collapsible element.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".hide-btn").click(function(){
4. $("#myCollapsible").collapse('hide');
5. });
6. });
7. </script>
Events
Bootstrap's collapse class includes few events for hooking into collapse functionality.
Event Description
show.bs.collapse This event fires immediately when the show instance method is called.
shown.bs.collapse This event is fired when a collapse element has been made visible to the user. It will
168
Event Description
wait until the CSS transition process has been fully completed before getting fired.
hide.bs.collapse This event is fired immediately when the hide method has been called.
hidden.bs.collaps This event is fired when a collapse element has been hidden from the user. It will
e wait until the CSS transition process has been fully completed before getting fired.
The following example displays an alert message to the user when sliding transition of a
collapsible element has been fully completed.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myCollapsible").on('hidden.bs.collapse', function(){
4. alert("Collapsible element has been completely closed.");
5. });
6. });
7. </script>
Example
Try this code »
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Carousel indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- Wrapper for carousel items -->
<div class="carousel-inner">
<div class="item active">
<img src="images/slide1.png" alt="First Slide">
</div>
<div class="item">
<img src="images/slide2.png" alt="Second Slide">
</div>
<div class="item">
169
<img src="images/slide3.png" alt="Third Slide">
</div>
</div>
<!-- Carousel controls -->
<a class="carousel-control left" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
— The output of the above example will look something like this:
Check out the snippets section for examples of some beautifully designed Bootstrap
carousels.
You can also add captions such as heading or description text to the individual slides of
the carousel, please check out the next example.
Example
Try this code »
<div id="myCarousel" class="carousel slide" data-interval="3000" data-ride="carousel">
<!-- Carousel indicators -->
<ol class="carousel-indicators">
170
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- Carousel items -->
<div class="carousel-inner">
<div class="item active">
<img src="images/slide1.png" alt="First Slide">
<div class="carousel-caption">
<h3>First slide label</h3>
<p>Lorem ipsum dolor sit amet...</p>
</div>
</div>
<div class="item">
<img src="images/slide2.png" alt="Second Slide">
<div class="carousel-caption">
<h3>Second slide label</h3>
<p>Aliquam sit amet gravida nibh, facilisis...</p>
</div>
</div>
<div class="item">
<img src="images/slide3.png" alt="Third Slide">
<div class="carousel-caption">
<h3>Third slide label</h3>
<p>Praesent commodo cursus magna vel...</p>
</div>
</div>
</div>
<!-- Carousel nav -->
<a class="carousel-control left" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
You might be wondering what this code was all about. Ok, let's go through each part of
this carousel code one by one for a better understanding.
Explanation of Code
The Bootstrap carousel has basically three components — carousel indicators (small
circles), carousel controls (previous and next arrows) and the carousel items or slides.
The outermost container of every carousel requires a unique id (in our
case id="myCarousel") so that it can be targeted by the carousel indicators (line no-4,5,6) and
carousel controls (line no-33,36) to function properly.
The data-ride="carousel" attribute of the .carousel element tells the Bootstrap to start
animating the carousel immediately when the page load. Whereas the data-intervalattribute
specifies the time delay between two slides.
171
The .data-slide-to attribute (line no-4,5,6) move the slide position to a particular item
(index beginning with 0) when clicking on the specific carousel indicator.
The slides are specified within the .carousel-inner (line no-9) and the content of each
slide is defined within the .item element that can be text and images.
The data-slide attribute on carousel controls (line no-33,36) accepts the
keywords prevor next, which alters the slide position relative to its current position.
Rest of the thing is self explanatory, such as the .carousel element specifies the
Bootstrap carousel, the .carousel-indicators element indicates how many slides are
there in the carousel and which slide is currently active, the .carousel-caption element
used within the .itemelement defines the caption for that slide etc.
Tip:It is required to add the class .active to one of the slides (i.e. on the .itemelement).
Otherwise, the carousel will not be visible.
Note:The .slide class adds CSS slide transition animation to the carousel that makes the items
slide when showing the new item. But it doesn't work in Internet Explorer 8 & 9 due to lack of
support of the necessary CSS3 properties.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myCarousel").carousel();
4. });
5. </script>
Options
There are certain options which can be passed to carousel() Bootstrap method to
customize the functionality of a carousel. Options can be passed via data attributes or
JavaScript.
172
For setting the modals options via data attributes, just append the option name
to data-, like data-interval="3000", data-pause="hover", etc.
Name Type Default Valu Description
e
interval number 5000 Specifies the amount of time to delay (in milliseconds) between one
slide to another in automatic cycling. If false, carousel will not
automatically cycle.
pause string "hover" Pauses the cycling of the carousel when mouse pointer enters the
null carousel and resumes the cycling when mouse pointer leaves the
carousel, by default. If set to null, hovering over the carousel won't
pause it.
wrap boolean true Specifies whether the carousel should cycle continuously or have
hard stops (i.e stop at the last slide).
keyboar boolean true Specifies whether the carousel should react to keyboard events. By
d default it is true that means if carousel has focus you can go to its
previous and next slide using the left and right arrow keys on the
keyboard.
Data attributes provides an easy way for setting the carousel options, however
JavaScript is the more preferable way as it prevents you from repetitive work. See
the .carousel(options) method in the section below to know how to set the options for
carousels using JavaScript.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myCarousel").carousel({
4. interval : false
5. });
6. });
7. </script>
173
Methods
These are the standard bootstrap's carousels methods:
.carousel(options)
This method initializes the carousel with optional options and starts cycling through
items.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myCarousel").carousel({
4. interval : 3000
5. });
6. });
7. </script>
.carousel('cycle')
This method start carousel for cycling through the items from left to right.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".start-slide").click(function(){
4. $("#myCarousel").carousel('cycle');
5. });
6. });
7. </script>
.carousel('pause')
This method stops the carousel from cycling through items.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".pause-slide").click(function(){
4. $("#myCarousel").carousel('pause');
5. });
6. });
7. </script>
.carousel(number)
174
This method cycles the carousel to a particular frame (start with 0, similar to an array).
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".slide-three").click(function(){
4. $("#myCarousel").carousel(3);
5. });
6. });
7. </script>
.carousel('prev')
This method cycles the carousel to the previous item.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".prev-slide").click(function(){
4. $("#myCarousel").carousel('prev');
5. });
6. });
7. </script>
.carousel('next')
This method cycles the carousel to the next item.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $(".next-slide").click(function(){
4. $("#myCarousel").carousel('next');
5. });
6. });
7. </script>
Events
Bootstrap's carousel class includes few events for hooking into carousel functionality.
Event Description
slide.bs.carousel This event fires immediately when the slide instance method is called.
175
Event Description
slid.bs.carousel This event is fired when the carousel has completed its slide transition.
The following example displays an alert message to the user when sliding transition of a
carousel item has been fully completed.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $('#myCarousel').on('slid.bs.carousel', function () {
4. alert("The sliding transition of previous carousel item has been fully completed.");
5. });
6. });
7. </script>
Example
Try this code »
<script type="text/javascript">
$(document).ready(function(){
// Defining the local dataset
var cars = ['Audi', 'BMW', 'Bugatti', 'Ferrari', 'Ford', 'Lamborghini', 'Mercedes Benz',
'Porsche', 'Rolls-Royce', 'Volkswagen'];
// Constructing the suggestion engine
var cars = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
176
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: cars
});
// Initializing the typeahead
$('.typeahead').typeahead({
hint: true,
highlight: true, /* Enable substring highlighting */
minLength: 1 /* Specify minimum characters required for showing suggestions */
},
{
name: 'cars',
source: cars
});
});
</script>
— The output of the above example will look something like this:
Note:Bloodhound is the typeahead.js suggestion engine. It is very flexible and offers advanced
functionalities such as prefetching remote data, fast lookups through intelligent caching using the
browser local storage, etc.
Tip:Set autocomplete="off" for the input box if you want to prevent default browser menus
from appearing over the Bootstrap type-ahead dropdown.
Example
Try this code »
<script type="text/javascript">
$(document).ready(function(){
// Sonstructs the suggestion engine
var countries = new Bloodhound({
177
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
// The url points to a json file that contains an array of country names
prefetch: 'data/countries.json'
});
// Initializing the typeahead with remote dataset without highlighting
$('.typeahead').typeahead(null, {
name: 'countries',
source: countries,
limit: 10 /* Specify max number of suggestions to be displayed */
});
});
</script>
Example
Try this code »
<body data-spy="scroll" data-target="#myScrollspy">
<div class="container">
<div class="row">
<div class="col-sm-3" id="myScrollspy">
<ul class="nav nav-tabs nav-stacked" data-offset-top="120" data-spy="affix">
<li class="active"><a href="#section1">Section One</a></li>
<li><a href="#section2">Section Two</a></li>
<li><a href="#section3">Section Three</a></li>
</ul>
</div>
<div class="col-sm-9">
<div id="section1">
<h2>Section One</h2>
<p>This is section one content…</p>
</div>
<hr>
<div id="section2">
<h2>Section Two</h2>
<p>This is section two content…</p>
</div>
<hr>
<div id="section3">
<h2>Section Three</h2>
178
<p>This is section three content…</p>
</div>
</div>
</div>
</div>
</body>
Note:The Bootstrap scrollspy plugin requires the use of a Bootstrap nav component (e.g.
navbar, nav tabs or pills) for proper highlighting of active links.
Example
Try this code »
<body data-spy="scroll" data-target="#myNavbar" data-offset="70">
<nav id="myNavbar" class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-
target="#navbarCollapse">
<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="#">Scrollspy</a>
</div>
<!-- Collection of nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#section1">Section 1</a></li>
<li><a href="#section2">Section 2</a></li>
<li><a href="#section3">Section 3</a></li>
<li class="dropdown"><a href="#" class="dropdown-toggle" data-
toggle="dropdown">Section 4<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#section4dot1">Section 4.1</a></li>
<li><a href="#section4dot2">Section 4.2</a></li>
<li><a href="#section4dot3">Section 4.3</a></li>
</ul>
</li>
179
<li><a href="#section5">Section 5</a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div id="section1">
<h2>Section 1</h2>
<p>This is section 1 content…</p>
</div>
<hr>
<div id="section2">
<h2>Section 2</h2>
<p>This is section 2 content…</p>
</div>
<hr>
<div id="section3">
<h2>Section 3</h2>
<p>This is section 3 content…</p>
</div>
<hr>
<h2>Section 4</h2>
<p>This is section 4 content</p>
<div id="section4dot1">
<h3>Section 4.1</h3>
<p>This is section 4.1 content…</p>
</div>
<div id="section4dot2">
<h3>Section 4.2</h3>
<p>This is section 4.2 content…</p>
</div>
<div id="section4dot3">
<h3>Section 4.3</h3>
<p>This is section 4.3 content…</p>
</div>
<hr>
<div id="section5">
<h2>Section 5</h2>
<p>This is section 5 content…</p>
</div>
</div>
</body>
You might be thinking what this code was all about. Ok, let's go through each part of
this scrollspy code one by one for a better understanding.
Explanation of Code
The Bootstrap scrollspy has basically two components — the target nav (e.g. navbar, nav
tabs or pills) and the scrollable area to spy on, which is often the <body> section.
180
The data-spy="scroll" attribute (line no-01) is applied to the scrollable element that is
being spied on, which is the <body> element.
The data-target attribute is added on the scrollable element with the ID or class of the
parent element of the Bootstrap .nav component so that nav links can be targeted by the
scrollspy for highlighting purpose.
The optional data-offset attribute specifies the number of pixels to offset from top when
calculating the position of scroll. Adjust the offset value if the targeted links are highlighting too
early or too late. The default value is 10 pixels.
Rest of the thing is self explanatory, such as the .navbar element specifies a Bootstrap
navbar, the element <div id="section1"></div> (line no-33) create a bookmark with
the id attribute, whereas the element <a href="#section1">Section 1</a> (line no-17) add
a link to this bookmark, from within the same page, and so on.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("body").scrollspy({
4. target: "#myNavbar",
5. offset: 70
6. })
7. });
8. </script>
Options
There are certain options which may be passed to scrollspy() Bootstrap method to
customize the functionality of a scrollspy.
Name Type Default Value Description
offset number 10 Number of pixels to offset from top when calculating position of scroll.
181
You can also set this options for scrollspy using the data attributes — just append the
option name to data-, like data-offset="0".
Methods
These are the standard bootstrap's scrollspy methods:
.scrollspy('refresh')
When using scrollspy in conjunction with adding or removing of elements from the
DOM, you'll need to call the refresh method like this:
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $('[data-spy="scroll"]').each(function(){
4. var $spy = $(this).scrollspy('refresh');
5. });
6. });
7. </script>
Events
Bootstrap's scrollspy class includes few events for hooking into scrollspy functionality.
Event Description
activate.bs.scrollspy This event fires whenever a new item becomes activated by the scrollspy.
The following example displays an alert message to the user when a new item becomes
highlighted by the scrollspy.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myNavbar").on("activate.bs.scrollspy", function(){
4. var currentItem = $(".nav li.active > a").text();
5. $("#info").empty().html("Currently you are viewing - " + currentItem);
6. })
7. });
182
8. </script>
Example
Try this code »
1. <ul class="nav nav-tabs nav-stacked" data-spy="affix" data-offset-top="195">
2. <li class="active"><a href="#one">Section One</a></li>
3. <li><a href="#two">Section Two</a></li>
4. <li><a href="#three">Section Three</a></li>
5. </ul>
Note:The data-offset- attributes only specify how many pixels that you must scroll in order to
toggle the pinning of an element, it did not set the position of pinned element. You must define
the top or bottom CSS property for the pinned element specifically in your style sheet to set its
position in the viewport.
183
You may also enable the affix plugin manually using the JavaScript — just call
the affix()method with the id or class selector of the required element in your
JavaScript code.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myNav").affix({
4. offset: {
5. top: $(".header").outerHeight(true)
6. }
7. });
8. });
9. </script>
Options
There are certain options which may be passed to affix() Bootstrap method to
customize the functionality of the affix plugin.
Nam Type Default Value Description
e
offset number | 10 Specify the number of pixels to offset from screen when
function | calculating position of scroll. If a single number is provided, the
object offset will be applied in both top and bottom directions. To set
offset for a single direction, or multiple unique offsets — just
provide an object like offset: {top:50, bottom:100}
You can also use a function if you want to dynamically provide an
offset in case of responsive designs.
target selector | the windowobjec Specifies the target element of the affix.
node | t
jQuery
element
You can also set these options for affix using the data attributes — just append the
option name to data-, like data-offset-top="195".
184
Methods
These are the standard bootstrap's affix methods:
.affix(options)
This method activates your content as affixed content. Accepts an optional
options object, like offset: {top: 10} or offset: {top:50, bottom:100}.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myNav").affix({
4. offset: {
5. bottom: 195
6. }
7. });
8. });
9. </script>
Events
Bootstrap's affix class includes few events for hooking into modal functionality.
Event Description
affix.bs.affix This event fires immediately before the element has been affixed.
affixed.bs.affix This event is fired after the element has been affixed.
affix-top.bs.affix This event fires immediately before the element has been affixed to top.
affixed-top.bs.affix This event is fired after the element has been affixed to top.
affix-bottom.bs.affix This event fires immediately before the element has been affixed-bottom.
affixed- This event is fired after the element has been affixed to bottom.
bottom.bs.affix
The following example displays an alert message when navigation menu has been
affixed.
Example
Try this code »
1. <script type="text/javascript">
2. $(document).ready(function(){
3. $("#myNav").on('affixed.bs.affix', function(){
185
4. alert("The navigation menu has been affixed. Now it doesn't scroll with the page.");
5. });
6. });
7. </script>
186