Web Lecture5
Web Lecture5
Lecture 5
What is Bootstrap?
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.
• In June 2014 Bootstrap was the No.1 project on GitHub!
Why Use Bootstrap?
• Advantages of Bootstrap:
• Easy to use: Anybody with just basic knowledge of HTML and CSS can
start using Bootstrap
• Responsive features: Bootstrap's responsive CSS adjusts to phones,
tablets, and desktops
• Mobile-first approach: In Bootstrap 3, mobile-first styles are part of
the core framework
• Browser compatibility: Bootstrap is compatible with all modern
browsers (Chrome, Firefox, Internet Explorer, Edge, Safari, and Opera)
Bootstrap Versions
• There are two ways to start using Bootstrap on your own web site.
• You can:
• Download Bootstrap from getbootstrap.com
• Include Bootstrap from a CDN
• Downloading Bootstrap
If you want to download and host Bootstrap yourself, go to
getbootstrap.com, and follow the instructions there.
• Bootstrap CDN
If you don't want to download and host Bootstrap yourself, you can
include it from a CDN (Content Delivery Network).
MaxCDN provides CDN support for Bootstrap's CSS and JavaScript.
MaxCDN:
• jQuery
Bootstrap uses jQuery for JavaScript plugins (like modals, tooltips, etc).
However, if you just use the CSS part of Bootstrap, you don't need jQuery.
Create First Web Page With Bootstrap
• Bootstrap uses HTML elements and CSS properties that require the HTML5
doctype.
• Always include the HTML5 doctype at the beginning of the page, along with
the lang attribute and the correct character set:
• <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
</html>
2. Bootstrap 3 is mobile-first
Bootstrap 3 is designed to be responsive to mobile devices. Mobile-first styles are
part of the core framework.
<meta name="viewport" content="width=device-width, initial-scale=1">
The initial-scale=1 part sets the initial zoom level when the page is first loaded
by the browser.
3. Containers
Bootstrap also requires a containing element to wrap site contents.
<!DOCTYPE html>
<html lang="en">
<body>
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<div class="container-
<meta name="viewport" content="width=dev fluid">
ice-width, initial-scale=1">
<link rel="stylesheet" href="https://max <h1>My First Bootstrap
cdn.bootstrapcdn.com/bootstrap/3.4.1/css/ Page</h1>
bootstrap.min.css">
<script src="https:// <p>This is some text.</p>
ajax.googleapis.com/ajax/libs/jquery/ </div>
3.6.4/jquery.min.js"></script>
<script src="https://
maxcdn.bootstrapcdn.com/bootstrap/3.4.1/
js/bootstrap.min.js"></script>
</body>
</head> </html>
Bootstrap Grids
<div class="row">
<div class="col-sm-4">.col-sm-4</div>
<div class="col-sm-8">.col-sm-8</div>
</div>
<body>
<!DOCTYPE html>
<html lang="en"> <div class="container-fluid">
<head> <h1>Hello World!</h1>
<title>Bootstrap Example</title> <p>Resize the browser window to see the
effect.</p>
<meta charset="utf-8">
<p>The columns will automatically stack on top of
<meta name="viewport" each other when the screen is less than 768px
content="width=device-width, initial-scale=1"> wide.</p>
<link rel="stylesheet" <div class="row">
href="https://maxcdn.bootstrapcdn.com/bootstr <div class="col-sm-4" style="background-
ap/3.4.1/css/bootstrap.min.css"> color:lavender;">.col-sm-4</div>
<script <div class="col-sm-8" style="background-
src="https://ajax.googleapis.com/ajax/libs/jquer color:lavenderblush;">.col-sm-8</div>
y/3.6.4/jquery.min.js"></script> </div>
<script </div>
src="https://maxcdn.bootstrapcdn.com/bootstra
p/3.4.1/js/bootstrap.min.js"></script> </body>
</head> </html>
No gutters
• <div class="row row-no-gutters">
<div class="col-sm-3">.col-sm-3</div>
<div class="col-sm-6">.col-sm-6</div>
<div class="col-sm-3">.col-sm-3</div>
</div>
Two Columns With Two Nested Columns
The following example shows how to get two columns starting at tablets and scaling to large
desktops, with another two columns (equal widths) within the larger column (at mobile
phones, these columns and their nested columns will stack):
</body>
Mixed: Mobile And Desktop
• The Bootstrap grid system has
• <div class="row"> four classes: xs (phones), sm
<div class="col-xs-9 col-md-7">.col-
xs-9 .col-md-7</div>
(tablets), md (desktops), and lg
<div class="col-xs-3 col-md-5">.col- (larger desktops). The classes can
xs-3 .col-md-5</div>
</div> be combined to create more
dynamic and flexible layouts.
<div class="row">
<div class="col-xs-6 col-md-10">.col-
xs-6 .col-md-10</div>
<div class="col-xs-6 col-md-2">.col-
xs-6 .col-md-2</div>
</div> • Tip: Each class scales up, so if
<div class="row"> you wish to set the same widths
<div class="col-xs-6">.col-xs-6</div>
<div class="col-xs-6">.col-xs-6</div>
for xs and sm, you only need to
</div> specify xs.
Mixed: Mobile, Tablet And Desktop
• <div class="row">
<div class="col-xs-7 col-sm-6 col-lg-8">.col-xs-
7 .col-sm-6 .col-lg-8</div>
<div class="col-xs-5 col-sm-6 col-lg-4">.col-xs-
5 .col-sm-6 .col-lg-4</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-8 col-lg-10">.col-xs-6
.col-sm-8 .col-lg-10</div>
<div class="col-xs-6 col-sm-4 col-lg-2">.col-xs-
6 .col-sm-4 .col-lg-2</div>
</div>
Clear Float
Clear floats (with the .clearfix class) at specific breakpoints to prevent strange wrapping with uneven content:
Example
<div class="row">
<div class="col-xs-6 col-sm-3">
Column 1
<br>
Resize the browser window to see the effect.
</div>
<div class="col-xs-6 col-sm-3">Column 2</div>
<!-- Add clearfix for only the required viewport -->
<div class="clearfix visible-xs"></div>
<div class="col-xs-6 col-sm-3">Column 3</div>
<div class="col-xs-6 col-sm-3">Column 4</div>
</div>
<body>
<div class="container-fluid">
<h1>Hello World!</h1>
<div class="row">
<div class="col-xs-6 col-sm-3" style="background-color:lavender;">
Column 1<br>
Resize the browser window to see the effect. Also try to remove the div clearfix line and see what happens.
</div>
<div class="col-xs-6 col-sm-3" style="background-color:lavenderblush;">Column 2</div>
<!-- Add clearfix for only the required viewport -->
<div class="clearfix visible-xs"></div>
<div class="col-xs-6 col-sm-3" style="background-color:lightcyan;">Column 3</div>
<div class="col-xs-6 col-sm-3" style="background-color:lightgray;">Column 4</div>
</div>
</div>
</body>
</html>
Offsetting Columns
Move columns to the right using .col-md-offset-* classes. These classes increase the left margin of a
column by * columns:
Example
<div class="row">
<div class="col-sm-5 col-md-6">.col-sm-5 .col-md-6</div>
<div class="col-sm-5 col-sm-offset-2 col-md-6 col-md-offset-0">
</div>
<
<body>
<div class="container-fluid">
<h1>Offsetting Columns</h1>
<p>Resize the browser window to see the effect.</p>
<div class="row" style="background-color:lavender;">
<div class="col-sm-5 col-md-6" style="background-color:lightgray;">.col-sm-5 .col-md-6</div>
<div class="col-sm-5 col-sm-offset-2 col-md-6 col-md-offset-0" style="background-color:lightcyan;">.col-sm-5 .col-
sm-offset-2 .col-md-6 .col-md-offset-0</div>
</div>
</div>
</body>
</html>
Push And Pull - Change Column Ordering
Change the order of the grid columns with .col-md-push-* and .col-md-pull-* classes:
Example
<div class="row">
<div class="col-sm-4 col-sm-push-8">.col-sm-4 .col-sm-push-8</div>
<div class="col-sm-8 col-sm-pull-4">.col-sm-8 .col-sm-pull-4</div>
</div>
<body>
<div class="container-fluid">
<h1>Push and Pull</h1>
<p>Resize the browser window to see the effect.</p>
<div class="row">
<div class="col-sm-4 col-sm-push-8" style="background-
color:lavender;">.col-sm-4 .col-sm-push-8</div>
<div class="col-sm-8 col-sm-pull-4" style="background-
color:lavenderblush;">.col-sm-8 .col-sm-pull-4</div>
</div>
</div>
</body>
</html>