Writing every style by hand takes time. A CSS framework supplies tested styles and components that can be reused. Bootstrap is the most widely used of these frameworks.

What Bootstrap Is

Bootstrap is a free framework of CSS and JavaScript. It provides a grid for layout, ready-made components such as navbars and cards, and utility classes for spacing and colour. It is responsive, which means that pages built with it adapt to phones, tablets and desktop screens.

Definition: Bootstrap. A free CSS and JavaScript framework of ready-made responsive components.

Adding Bootstrap to a Page

There are two ways. With a CDN, a link element in the head loads Bootstrap from a content delivery network, which needs no download. Alternatively, the files can be downloaded and linked from the project folder, which works offline. A page must also contain the viewport meta tag, <meta name="viewport" content="width=device-width, initial-scale=1">, so that phones scale the page properly.

The Grid System

The Bootstrap grid has twelve column units in each row. A container holds a row, and a row holds columns. A class such as col-md-4 makes a column take four of the twelve units, which is one third of the width, on medium screens and wider. On smaller screens the columns stack one above the other.

Definition: Grid system. Bootstrap's twelve-column layout, built from container, row and column classes.

Breakpoints and Utility Classes

Bootstrap changes the layout at set screen widths called breakpoints. Table 2.8 lists them.

Table 2.8: Bootstrap 5 breakpoints

Class prefix From width Typical device
(none) 0 px Small phones
sm 576 px Large phones
md 768 px Tablets
lg 992 px Laptops
xl 1200 px Desktops
xxl 1400 px Large desktops

Utility classes change one thing each. The class p-3 adds padding, mt-2 adds a top margin, text-center centres text, bg-light gives a light background and d-none d-md-block hides an element on small screens and shows it from medium screens.

Example 2.8: A three-column responsive row

<link rel="stylesheet"
  href="https://unpkg.com/bootstrap@5.3.3/dist/css/bootstrap.min.css">
<div class="container">
  <div class="row text-center">
    <div class="col-md-4 bg-light border p-3">One</div>
    <div class="col-md-4 bg-light border p-3">Two</div>
    <div class="col-md-4 bg-light border p-3">Three</div>
  </div>
</div>

Output in the browser:

On a screen at least 768 pixels wide the three columns sit side by side. On a phone they stack.

Why Use a Framework

A framework saves time because common problems are already solved and tested in all major browsers. Teams that share a framework also share a vocabulary of class names, so a new developer can read the code quickly. The cost is that pages can look alike, and the framework adds file size. Authors should therefore use only what they need and add their own styles for a distinct look.

Common mistakes

  • Putting columns directly in a container without a row.
  • Forgetting the viewport meta tag, so that phones show a tiny desktop page.
  • Adding more than twelve column units in a row, which wraps the extra column.
  • Overriding Bootstrap with many custom styles instead of using its classes.

Key takeaways

  • Bootstrap supplies a grid, components and utility classes, and it is responsive.
  • Link it from a CDN or download it, and add the viewport meta tag.
  • Container, row and column classes build the twelve-unit grid.
  • Breakpoints sm, md, lg, xl and xxl set where the layout changes.

Practice questions

  1. How many units does each Bootstrap row have?
  2. What does col-md-6 do on a phone?
  3. Name two ways to add Bootstrap to a page.

Answers to the practice questions

  1. Twelve.
  2. The column takes the full width and stacks, because md applies only from 768 pixels.
  3. Link it from a CDN, or download the files and link them locally.

Sources

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