Bootstrap supplies finished interface parts. The author adds the right classes, and the component appears fully styled.

A Bootstrap navbar is written with the classes navbar and navbar-expand-lg, a brand link and a list of items with the class nav-link. On screens smaller than the large breakpoint, it collapses into a menu button, which a toggler button controls. The same HTML thus works on phones and desktops.

Cards

A card is a bordered box for a piece of content. The class card makes the box, and card-body adds padding inside it. Inside a card body, card-title and card-text style a heading and a paragraph. A card can also hold an image at the top. Cards are often placed inside grid columns to make a row of equal cards.

Buttons

A button gets the class btn plus a colour class: btn-primary for blue, btn-success for green, btn-danger for red and btn-outline-secondary for an outlined style. The classes btn-lg and btn-sm change the size.

Table 2.9: Common Bootstrap components

Component Main classes Purpose
Navbar navbar, nav-link Site navigation
Card card, card-body Boxed content
Button btn, btn-primary Actions
Alert alert, alert-warning Messages
Badge badge, bg-success Small labels
Modal modal, modal-dialog Dialog over the page

A modal is a dialog that appears above the page and darkens the background until the user closes it. A button with data-bs-toggle="modal" and data-bs-target="#myModal" opens the modal whose id is myModal. A modal needs Bootstrap's JavaScript file as well as its CSS, so the page must also load the Bootstrap bundle script before the closing body tag.

Definition: Modal. A dialog box that appears above the page and asks for the user's attention.

Example 2.9: A card and a button

<link rel="stylesheet"
  href="https://unpkg.com/bootstrap@5.3.3/dist/css/bootstrap.min.css">
<div class="card" style="width: 18rem;">
  <div class="card-body">
    <h5 class="card-title">BCA Course</h5>
    <p class="card-text">Three-year programme in computer applications.</p>
    <a href="#" class="btn btn-primary">Apply now</a>
  </div>
</div>

Output in the browser:

Alerts, Badges and Forms

Bootstrap also styles smaller parts. An alert is a coloured message box made with alert and a colour class such as alert-warning, and it can carry a close button. A badge is a small label made with badge and a background class, and it is often used for counts. Form controls receive the class form-control, and form-label styles the label above them, so that fields become full width, rounded and consistent. The classes form-check and form-check-input style checkboxes and radio buttons in the same way. Used together, these classes give a complete, tidy login form with very little custom CSS.

Common mistakes

  • Forgetting the Bootstrap JavaScript file, so that the navbar toggle and modal do not work.
  • Writing the data-bs-target value without the hash sign.
  • Using the old Bootstrap 4 attribute names, such as data-toggle, with Bootstrap 5.
  • Nesting components in the wrong order, for example a card-title outside card-body.

Key takeaways

  • Components work by adding the right Bootstrap classes to ordinary HTML.
  • A navbar collapses into a menu button on small screens.
  • Cards, buttons, alerts and badges need only classes, and the modal also needs JavaScript.
  • A modal is opened by a button that names it with data-bs-target.

Practice questions

  1. Which class gives a blue button?
  2. Why does a modal need the Bootstrap JavaScript file?
  3. What does card-body do?

Answers to the practice questions

  1. btn-primary together with btn.
  2. Opening and closing the dialog is done by Bootstrap's script, not by CSS alone.
  3. It adds padding and holds the content inside a card.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit II