Bootstrap 5 Exercises

Learning Bootstrap 5 is best done through practice. While tutorials and guides introduce concepts, exercises let you apply what you’ve learned to build responsive and interactive web layouts.

This post provides practical Bootstrap 5 exercises for developers of all levels. Whether you’re a beginner or looking to polish your skills, these exercises will help you become proficient in using Bootstrap 5.

Why Practice Bootstrap 5?

Bootstrap 5 is one of the most popular front-end frameworks for creating responsive websites. By practicing Bootstrap exercises, you can:

  1. Master Responsive Design: Learn how to build layouts that adapt to different screen sizes.
  2. Save Development Time: Quickly use Bootstrap’s pre-built components and utilities.
  3. Build Real-World Projects: Strengthen your portfolio with projects that demonstrate your skills.
  4. Prepare for Job Interviews: Many technical interviews require hands-on coding, and Bootstrap exercises can help you prepare.

Getting Started with Bootstrap 5 Exercises

Before diving into exercises, ensure you have the following:

  • Code Editor: Install a code editor like Visual Studio Code or Sublime Text.
  • Bootstrap 5 Setup: Include Bootstrap 5 in your project by adding the CDN link or downloading the library. Use the following CDN link in your <head> tag:
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>

Bootstrap 5 Exercises

Exercise 1: Create a Responsive Navbar

Objective:

Build a responsive navigation bar with the following features:

  • Brand logo on the left.
  • Links to “Home,” “About,” and “Contact.”
  • A collapsible menu for smaller screens.

Solution:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Brand</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
      <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" 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="#">Contact</a>
        </li>
      </ul>
    </div>
  </div>
</nav>

Exercise 2: Build a Card Layout

Objective:

Create a card layout with the following:

  • An image at the top.
  • A title, description, and button inside the card.
  • Use a responsive grid system to display 3 cards per row.

Solution:

<div class="container my-5">
  <div class="row g-4">
    <div class="col-md-4">
      <div class="card">
        <img src="https://via.placeholder.com/300x200" class="card-img-top" alt="Image">
        <div class="card-body">
          <h5 class="card-title">Card Title</h5>
          <p class="card-text">Some quick example text to describe the content of the card.</p>
          <a href="#" class="btn btn-primary">Read More</a>
        </div>
      </div>
    </div>
    <div class="col-md-4">
      <div class="card">
        <img src="https://via.placeholder.com/300x200" class="card-img-top" alt="Image">
        <div class="card-body">
          <h5 class="card-title">Card Title</h5>
          <p class="card-text">Some quick example text to describe the content of the card.</p>
          <a href="#" class="btn btn-primary">Read More</a>
        </div>
      </div>
    </div>
    <div class="col-md-4">
      <div class="card">
        <img src="https://via.placeholder.com/300x200" class="card-img-top" alt="Image">
        <div class="card-body">
          <h5 class="card-title">Card Title</h5>
          <p class="card-text">Some quick example text to describe the content of the card.</p>
          <a href="#" class="btn btn-primary">Read More</a>
        </div>
      </div>
    </div>
  </div>
</div>

Exercise 3: Design a Login Form

Objective:

Create a responsive login form with the following:

  • Input fields for email and password.
  • A submit button styled with Bootstrap classes.

Solution:

<div class="container d-flex justify-content-center align-items-center vh-100">
  <form class="border p-4 rounded bg-light" style="width: 300px;">
    <h3 class="text-center mb-4">Login</h3>
    <div class="mb-3">
      <label for="email" class="form-label">Email address</label>
      <input type="email" class="form-control" id="email" placeholder="Enter email">
    </div>
    <div class="mb-3">
      <label for="password" class="form-label">Password</label>
      <input type="password" class="form-control" id="password" placeholder="Enter password">
    </div>
    <button type="submit" class="btn btn-primary w-100">Login</button>
  </form>
</div>

Exercise 4: Create a Responsive Grid

Objective:

Build a grid layout with the following:

  • 12 columns on large screens.
  • 6 columns on medium screens.
  • 4 columns on small screens.

Solution:

<div class="container my-5">
  <div class="row">
    <div class="col-lg-1 col-md-2 col-sm-3 border">1</div>
    <div class="col-lg-1 col-md-2 col-sm-3 border">2</div>
    <div class="col-lg-1 col-md-2 col-sm-3 border">3</div>
    <div class="col-lg-1 col-md-2 col-sm-3 border">4</div>
    <div class="col-lg-1 col-md-2 col-sm-3 border">5</div>
    <div class="col-lg-1 col-md-2 col-sm-3 border">6</div>
    <div class="col-lg-1 col-md-2 col-sm-3 border">7</div>
    <div class="col-lg-1 col-md-2 col-sm-3 border">8</div>
    <div class="col-lg-1 col-md-2 col-sm-3 border">9</div>
    <div class="col-lg-1 col-md-2 col-sm-3 border">10</div>
    <div class="col-lg-1 col-md-2 col-sm-3 border">11</div>
    <div class="col-lg-1 col-md-2 col-sm-3 border">12</div>
  </div>
</div>

Exercise 5: Create a Carousel

Objective:

Use Bootstrap’s Carousel component to create a slideshow.

Solution:

<div id="carouselExample" class="carousel slide" data-bs-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="https://via.placeholder.com/800x400" class="d-block w-100" alt="Slide 1">
    </div>
    <div class="carousel-item">
      <img src="https://via.placeholder.com/800x400" class="d-block w-100" alt="Slide 2">
    </div>
    <div class="carousel-item">
      <img src="https://via.placeholder.com/800x400" class="d-block w-100" alt="Slide 3">
    </div>
  </div>
  <button class="carousel-control-prev" type="button" data-bs-target="#carouselExample" data-bs-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="visually-hidden">Previous</span>
  </button>
  <button class="carousel-control-next" type="button" data-bs-target="#carouselExample" data-bs-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="visually-hidden">Next</span>
  </button>
</div>

Conclusion

Practical exercises are an excellent way to master Bootstrap 5. By working on these projects, you’ll develop a deeper understanding of how to use Bootstrap’s components, grid system, and utilities to build responsive and professional websites.

Leave a Comment