Bootstrap Notes
Bootstrap Notes
P IYUSH M ISHRA
What is Bootstrap?
• Bootstrap is a free front-end framework
for faster and easier web development
• Bootstrap includes HTML and CSS based
design templates for typography, forms,
buttons, tables, navigation, modals, image
carousels and many other, as well as
optional JavaScript plugins
• Bootstrap also gives you the ability to
easily create responsive designs
Bootstrap History
Bootstrap was developed by Mark Otto and
Jacob Thornton at Twitter, and released as an
open-source product in August 2011 on
GitHub.
Why Use Bootstrap?
Advantages of Bootstrap:
You can:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
You should include the following <meta> element in all your web pages:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
This gives the browser instructions on how to control the page's dimensions and scaling.
The width=device-width part sets the width of the page to follow the screen-width of the device (which will vary
depending on the device).
The initial-scale=1.0 part sets the initial zoom level when the page is first loaded by the browser.
Here is an example of a web page without the viewport meta tag, and the same web page with the viewport meta
tag:
Tip: If you are browsing this page with a phone or a tablet, you can click on the two links below to see the
difference.
span span span span span span span span span span span s
1 1 1 1 1 1 1 1 1 1 1
span 4 span 8
span 6 span 6
span 12
Grid Classes
The Bootstrap grid system has four classes:
• xs (for phones)
• sm (for tablets)
• md (for desktops)
• lg (for larger desktops)
The classes above can be combined to create more dynamic and flexible
layouts.
<div class="row">
</div>
Striped Rows
The .table-striped class adds zebra-stripes to a table:
Bordered Table
The .table-bordered class adds borders on all sides of the table
and cells:
Hover Rows
The .table-hover class adds a hover effect (grey background
color) on table rows:
Condensed Table
The .table-condensed class makes a table more compact by
cutting cell padding in half:
Alerts
Bootstrap provides an easy way to create predefined alert messages:
Warning! This alert box indicates a warning that might need attention.
Alerts are created with the .alert class, followed by one of the four contextual
classes .alert-success, .alert-info, .alert-warning or .alert-danger:
Example
<div class="alert alert-success">
<strong>Success!</strong> Indicates a
successful or positive action.
</div>
<div class="alert alert-info">
<strong>Info!</strong> Indicates a neutral informative change or action.
</div>
Button Styles
Bootstrap provides seven styles of
buttons:
To achieve the button styles above,
Bootstrap has the following classes:
• .btn-default
• .btn-primary
• .btn-success
• .btn-info
• .btn-warning
• .btn-danger
• .btn-link
The following example shows the code for the different button styles:
Example
Button Sizes
Bootstrap provides four button sizes:
• .btn-lg
• .btn-md
• .btn-sm
• .btn-xs
Bootstrap Forms
Bootstrap's Default Settings
Form controls automatically receive some global
styling with Bootstrap:
<form>
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd">
</div>
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
<button type="submit" class="btnbtn-default">Submit</button>
</form>
When used together with the <label> element, the for attribute specifies
which form element a label is bound to.
</div>
</div>
Example Explained
The "Trigger" part:
The .modal class identifies the content of <div> as a modal and brings focus to
it.
The .fade class adds a transition effect which fades the modal in and out.
Remove this class if you do not want this effect.
The .modal-dialog class sets the proper width and margin of the modal.
modal-sm
The .modal-header class is used to define the style for the header of the
modal. The <button> inside the header has a data-dismiss="modal" attribute
which closes the modal if you click on it. The .close class styles the close
button, and the .modal-title class styles the header with a proper line-height.
The .modal-body class is used to define the style for the body of the modal.
Add any HTML markup here; paragraphs, images, videos, etc.
The .modal-footer class is used to define the style for the footer of the modal.
Note that this area is right aligned by default.
The following example adds a dropdown menu for the "Page 1" button:
In the following example we insert a "Sign Up" button and a "Login" button to
the right in the navigation bar. We also add a glyphicon on each of the two new
buttons:
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">WebSiteName</a>
</div>
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Page 1</a></li>
<li><a href="#">Page 2</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
<li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
</ul>
</div>
</nav>
Navbar Buttons
To add buttons inside the navbar, add the .navbar-btn class on a Bootstrap button:
Example
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">WebSiteName</a>
</div>
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>
<button class="btn btn-danger navbar-btn">Button</button>
</div>
</nav>
Navbar Forms
To add form elements inside the navbar, add the .navbar-form class to a form
element and add an input(s). Note that we have added a .form-group class to
the div container holding the input. This adds proper padding if you have more
than one inputs (you will learn more about this in the Forms chapter).
Example
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">WebSiteName</a>
</div>
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Page 1</a></li>
<li><a href="#">Page 2</a></li>
</ul>
<form class="navbar-form navbar-left" action="/action_page.php">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</nav>
The .navbar-fixed-top class makes the navigation bar fixed at the top:
We should hide the navigation bar; and only show it when it is needed.
In the following example the navigation bar is replaced by a button in the top
right corner. Only when the button is clicked, the navigation bar will be
displayed:
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">WebSiteName</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Page 1</a></li>
<li><a href="#">Page 2</a></li>
<li><a href="#">Page 3</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
<li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
</ul>
</div>
</div>
</nav>
<div class="item">
<img src="chicago.jpg" alt="Chicago">
</div>
<div class="item">
<img src="ny.jpg" alt="New York">
</div>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphiconglyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphiconglyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>
</div>
Example Explained
The outermost <div>:
Carousels require the use of an id (in this case id="myCarousel") for carousel
controls to function properly.
The .slide class adds a CSS transition and animation effect, which makes the
items slide when showing a new item. Omit this class if you do not want this
effect.
This code adds "left" and "right" buttons that allows the user to go back and
forth between the slides manually.
The data-slide attribute accepts the keywords "prev" or "next", which alters
the slide position relative to its current position.