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 like 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 the Bootstrap requires HTML5 doctype to render HTML elements and CSS properties without any styling problems.

 

As discussed, the Bootstrap framework built with mobile-first strategy to make sure 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 required Bootstrap CSS and JS files to convert the above HTML template to 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 which we need to create to use 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, then Popper.js, and then 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 like as shown below.

 

Hello World

 
 
 

Topics Covered