Getting started (1): Startup / Download / Contents / Browsers / JavaScript
The easiest way to start up with Bootstrap 4 is using CDN. This tutorial is also made throughout with CDN.
The structure of the starter template is shown as follows. ⇒ Structure
<!DOCTYPE html>
html
Attribute
lang="{language-code}"
head
meta
Attribute
charset="utf-8"
meta
Attributes
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
link
Attributes
rel="stylesheet"
href="{URL of Bootstrap CSS CDN}"
integrity="sha384-{SHA-384 string}"
crossorigin="anonymous"
title
{page-title}
style
, etc.body
script
Attributes
src="{URL of jQuery CDN}"
integrity="sha384-{SHA-384 string}"
crossorigin="anonymous"
script
Attributes
src="{URL of Popper.js CDN}"
integrity="sha384-{SHA-384 string}"
crossorigin="anonymous"
script
Attributes
src="{URL of Bootstrap JavaScript CDN}"
integrity="sha384-{SHA-384 string}"
crossorigin="anonymous"
HTML documents must start with <!DOCTYPE html>
. It is required for backward compatibility to indicate it is a HTML 5.X document. ⇒ Reference Note
DOCTYPE
is usually written in uppercase. It is required for older HTML's (XHTML, HTML4, etc.), influenced by their ancestor SGML. But it is now case-insensitive (since HTML5.0, see W3C reference). Bootstrap starter template uses lowercase <!doctype html>
. It is valid but still rare.
Set the main language of the page with the attribute lang="{language code}"
to the root element html
. Use one of two-letter codes in ISO 639-1 (i.e. lang="en"
for English). ⇒ Reference
lang
)
UTF-8 must be used for the character encoding. ⇒ Summary Reference
meta
tag
charset="utf-8"
"UTF-8"
and "Utf-8"
are also validcharset
)
Viewport setting are required for mobile devices. ⇒ Reference Note About iOS 9
viewport
)
Note (May 2018): Setting a viewport using a meta
tag with name="viewport" content="..."
was first implemented by Safari. As other mobile divices followed later, it has become a de-facto standard.
The original iPhone/Safari syntax uses meta
only. But the working draft has been made as a part of CSS. Most present mobile browsers support meta
but only a few browsers support its CSS implementation (see caniuse - @viewport and MDN - @viewport for CSS support).
The working draft has a mapping rule between content
value (delimited by comma ,
) and CSS @viewport
(delemited by semicolon ;
).
shrink-to-fit=no
is a iPhone iOS 9 (Safari 9)-only setting. Without this, it ignores initial-scale=1
to fit (shrink) to the viewport (as Safari 9 default). See What's New in Safari - 9.0. It is not written in W3C editor's draft as Safari 10 removed this feature. Other browsers just ignore it.
Set attributes integrity="sha384-{SHA-384 string}"
and crossorigin="anonymous"
to loader tags (link
for CSS, script
for JavaScript) on using CDN. ⇒ Reference Note
integrity
crossorigin
SHA-384 is one of SHA-2 Cryptographic hash function family, and is regarded to have enough cryptographic strength for current Web applications. It is used to have CDN verify a fingerprint (Base64 string of SHA-384) of data.
You can copy and paste fingerprint strings from Bootstrap starter template. You can also calculate strings by yourself using terminal command-line (see Tools for generating SRI hashes of MDN - Subresource Integrity for detail).
Load the CSS file from BootstrapCDN. See starter template for actual attributes. ⇒ Note
Bootstrap is mostly a CSS library. But some dynamic features are implemented with JavaScript. It depends on jQuery and is (historically) called "(Bootstrap) JavaScript plugin" as it is written as a jQuery plugin. Bootstrap (≥v4.0) also depends on Popper.js.
Both of them must be loaded before Bootstrap JavaScript plugin. Usually, JavaScript libraries are loaded in the following order. But you can exchange 1 and 2 as jQuery and Popper.js are independent. See starter template for actual attributes.
You can get and use Bootstrap in various ways.
(.map
files are omitted)
bootstrap/
Files
css/
Files
bootstrap(.min).css
- Full versionbootstrap-grid-(.min).css
- Grid system and Flex utilities onlybootstrap-reboot-(.min).css
- Reboot (reset CSS) onlyjs/
Files
bootstrap.bundle(.min).js
- Popper.js and Bootstrap JS (jQuery requied as prerequisites)bootstrap(.min).js
- Bootstrap JS only (jQuery and Popper.js requied as prerequisites)(Partial items only)
bootstrap-4.X.X/
Files
assets/
- CSS, JavaScript, and image files for documentationbuild/
- Script files and settings for building Bootstrapdist/
- Precompiled files (same as Precompiled Bootstrap)docs/
- Documentation source files (Markdown)js/
JavaScript files Files
nuget/
- Files for NuGet (pacage manager for .NET)scss/
Sass (.scss) source filespackage.json
In current JavaScript programming, three major mojule specifications exist as follows.
Bootstrap JavaScript modular sources are written in ES2015. They can be combined with other ES2015 modules (e.g. unbundled modular sources of Popper.js).
CommonJS and AMD are also supported from version 4.1.3. Modular source files in bootstrap-4.X.X/js/dist/
are processed output by rollup.js to UMD (Universal Module Definition - a file format which supports both CommonJS and AMD). UMD files processed by rollup.js also support loading directly from browsers with <script>
tags.
Bootstrap 4 supports major browsers and platforms. Supported browsers are described in .browserslistrc
(package.json
until v4.1.2) by browserslist format (see Best Practices and Full List for the query syntax). ⇒ Show entry
# https://github.com/browserslist/browserslist#readme
>= 1%
last 1 major version
not dead
Chrome >= 45
Firefox >= 38
Edge >= 12
Explorer >= 10
iOS >= 9
Safari >= 9
Android >= 4.4
Opera >= 30
It is referenced by Autoprefixer to process CSS with adding vendor prefixes. Autoprefixer is a required tool for customizing Bootstrap CSS (≥ ver. 3.2, see Customizing CSS for detail).
See the official pages for actual supports as below links.
Bootstrap has JavaScript API for implementing dynamic behaiviors. They are is called "(Bootstrap) JavasScript plugins" as they are made as jQuery plugins. See Loading JavaScript for the basic startup settings.
Their original (unbundled) source files are written as ES2015(ES6) modules. You can also make use of JavaScript API's as modules. See Bootstrap source code (press About JavaScript modules button to show detail).
Bootstrap JavaScript plugins implicitly perform initialization processes on startup on load as default (usually you do not have to write any code). However, following components need explicit initializations (see code examples at target links).
Bootstrap JavaScript plugins use custom data attributes (data-*
) for setting options. You can write HTML attributes which begin with data-
(e.g. data-collapse
) to set various API options without any additional JavaScript code.
Bootstrap JavaScript API is made as a collection of jQuery plugins. You first select elements with jQuery, and invoke methods from elements. The code below is example invocations of Modals. See available options for Options, also see methods (command strings) for Methods.
// Example 1: Creates an interface object with defaults (normally not required)
$('#my-modal').modal();
// Example 2: Sets an option (same as setting an attribute as data-keyboard="false")
// (see "Options" of the official document)
$('#my-modal').modal({ keyboard: false });
// Example 3: Executes a command
// (see "Methods" of the official document)
$('#my-modal').modal('show');
Bootstrap JavaScript plugins provide custom events for their specific actions. Event names are provided with the jQuery event namespace (dot-separated). See the naming convention as follows.
{event}.bs.{component}
(e.g. show.bs.modal
)
{event}
- Event name (e.g. show
)bs
- First namespace - Specifies it is a part of Bootstrap API{component}
- Second namespace - A component name as a jQuery plugin (e.g. modal
)The code below is an example of setting an event handler to a Modal component. See Events for the whole listing of available events.
// Event "show.bs.modal" fires when the method $().modal('show') is invoked.
$('#my-modal').on('show.bs.modal', function(event) {
// The modal is going to show but is not shown yet.
// You can use here to change modal contents to display.
});
Also see the live demo example of Varying modal content (working JavaScript code and detailed comments are available).
Bootstrap JavaScript plugins are registered to $.fn
(jQuery prototype object). All Bootstrap plugins are ($.fn.)alert
, button
, carousel
, collapse
, dropdown
, modal
, popover
, scrollspy
, tab
, toast
, and tooltip
. As they they are registered as plain lowercase names, they may conflict to other jQuery plugins. You can use .noConflict()
to resolve conflicts.
// Restores (the original) $.fn.button and gets Bootstrap button plugin object.
var bootstrapButton = $.fn.button.noConflict();
// Re-registor as a safe different name (bootstrapBtn)
$.fn.bootstrapBtn = bootstrapButton;
// You can call button API as $(SELECTOR).bootstrapBtn(ARGUMENTS)
Default settings of each plugins are set to $.fn.{module-name}.Constructor.Default
objects. You can change API defaults as follows.
// Example: Changes the default setting of keyboard operation to OFF.
$.fn.modal.Constructor.Default.keyboard = false;