What is Bootstrap?
Bootstrap is a front-end framework designed to simplify the process of creating responsive and visually appealing websites. To give you a clearer picture, let’s go through a few examples of how Bootstrap can be used in practice.
Example 1: Basic Layout with Grid System
Bootstrap's grid system allows you to create layouts that adjust to different screen sizes. Here’s a simple example of a basic layout using Bootstrap's grid classes:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bootstrap Grid Example</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="p-3 mb-2 bg-primary text-white">Column 1</div>
</div>
<div class="col-md-4">
<div class="p-3 mb-2 bg-secondary text-white">Column 2</div>
</div>
<div class="col-md-4">
<div class="p-3 mb-2 bg-success text-white">Column 3</div>
</div>
</div>
</div>
</body>
</html>
Output: This will display a row with three equally sized columns. On medium and larger screens, each column will take up one-third of the width, and on smaller screens, the columns will stack vertically.
Example 2: Navigation Bar
Bootstrap provides a variety of pre-styled navigation components. Here’s an example of a simple navigation bar:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bootstrap Navbar Example</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Brand</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-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 active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></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>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
</div>
</nav>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
Output: This will render a responsive navigation bar with a brand name and several links. On smaller screens, the navigation items will collapse into a hamburger menu.
Example 3: Alert Message
Bootstrap also includes various utility components, like alerts. Here’s how you can use them:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bootstrap Alert Example</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<div class="alert alert-success" role="alert">
This is a success alert—check it out!
</div>
</div>
</body>
</html>
Output: This will show a green success alert message with some padding and margin, which will adapt to various screen sizes.
Summary
Bootstrap makes it easy to create responsive designs with minimal effort. By leveraging its pre-built components and grid system, you can quickly prototype and develop modern web interfaces. The examples provided illustrate just a fraction of what you can achieve with Bootstrap.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.