Module 09 · Lesson 3
Typography, Colors & Utility Classes
The single-purpose helper classes that let you style 90% of a page without writing any CSS.
Introduction
Bootstrap ships with hundreds of utility classes — tiny, single-purpose classes that each set one CSS property. Combine them and you can control typography, color, spacing, and layout right in your HTML. This is the same idea behind "utility-first" CSS, and it makes prototyping extremely fast.
Utilities are the most-used part of Bootstrap. Learn the naming pattern once and you can guess most class names without checking the docs.
Theory
Typography
- Headings —
h1–h6are styled automatically;.h1–.h6apply heading size to any element. - Display headings —
.display-1….display-6for large hero text. - Text utilities —
.text-center,.text-start,.text-end,.fw-bold,.fst-italic,.text-uppercase,.lead,.small,.text-truncate.
Colors & backgrounds
Bootstrap defines theme colors: primary, secondary, success, danger, warning, info, light, dark.
- Text color —
.text-primary,.text-danger,.text-muted,.text-white. - Background —
.bg-success,.bg-warning,.bg-dark,.bg-light.
Spacing
The pattern is {property}{sides}-{size}:
- property:
m(margin) orp(padding) - sides:
t,b,s(start/left),e(end/right),x(left+right),y(top+bottom), or blank (all) - size:
0–5(andautofor margins)
So mt-3 = margin-top size 3, px-4 = horizontal padding size 4, mx-auto = center horizontally.
Display & flex utilities
.d-none,.d-block,.d-flex,.d-inline-block(and responsive variants like.d-md-flex)..justify-content-center,.align-items-center,.flex-column,.gap-3for flex layouts.
Real World Example
<div class="p-4 bg-light rounded text-center">
<h2 class="display-6 fw-bold text-primary">Big Title</h2>
<p class="lead text-muted">A short tagline with muted text.</p>
<span class="badge bg-success">New</span>
</div>
Live preview:
Big Title
A short tagline with muted text.
New
primary
secondary
danger
warning
info
dark
Common Mistakes
- Using Bootstrap 4 spacing on the wrong sides. In v5,
l/rbecames/e(start/end) —ml-2is nowms-2. - Forgetting text color contrast.
.bg-warningand.bg-infoare light — pair them with.text-dark. - Writing custom CSS for things a utility already does. Check for a utility first; it keeps markup consistent.
- Overusing utilities until markup is unreadable. For repeated patterns, make a component class instead.
Best Practices
- Memorize the spacing scale (0–5) — it covers almost every gap you need.
- Use responsive utility variants (
d-md-flex,text-lg-end) to adapt without media queries. - Prefer semantic theme colors (
text-dangerfor errors) so meaning stays consistent. - Group related utilities logically in the class list (layout, then spacing, then color) for readability.
Practice Exercise
- Create a card-like box with
p-4, a light background, and rounded corners. - Add a centered
display-5heading and alead text-mutedparagraph. - Add three colored badges using
bg-primary,bg-success, andbg-danger. - Use
d-flex justify-content-center gap-2to lay the badges out in a centered row.
Assignment
Recreate a simple pricing callout using only utilities:
- A centered box with vertical padding and a light background.
- A display heading for the price, a muted "per month" line, and a primary button.
- Make the box full width on mobile and half width (centered with
mx-auto) on large screens.
Hint: combine
col-12 col-lg-6 mx-auto with spacing and text utilities — no custom CSS allowed.Interview Questions
- What is a utility class? — A small class that sets a single CSS property (e.g.
.mt-3sets margin-top), letting you style elements directly in HTML. - What does
px-4do? — Applies horizontal padding (left and right) at size 4. - How do you center a block element horizontally with utilities? —
mx-auto(with a set width), ord-flex justify-content-centeron the parent. - How do the directional utilities differ between Bootstrap 4 and 5? — v5 uses logical sides
s/e(start/end) instead ofl/r. - How do you hide an element only on small screens? —
d-none d-md-blockhides it below md and shows it from md up.
Additional Resources
- getbootstrap.com/docs/5.3/utilities/spacing — the spacing utility reference
- getbootstrap.com/docs/5.3/content/typography — typography classes
- getbootstrap.com/docs/5.3/utilities/colors — color and background utilities