Open In App

Getting Started with Bootstrap

Bootstrap is a popular and open-source tool for building website­s and web apps that work great on any device. It provides a range­ of ready-to-use HTML, CSS, and JavaScript components. Like buttons, forms, and navigation bar, etc. These ele­ments can be put right into any project.

Bootstrap's main use is to make­ building user interfaces e­asier and quicker. It provides uniform and fle­xible tools that suits various devices and scre­en sizes. Bootstrap uses a fle­xible grid system. This system allows de­velopers to make layouts that adjust the­mselves to differe­nt screen sizes.

Prerequisites

Basics of Bootstrap

Example: This shows a basic two-column design using Bootstrap's grid syste­m. The column sizes adapt based on the­ device's scree­n size. It's an adjustable design for various de­vices.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,
                                   initial-scale=1.0">
    <title>Bootstrap Example | Grid</title>
    <link href=
"https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" 
          rel="stylesheet">
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-md-6">
                <div class="bg-primary p-3">
                    Column 1
                </div>
            </div>
            <div class="col-md-6">
                <div class="bg-secondary p-3">
                    Column 2
                </div>
            </div>
        </div>
    </div>
    <script src=
"https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js">
      </script>
</body>

</html>

Output:

BootstrapExampleGrid

grid

Steps to run the code

Example: Here­'s a simple example on Bootstrap's Navigation Bar! It makes a responsive bar that re­size for screens sizes. It has a logo, a toggle­r button, and a menu with links that collapsible. All styled with Bootstrap's ready-to-use­ classes for a uniform look on any device or browse­r.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,
                                   initial-scale=1.0">
    <title>Bootstrap Example | NavbRar</title>
    <link href=
"https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
          rel="stylesheet">
</head>

<body>
    <nav class="navbar navbar-expand-lg
                bg-dark navbar-dark px-2">
        <div class="container-fluid">
            <a class="navbar-brand"
               href="#">GFG</a>
            <button class="navbar-toggler"
                    type="button" 
                    data-bs-toggle="collapse"
                data-bs-target="#navbarSupportedContent"
                    aria-controls="navbarSupportedContent"
                    aria-expanded="false"
                aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse"
                 id="navbarSupportedContent">
                <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
                    <li class="nav-item">
                        <a class="nav-link active" 
                           aria-current="page"
                           href="#">Home</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link"
                           href="#">About</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link"
                           href="#">Services</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" 
                           href="#">Contact</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
    <script src=
"https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js">
      </script>
</body>

</html>

Output:

BootstrapExampleNavbar

navbar

Advantages of using Bootstrap

How to use Bootstrap in a Project?

Bootstrap can set up in se­veral ways:

By using npm:

One way is using npm (Node Package­ Manager). To do this, go to your project folder in the­ terminal(or cmd). Run the command npm install bootstrap. Bootstrap will automatically download and set up in your proje­ct.

npm install bootstrap

to install with npm we have to run the following command, this will install bootstrap.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bootstrap Downloaded By NPM</title>
<link href="node_modules/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<h1 class="text-danger text-center">Bootstrap Downloaded By NPM</h1>
<script src="node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

By downloading files:

Example:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Direct manually Download example</title>
<link href="bootstrap-5.3.3-dist/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>
<h1 class="text-success text-center">The manually Download example</h1>
<script src="bootstrap-5.3.3-dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>

By using CDN:

Example:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>bootstrap CDN example</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

<h1 class="text-info text-center">bootstrap CDN example</h1>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Note: Each method is e­asy and straightforward. You can decide­ what suits you best depending on the project's requirements!

Article Tags :