Writing all the CSS for a site by hand takes time. Bootstrap provides a ready set of styles and components for responsive pages.
Introduction to Bootstrap
Bootstrap is a free, open-source front-end framework of CSS and JavaScript, first created at Twitter in 2011. It provides ready-made responsive styles for typography, buttons, forms, tables and images, and components such as navbars, cards, modals, carousels and alerts. It is mobile-first and works in all modern browsers. Bootstrap 5, the current version, no longer needs jQuery.
Definition: Bootstrap. A free front-end framework of ready-made CSS classes and JavaScript components for building responsive, mobile-first web pages quickly.
Downloading and Linking
Bootstrap can be used in two ways. Download the compiled CSS and JS files from getbootstrap.com and link them from the project folder. Or link them from a CDN (Content Delivery Network), which is quick and lets browsers reuse cached copies: a <link> to bootstrap.min.css in the head, and a <script> to bootstrap.bundle.min.js before </body>. The viewport meta tag is required.
Using Bootstrap Classes
Bootstrap is used by adding classes to HTML elements. Examples: container (a centred, responsive page box) and container-fluid (full width); btn btn-primary, btn-success and btn-danger for coloured buttons; table table-striped table-bordered for styled tables; form-control and form-label for form fields; img-fluid for responsive images; text-center, text-primary and bg-light for text and colour; spacing utilities such as m-3 (margin) and p-2 (padding); d-flex and justify-content-between for Flexbox; and navbar navbar-expand-lg for a navbar that collapses on small screens.
The Grid System
Bootstrap's grid system lays out pages in rows and columns. A .container holds .row elements, and each row holds .col elements. Each row is divided into 12 columns. A class such as col-md-4 means "take 4 of 12 columns (one third) on medium screens and larger"; on smaller screens, columns stack. The breakpoint prefixes are col- (all sizes), col-sm- (576px and up), col-md- (768px), col-lg- (992px), col-xl- (1200px) and col-xxl- (1400px). Classes can be combined: col-12 col-md-6 col-lg-4 gives one column on phones, two on tablets and three on laptops. gutters (g-3) set the gap between columns, and offset-md-2 shifts a column right.
Table 2.4: Bootstrap grid breakpoints
| Prefix | Screen width | Typical device |
|---|---|---|
| col- | All widths | Phones (stacked by default) |
| col-sm- | 576px and up | Large phones |
| col-md- | 768px and up | Tablets |
| col-lg- | 992px and up | Laptops |
| col-xl- | 1200px and up | Desktops |
Example 2.4: Three course cards with the grid
Problem: Show three course cards that appear one per row on phones, two per row on tablets and three per row on laptops, with styled buttons.
Solution:
- Link Bootstrap from the CDN and add the viewport meta tag.
- Inside a container and a row, give each card the classes col-12 col-md-6 col-lg-4.
- Use card, card-body and btn btn-primary classes.
Result: The cards rearrange themselves at each breakpoint without any custom CSS.
<!-- CDN: cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/ -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/
bootstrap@5.3.3/dist/css/bootstrap.min.css">
<div class="container my-4">
<div class="row g-3">
<div class="col-12 col-md-6 col-lg-4">
<div class="card"><div class="card-body">
<h5 class="card-title">BCA</h5>
<a href="#" class="btn btn-primary">Apply</a>
</div></div>
</div>
<div class="col-12 col-md-6 col-lg-4"> ... BBA card ... </div>
<div class="col-12 col-md-6 col-lg-4"> ... B.Com card ... </div>
</div>
</div>
Common mistakes
- Putting columns directly in a container without a row.
- Making the column numbers in a row add up to more than 12.
- Forgetting to link the Bootstrap JavaScript, so that the navbar toggle does not work.
Key takeaways
- Bootstrap is a responsive, mobile-first CSS and JS framework.
- Link it by download or from a CDN.
- Use it by adding classes such as btn, table, form-control and container.
- The grid has containers, rows and 12 columns with breakpoint prefixes.
Practice questions
- What is Bootstrap?
- How many columns does a Bootstrap row have?
- What does col-md-6 mean?
Answers to the practice questions
- A free front-end framework of ready-made responsive CSS classes and JavaScript components.
- Twelve.
- The column takes 6 of 12 columns (half the width) on screens 768px and wider.
Sources
- Official BCA syllabus Web Technologies, course code 25BCA402SEC02, Unit II
