Bootstrap Basics
Bootstrap basics are the handful of building blocks every Bootstrap 5 page relies on: the required page setup, containers, utility classes for spacing and colour, and ready-made components such as buttons and alerts. Once you know these, you can assemble a clean, responsive layout without writing much custom CSS, and you will understand what each class in someone else's Bootstrap markup is doing.
The page setup Bootstrap expects
Bootstrap needs an HTML5 doctype and the responsive viewport meta tag. Without the viewport tag, phones render the page at desktop width and shrink it, so breakpoints never kick in. You then link the CSS in the head and the JavaScript bundle before the closing body tag. The bundle includes Popper, which dropdowns and tooltips need.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap starter</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container py-4">
<h1 class="display-6 mb-3">Welcome</h1>
<p class="lead text-secondary">A short intro styled with utility classes.</p>
<div class="alert alert-info" role="alert">Your profile is 80% complete.</div>
<button type="button" class="btn btn-primary me-2">Save</button>
<button type="button" class="btn btn-outline-secondary">Cancel</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Containers hold everything together
A container centres your content and adds horizontal padding. .container has a fixed maximum width that changes at each breakpoint. .container-fluid always spans the full width. Responsive variants such as .container-md stay full width until the medium breakpoint, then become fixed. Grids, which come next, must live inside a container.
Utility classes do most of the styling
Bootstrap 5 leans heavily on small single-purpose classes. Learning their naming pattern pays off quickly:
- Spacing:
mfor margin,pfor padding, then a side (t,b,sfor start,efor end,x,y) and a size from 0 to 5. Somt-3is top margin andpx-2is left and right padding. - Colour:
text-primary,bg-light,text-bg-darkand similar, based on theme names like primary, success, danger and warning. - Display and flex:
d-none,d-md-block,d-flex,justify-content-between,align-items-center. - Typography:
fw-bold,fs-5,text-center,text-truncate.
Components come with a base class and modifiers
Most components follow the same recipe: a base class plus a modifier. A button needs btn and then a style like btn-success or btn-outline-danger. Sizes are added with btn-sm or btn-lg. Alerts, badges and cards work the same way. Interactive components such as modals and collapses are switched on with data-bs- attributes, for example data-bs-toggle="modal", so you often need no JavaScript of your own.
Mistakes beginners make with the basics
The classic one is using Bootstrap 4 class names. Bootstrap 5 renamed left and right utilities to start and end, so ml-2 became ms-2 and mr-auto became me-auto. Data attributes also gained a bs prefix: data-toggle no longer works, you need data-bs-toggle. Tutorials written for the older version will silently fail.
Another frequent issue is loading bootstrap.min.js instead of the bundle and then wondering why dropdowns do nothing. Either use bootstrap.bundle.min.js or load Popper separately. Bootstrap 5 also no longer needs jQuery.
Key points
Start every page with the viewport tag, wrap content in a container, style with utility classes before writing custom CSS, and use the base-plus-modifier pattern for components. Double-check that the class names you copy are Bootstrap 5 names.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.