1. Add the HTML5 doctype
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.
To ensure proper rendering and touch zooming, add the following <meta>
tag inside the <head>
element:
<meta name="viewport" content="width=device-width, initial-scale=1">
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
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.
There are two container classes to choose from:
- The
.container
class provides a responsive fixed width container - The
.container-fluid
class provides a full width container, spanning the entire width of the viewport
Two Basic Bootstrap Pages
The following example shows the code for a basic Bootstrap page (with a responsive fixed width container):
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h1>My First Bootstrap Page</h1>
<p>This is some text.</p>
</div>
</body>
</html>
The following example shows the code for a basic Bootstrap page (with a full width container):
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
<h1>My First Bootstrap Page</h1>
<p>This is some text.</p>
</div>
</body>
</html>
Bootstrap Grid System
Bootstrap's grid system allows up to 12 columns across the page.
If you do not want to use all 12 columns individually, you can group the columns together to create wider columns:
span 1 | span 1 | span 1 | span 1 | span 1 | span 1 | span 1 | span 1 | span 1 | span 1 | span 1 | span 1 |
span 4 | span 4 | span 4 | |||||||||
span 4 | span 8 | ||||||||||
span 6 | span 6 | ||||||||||
span 12 |
Bootstrap's grid system is responsive, and the columns will re-arrange automatically depending on the screen size.
Grid Classes
The Bootstrap grid system has four classes:
xs
(for phones - screens less than 768px wide)sm
(for tablets - screens equal to or greater than 768px wide)md
(for small laptops - screens equal to or greater than 992px wide)lg
(for laptops and desktops - screens equal to or greater than 1200px wide)
The classes above can be combined to create more dynamic and flexible layouts.
Basic Structure of a Bootstrap Grid
The following is a basic structure of a Bootstrap grid:
<div class="row">
<div class="col-*-*"></div>
<div class="col-*-*"></div>
</div>
<div class="row">
<div class="col-*-*"></div>
<div class="col-*-*"></div>
<div class="col-*-*"></div>
</div>
<div class="row">
...
</div>
First; create a row (<div class="row">
). Then, add the desired number of columns (tags with appropriate .col-*-*
classes). Note that numbers in .col-*-*
should always add up to 12 for each row.
Below we have collected some examples of basic Bootstrap grid layouts.
Three Equal Columns
The following example shows how to get a three equal-width columns starting at tablets and scaling to large desktops. On mobile phones or screens that are less than 768px wide, the columns will automatically stack:
Example
<div class="row">
<div class="col-sm-4">.col-sm-4</div>
<div class="col-sm-4">.col-sm-4</div>
<div class="col-sm-4">.col-sm-4</div>
</div>
Two Unequal Columns
The following example shows how to get two various-width columns starting at tablets and scaling to large desktops:
Example
<div class="row">
<div class="col-sm-4">.col-sm-4</div>
<div class="col-sm-8">.col-sm-8</div>
</div>
Tip: You will learn more about Bootstrap grids later in this tutorial.
Bootstrap's Default Settings
Bootstrap's global default font-size is 14px, with a line-height of 1.428.
This is applied to the <body>
element and all paragraphs (<p>
).
In addition, all <p>
elements have a bottom margin that equals half their computed line-height (10px by default).
Bootstrap vs. Browser Defaults
In this chapter, we will look at some HTML elements that will be styled a little bit differently by Bootstrap than browser defaults.
<h1> - <h6>
By default, Bootstrap will style the HTML headings (<h1>
to <h6>
) in the following way:
Example
h1 Bootstrap heading (36px)
h2 Bootstrap heading (30px)
h3 Bootstrap heading (24px)
h4 Bootstrap heading (18px)
h5 Bootstrap heading (14px)
h6 Bootstrap heading (12px)
<small>
In Bootstrap the HTML <small>
element is used to create a lighter, secondary text in any heading:
Example
h1 heading secondary text
h2 heading secondary text
h3 heading secondary text
h4 heading secondary text
h5 heading secondary text
h6 heading secondary text
<mark>
Bootstrap will style the HTML <mark>
element in the following way:
Example
Use the mark element to highlight text.
<abbr>
Bootstrap will style the HTML <abbr>
element in the following way:
Example
The WHO was founded in 1948.
<blockquote>
Bootstrap will style the HTML <blockquote>
element in the following way:
Example
For 50 years, WWF has been protecting the future of nature. The world's leading conservation organization, WWF works in 100 countries and is supported by 1.2 million members in the United States and close to 5 million globally.
To show the quote on the right, use the .blockquote-reverse
class:
Example
For 50 years, WWF has been protecting the future of nature. The world's leading conservation organization, WWF works in 100 countries and is supported by 1.2 million members in the United States and close to 5 million globally.
<dl>
Bootstrap will style the HTML <dl>
element in the following way:
Example
- Coffee
- - black hot drink
- Milk
- - white cold drink
<code>
Bootstrap will style the HTML <code>
element in the following way:
Example
The following HTML elements: span
, section
, and div
defines a section in a document.
<kbd>
Bootstrap will style the HTML <kbd>
element in the following way:
Example
Use ctrl + p to open the Print dialog box.
<pre>
Bootstrap will style the HTML <pre>
element in the following way:
Example
Text in a pre element is displayed in a fixed-width font, and it preserves both spaces and line breaks.
Contextual Colors and Backgrounds
Bootstrap also has some contextual classes that can be used to provide "meaning through colors".
The classes for text colors are:.text-muted
, .text-primary
, .text-success
, .text-info
, .text-warning
, and .text-danger
:
Example
This text is muted.
This text is important.
This text indicates success.
This text represents some information.
This text represents a warning.
This text represents danger.
The classes for background colors are:.bg-primary
, .bg-success
, .bg-info
, .bg-warning
, and .bg-danger
:
Example
This text is important.
This text indicates success.
This text represents some information.
This text represents a warning.
This text represents danger.
More Typography Classes
The Bootstrap classes below can be added to style HTML elements further:
Class | Description | Example |
---|---|---|
.lead | Makes a paragraph stand out | |
.small | Indicates smaller text (set to 85% of the size of the parent) | |
.text-left | Indicates left-aligned text | |
.text-center | Indicates center-aligned text | |
.text-right | Indicates right-aligned text | |
.text-justify | Indicates justified text | |
.text-nowrap | Indicates no wrap text | |
.text-lowercase | Indicates lowercased text | |
.text-uppercase | Indicates uppercased text | |
.text-capitalize | Indicates capitalized text | |
.initialism | Displays the text inside an <abbr> element in a slightly smaller font size | |
.list-unstyled | Removes the default list-style and left margin on list items (works on both <ul> and <ol> ). This class only applies to immediate children list items (to remove the default list-style from any nested lists, apply this class to any nested lists as well) | |
.list-inline | Places all list items on a single line | |
.dl-horizontal | Lines up the terms (<dt> ) and descriptions (<dd> ) in <dl> elements side-by-side. Starts off like default <dl> s, but when the browser window expands, it will line up side-by-side | |
.pre-scrollable | Makes a <pre> element scrollable |
0 Comments