How to use bootstrap with html


Setting up Bootstrap in a basic HTML project with JavaScript is straightforward. Here’s a step-by-step guide for setting up Bootstrap, including the necessary CSS and JavaScript files. This example uses the CDN method, which is the quickest way to integrate Bootstrap into a project without additional tools or a local server.

1. Basic Project Structure

The basic structure of a Bootstrap project will include:

  • An HTML file that references Bootstrap's CSS for styling.
  • Optional JavaScript for adding interactive components like modals, dropdowns, and tooltips.
project-folder/ │ ├── index.html └── css/ └── js/

2. HTML Boilerplate with Bootstrap

Let’s build an example HTML file that includes the basic Bootstrap setup:

Step-by-Step: HTML with Bootstrap (CDN)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Bootstrap Basic Setup</title> <!-- Bootstrap CSS via CDN --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+Knujsl5+CEmAIklW7R39xkE5skCpce4N9q+t4Z1YV4BXTH" crossorigin="anonymous"> </head> <body> <!-- Navigation Bar (Example Bootstrap Component) --> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <a class="navbar-brand" href="#">Navbar</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav"> <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="#">Features</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Pricing</a> </li> </ul> </div> </nav> <!-- Bootstrap Grid System --> <div class="container"> <div class="row"> <div class="col-md-8"> <h1>Welcome to Bootstrap Setup</h1> <p>This is an example of a basic Bootstrap project with HTML and JavaScript setup.</p> </div> <div class="col-md-4"> <button class="btn btn-primary" id="myButton">Click Me</button> </div> </div> </div> <!-- Bootstrap JavaScript and Popper.js via CDN --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js" integrity="sha384-9NdAAo8B7fUz+SIRtwFpsRnWnfs1U4R1P1QjE6KU5Qb8XZRucwhDkFDddjv7v4/s" crossorigin="anonymous"></script> <!-- Optional Custom JavaScript --> <script> document.getElementById("myButton").addEventListener("click", function() { alert("Button clicked!"); }); </script> </body> </html>

3. Explanation of Key Parts

1. Basic HTML Structure

  • The <html>, <head>, and <body> tags represent a standard HTML document.
  • The <meta> tags ensure that the page scales properly on different devices (responsive design).

2. Bootstrap CSS

  • This <link> tag includes Bootstrap’s CSS file via a CDN:

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
  • The rel="stylesheet" specifies that it’s a stylesheet, and the href contains the URL pointing to the Bootstrap CSS file.

3. Navbar (Bootstrap Component)

  • The example includes a navigation bar using Bootstrap classes (navbar, navbar-expand-lg, bg-light), which provide styles and layout for the navbar.

4. Grid System

  • The container, row, and col-md-* classes create a responsive layout.
  • In this example, the grid is divided into 8 and 4 columns using Bootstrap’s grid system.

5. JavaScript (Bootstrap + Popper.js)

  • The <script> tag loads Bootstrap’s JavaScript via CDN:

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
  • Popper.js (included in bootstrap.bundle.min.js) is required for components like tooltips, dropdowns, and modals.

6. Custom JavaScript

  • A small script adds a simple JavaScript interaction:

    document.getElementById("myButton").addEventListener("click", function() { alert("Button clicked!"); });
  • When the "Click Me" button is pressed, an alert pops up.

4. Bootstrap Components in Action

  • Navbar: Provides a responsive navigation bar with dropdown support.
  • Grid System: Makes the page responsive using a flexible grid structure.
  • Buttons: The "Click Me" button is styled using the btn and btn-primary classes.
  • JavaScript Components: Using the included JavaScript bundle, interactive components like dropdowns, modals, and tooltips work out of the box.

5. Customizing Bootstrap

  • To customize Bootstrap (e.g., color schemes, spacing), you can either override the default Bootstrap classes in a custom CSS file or use SASS to compile your own Bootstrap version.