Bootstrap First Web Page Example

In previous bootstrap chapters, we learned what is bootstrap? and how to setup bootstrap environment. Now, we will learn how to create a simple bootstrap hello world application by using bootstrap components with example.

 

To create a bootstrap web page template, first we need to include HTML5 doctype at the beginning of the page, along with lang and viewport meta tags for proper responsive behavior as shown below.

 

<!DOCTYPE html>
<html lang="en">
<head>
   <!-- Required meta tags -->
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
   ………
</body>
</html>

If you observe the above HTML code, we added lang and chartset attributes to specify the language and chart encoding for the HTML document. We included HTML5 doctype at the beginning of the page because Bootstrap requires HTML5 doctype to render HTML elements and CSS properties without any styling problems.

 

As discussed, the Bootstrap framework is built with a mobile-first strategy to ensure that the design is responsive for mobile devices. So, to ensure proper rendering and touch zooming for all devices we included viewport meta tag in <head> element.

 

Here, the width=device-width part is useful to adjust the page width based on the screen width of the device and initial-scale=1 part is useful to set the initial zooming level whenever the page is first loaded by the browser.

 

Along with the above meta tags, we need to include the required Bootstrap CSS and JS files to convert the above HTML template to a bootstrap template like as shown below.

 

Live Preview<!DOCTYPE html>
<html lang="en">
<head>
   <title>Bootstrap Example</title>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <!-- Bootstrap CSS -->
   <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
   <!-- Optional JavaScript -->
   <!-- jQuery first, then Popper.js, then Bootstrap JS -->
   <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
   <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
    <h1>Hello World</h1>
</body>
</html>

This is the basic template format that we need to create to use a bootstrap framework in our application. Here, the JS files (jquery.js, popper.js, bootstrap.js) are optional, and those are required to add only when you try to use bootstrap JS components such as alerts, modals, buttons for toggling, carousel for sliding, tooltips, etc.

 

While adding JS files (jquery.js, popper.js, bootstrap.js), you need to make sure that the jQuery comes first, second Popper.js, and last bootstrap.js file like as we mentioned in the above code.

 

To improve the performance of your web pages, include your JavaScript files at the bottom of the page, right before the closing </body> tag, instead of adding it in <head> tag.

 

The above example will return the result as shown below.

 

Hello World