Components I: Buttons, Cards & Navbar
Three of the most-used Bootstrap components, ready to drop into any page.
Introduction
Components are pre-built, pre-styled UI pieces. You add the right classes (and sometimes a data attribute) and Bootstrap handles the appearance and behavior. In this lesson we cover three workhorses: buttons, cards, and the navbar.
Theory
Buttons
- Variants —
.btnplus a color:.btn-primary,.btn-success,.btn-danger, etc. - Outline —
.btn-outline-primarygives a bordered, transparent button. - Sizes —
.btn-lgand.btn-sm. - States — add
disabledor thedisabledattribute.
Cards
A .card is a flexible content container. Common inner pieces: .card-img-top, .card-body, .card-title, .card-text, and .card-footer.
Navbar
The navbar is responsive: it collapses behind a "hamburger" toggle on small screens. Key classes: .navbar, .navbar-expand-lg (when to switch from collapsed to full), .navbar-brand, .navbar-toggler, and .nav-link. The toggle uses data-bs-toggle="collapse".
Real World Example
Buttons, a card, and a working responsive navbar — all rendered live below:
<button class="btn btn-primary">Primary</button>
<button class="btn btn-outline-success">Outline</button>
<div class="card" style="width:18rem">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text.</p>
<a href="#" class="btn btn-primary">Go</a>
</div>
</div>
Buttons:
Card:
Responsive navbar (resize to see it collapse):
Common Mistakes
- Using
.btn-primarywithout.btn. The base.btnclass is required for padding, border-radius, and behavior. - Mismatched toggler target. The navbar toggler's
data-bs-targetmust match the collapse element'sid(with a#). - Forgetting
.navbar-expand-{bp}. Without it the navbar never expands and stays collapsed at all sizes. - Putting card content directly in
.card. Wrap text in.card-bodyso it gets proper padding.
Best Practices
- Use semantic button colors:
btn-dangerfor destructive actions,btn-successfor confirmation. - Use real
<button>elements for actions and<a>for navigation. - Keep navbars inside a container so brand and links align with the rest of your layout.
- Group cards in a grid row (
col-md-4) for responsive card decks.
Practice Exercise
- Create one solid button and one outline button of the same color, side by side.
- Build a card with a title, a sentence of text, and a button in the body.
- Add a navbar with a brand and three links, using
navbar-expand-lg. - Shrink the browser below the lg breakpoint and confirm the navbar collapses to a hamburger.
Assignment
Build the top of a product page:
- A dark navbar with a brand, three nav links, and a right-aligned "Sign in" button.
- A row of three product cards (use the grid) each with a title, price text, and an "Add to cart" button.
- Make the cards stack on mobile and sit three-across on desktop.
ms-auto on the nav list to push the sign-in button to the right edge of the navbar.Interview Questions
- What classes make a basic Bootstrap button? —
.btnplus a variant such as.btn-primary. - How do you make a navbar collapse on mobile? — Use
.navbar-expand-{breakpoint}with a.navbar-togglerthat targets the collapse element viadata-bs-target. - What is the role of
.card-body? — It adds consistent padding around the card's content. - What does
.btn-outline-*produce? — A transparent button with a colored border and text that fills in on hover. - Which Bootstrap part powers the collapsing navbar? — The JavaScript bundle (the Collapse plugin) plus
data-bs-toggle="collapse".
Additional Resources
- getbootstrap.com/docs/5.3/components/buttons — button reference
- getbootstrap.com/docs/5.3/components/card — card reference
- getbootstrap.com/docs/5.3/components/navbar — navbar reference