BOOTSTRAP 4 GETTING STARTED
In this tutorial, you will learn how to add bootstrap to your project using CDN or downloaded module and create your first bootstrap powered webpage.
What does exaclty bootstrap provide?
Before getting started with Bootstrap first understand what actually bootstrap delivers us and how to use them.
- want to a bordered table - add .table-bordered class to <table> tag
- Want some text color - Add text color class of bootstrap like .text-primary, .text-danger, etc.
It prevents you from writing much of CSS and at the same time building modern and responsive websites using it.
How To Setup Bootstrap?
Bootstrap can be very easily added to your HTML project. There are 2 different ways by which you can add bootstrap to HTML files.
What is CDN?
CDN stands for Content Delivery Network. It refers to geographically distributed data centres and servers that ensure to deliver data faster.
1. Bootstrap CDN
Bootstrap can be very easy to set up to HTML file using its CDN (Content Delivery Network).
Adding Bootstrap CSS - To add CSS part of bootstrap include the following
<head> seaction of your HTML document before all other stylesheets to load CSS part of bootstrap.
<link rel="stylesheet" href="https://email@example.com/dist/css/bootstrap.min.css">
Add the following
<script> just before the end of
<!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script> <script src="https://firstname.lastname@example.org/dist/umd/popper.min.js"></script> <script src="https://email@example.com/dist/js/bootstrap.min.js"></script>
2. Bootstrap download
To download compiled and ready to use Bootstrap 4 files visit the official site and click the download button.
A zip file will download as shown below.
Now unzip (extract) the file and you get 2 different folders inside it: JS and CSS as shown below.
Adding Bootstrap CSS - Open CSS folder and you get various files with extension .css and .map. But we are only interested in CSS file. i.e files with .css extension.
Now you have bootstrap.css and bootstrap.min.css file in the folder. Both of these files are the same, just one is compressed and another is not. Add either of these two files in your HTML file in the head section using the link tag. Leave the rest files as it is. ( bootstrap.min.css is recommended because it is minified therefore has less size ).
<link rel="stylesheet" href="./bootstrap/css/bootstrap.min.css">
Adding Bootstrap JS - Similarly, in the JS folder find the bootstrap.min.js file and add it to your HTML file (minified file is recommended to add).
You will also have to add jquery and Popper.js externally for some of the bootstrap components to work properly. Now your bootstrap set to be used.
<!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script> <script src="https://firstname.lastname@example.org/dist/umd/popper.min.js"></script> <script src="./bootstrap/js/bootstrap.min.js"></script>
Before using bootstrap you need to be aware of some important global style and settings used by bootstrap.
These styles and settings are for the normalization of cross-browser style.
Bootstrap uses HTML and requires the use of the HTML5
doctype. Always include HTML5 doctype at the beginning of the page. Without it, you will see some incomplete style.
Also, add the lang attribute and the correct character set. As shown below in the code.
<!DOCTYPE html> <html lang="en">
Responsive Meta Tag
Bootstrap is developed as a mobile-first approach, it optimizes code for mobile devices first and then scales it up to larger devices using a media query. To ensure proper rendering of the Bootstrap component add the following meta tag in
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
The width=device-width part of the above meta tag sets the width of the webpage to follow the screen width of the device.
The initial-scale=1 part sets the initial zoom level when the page is first loaded.
The shrink-to-fit=no override the behaviour caused by width=device-width to cause the page to scale down to fit the content that overflows the viewport bounds.
Bootstrap starter template
Now we have understood how to add bootstrap to web pages and what extra bootstrap globals we have to care about. Let's now look at the bootstrap starter template.
To start with bootstrap you can directly copy this code, paste it into your code editor and start working on your bootstrap project.
▶ Run the code
<!DOCTYPE html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <title>Bootstrap starter template</title> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://email@example.com/dist/css/bootstrap.min.css"> </head> <body> <h1>Hello, world!</h1> <!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script> <script src="https://firstname.lastname@example.org/dist/umd/popper.min.js"></script> <script src="https://email@example.com/dist/js/bootstrap.min.js"></script> </body> </html>
Bootstrap CDN vs Local
It is suggested to use CDN because your visitors may already have visited some websites that contain Bootstrap.
In that case, their browser has already downloaded Bootstrap's CSS and JS from the same CDN. The files are stored in the cache memory and when visitors visit your website then it directly loads it from cache memory makes the website faster.
Also, CDN fetches data from the closest server which may be another reason to fasten your website.
Want to learn more about programming and web development? We have a few recommendations for you.
Points to remember:
- There are 2 ways to add bootstrap to your website: 1. Using CDN, 2. By downloading a compiled version of bootstrap
- To ensure proper rendering by bootstrap add viewport metatag in the head.