1Bootstrap UI Components and Styling
▶
Bootstrap is one of the most widely adopted front-end frameworks in the world, and for good reason: it provides a comprehensive library of pre-built UI components, a powerful responsive grid system, and an extensive set of utility classes that allow developers to build polished, professional interfaces without writing large amounts of custom CSS. Rather than reinventing the wheel for every project, Bootstrap lets you compose sophisticated layouts and interactive elements using well-tested, accessible patterns. This topic walks through the most essential Bootstrap components and utilities — navigation bars, buttons, modals, cards, utility classes, and the responsive grid — explaining how each works and demonstrating how they fit together in real-world markup.
Bootstrap 5 (the current major version) drops the jQuery dependency and relies on its own lightweight JavaScript bundle. You include it via a CDN link in your HTML <head> for the CSS and just before </body> for the JS bundle, or you install it through a package manager like npm. Everything described below applies to Bootstrap 5 unless otherwise noted.
Navigation Bars with Bootstrap
A navigation bar is almost always the first component a visitor sees. Bootstrap's navbar system gives you a fully responsive, accessible header in a handful of classes. The foundation is a <nav> element with the navbar class applied. On its own, navbar sets up flexbox alignment and some padding. You then add navbar-expand-{breakpoint} — for example navbar-expand-lg — to tell Bootstrap at which viewport width the navbar should switch from a collapsed mobile view to a fully expanded desktop view.
Inside the <nav> element, two key sub-components appear most often. The brand element uses the class navbar-brand and is typically an <a> tag wrapping either a text logo or an <img> tag. The navigation links live inside a <ul> with the class navbar-nav. Each list item receives nav-item and the anchor inside it receives nav-link. An active page is indicated by adding the active class and the ARIA attribute aria-current="page" to the relevant nav-link.
The collapse behavior — the hamburger menu on small screens — requires two more pieces. A <button> with class navbar-toggler acts as the toggle trigger. It carries the data attributes data-bs-toggle="collapse" and data-bs-target="#navbarMenu" (where the value matches the id of the collapsible wrapper). The collapsible wrapper itself is a <div> with both collapse and navbar-collapse classes plus the matching id. Bootstrap's JavaScript listens for clicks on the toggler and toggles the show class on the wrapper automatically — no custom JavaScript needed.
Bootstrap also ships theme helpers for navbars. Adding navbar-dark and a background utility like bg-dark produces a dark bar with light text. Using navbar-light with bg-light gives you the inverse. You can also apply any bg-* utility or even a custom background color.
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container">
<!-- Brand / Logo -->
<a class="navbar-brand" href="#">MyApp</a>
<!-- Hamburger toggler (visible on small screens) -->
<button class="navbar-toggler" type="button"
data-bs-toggle="collapse"
data-bs-target="#mainNav"
aria-controls="mainNav"
aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<!-- Collapsible link list -->
<div class="collapse navbar-collapse" id="mainNav">
<ul class="navbar-nav ms-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
</ul>
</div>
</div>
</nav>
Notice the utility class ms-auto on the <ul>. This is a spacing utility that pushes the link group to the right side of the flex container by applying margin-inline-start: auto — a common technique for right-aligning nav links. The classes mb-2 mb-lg-0 remove bottom margin on large screens while providing small-screen spacing.
Bootstrap Button Styles
Buttons are among the most frequently used interactive elements in any interface, and Bootstrap provides a complete system for styling them consistently. Every Bootstrap button requires the btn base class, which sets the cursor, padding, border-radius, font size, and transition behavior. On its own, btn renders an unstyled button; you pair it with a contextual variant to apply color meaning.
The standard contextual variants are named after Bootstrap's theme colors:
| Class | Background Color | Typical Use |
|---|---|---|
btn-primary |
Blue (default) | Main call-to-action |
btn-secondary |
Gray | Alternative or cancel actions |
btn-success |
Green | Confirm, save, submit |
btn-danger |
Red | Delete, remove, destructive actions |
btn-warning |
Yellow/Amber | Caution, proceed with care |
btn-info |
Cyan | Informational, neutral actions |
btn-light |
White/Light gray | Subtle actions on dark backgrounds |
btn-dark |
Dark gray/Black | Prominent actions on light backgrounds |
Size variants let you scale a button up or down without writing any custom CSS. Adding btn-lg increases padding and font size, while btn-sm reduces them. This is especially useful when buttons appear inside compact UI elements like cards or table rows.
<!-- Standard sizes -->
<button class="btn btn-primary btn-lg">Large Primary</button>
<button class="btn btn-secondary">Default Secondary</button>
<button class="btn btn-danger btn-sm">Small Danger</button>
The outline variants (btn-outline-*) swap the filled background for a transparent one, keeping only a colored border and matching text. This is ideal for secondary actions you want to de-emphasize visually without making them invisible. On hover, the full color fills in, providing clear interactive feedback.
<button class="btn btn-outline-primary">Outline Primary</button>
<button class="btn btn-outline-danger">Outline Danger</button>
Buttons can be applied not just to <button> elements but also to <a> tags and <input type="submit"> elements. When applying to an <a> tag, add role="button" for accessibility. Disabled states are achieved with the HTML disabled attribute on real buttons or by adding the disabled class and aria-disabled="true" on anchor-based buttons.
Modal Dialogs
Modals are overlay dialogs that appear on top of the main page content, drawing focus to a specific task or piece of information without navigating away from the page. Bootstrap's modal component handles the overlay backdrop, focus trapping, keyboard dismissal (Escape key), and scroll locking — all behaviors that are tedious to implement correctly from scratch.
The structural anatomy of a Bootstrap modal has two outer layers. The outermost element is a <div> with the class modal and an id that will be used as the trigger target. It also typically receives tabindex="-1" and ARIA attributes for screen-reader accessibility. Inside it sits .modal-dialog, which controls sizing and positioning. Inside that is .modal-content, the visible white box, which in turn contains three semantic sections:
.modal-header— typically contains a title (.modal-title) and a close button withdata-bs-dismiss="modal"..modal-body— the main content area, which can hold any HTML..modal-footer— usually contains action buttons like "Save" and "Cancel".
To trigger a modal from a button or link anywhere on the page, add two data attributes to the trigger element: data-bs-toggle="modal" tells Bootstrap's JS that clicking this element should open a modal, and data-bs-target="#myModal" specifies which modal by matching its id.
<!-- Trigger button -->
<button class="btn btn-primary"
data-bs-toggle="modal"
data-bs-target="#confirmModal">
Open Modal
</button>
<!-- Modal markup -->
<div class="modal fade" id="confirmModal" tabindex="-1"
aria-labelledby="confirmModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="confirmModalLabel">Confirm Action</h5>
<button type="button" class="btn-close"
data-bs-dismiss="modal"
aria-label="Close"></button>
</div>
<div class="modal-body">
<p>Are you sure you want to proceed? This action cannot be undone.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary"
data-bs-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-danger">Yes, Delete</button>
</div>
</div>
</div>
</div>
The fade class on the outer modal adds a CSS transition so the modal fades in and out smoothly. Without it, the modal appears and disappears instantly.
The .modal-dialog-centered modifier class vertically centers the dialog in the viewport, which is generally more visually appealing than the default top-of-screen positioning. Size modifiers adjust the dialog's maximum width: modal-sm produces a narrow 300px dialog, modal-lg produces a wider 800px dialog, and modal-xl extends to 1140px. For very tall content, modal-dialog-scrollable makes only the body area scroll while keeping the header and footer fixed.
Card Components
Cards are versatile content containers used to group related information — think product listings, blog post previews, user profiles, or dashboard widgets. Bootstrap's card component is a flexible box with optional sections for images, headers, footers, and body content.
The minimum card structure is a <div class="card"> containing a <div class="card-body">. Inside the body, .card-title (usually an <h5>) and .card-text (usually a <p>) establish a clear typographic hierarchy. The card-body also commonly holds buttons, links styled with card-link, or any other inline content.
<div class="card" style="width: 18rem;">
<img src="product.jpg" class="card-img-top" alt="Product image">
<div class="card-body">
<h5 class="card-title">Wireless Headphones</h5>
<p class="card-text">
Premium noise-cancelling headphones with 30-hour battery life.
</p>
<a href="#" class="btn btn-primary">View Product</a>
</div>
<div class="card-footer text-muted">
Last updated 3 days ago
</div>
</div>
The .card-img-top class on the <img> element places the image at the top of the card with rounded top corners matching the card's border radius. Using .card-img-bottom places it at the bottom with rounded bottom corners instead. Bootstrap ensures the image fills the card's width while maintaining its natural aspect ratio. If you need a full-bleed image with text overlaid, the .card-img-overlay class positions text content absolutely over a .card-img background image.
The .card-footer sits at the bottom of the card, visually separated by a subtle border-top. It is the right place for timestamps, status labels, or secondary action links. Pairing it with a utility like text-muted keeps it visually subordinate to the main card content.
Cards integrate naturally with Bootstrap's grid. Wrapping multiple cards in a .row and placing each in a .col-md-4 column creates a three-up card grid on medium screens and above that collapses to a single column on small screens. The row-cols-* shorthand classes (row-cols-1 row-cols-md-3) make this even more concise.
Bootstrap Utility Classes
Utility classes are single-purpose CSS classes that do one specific thing. Instead of writing custom CSS rules for common needs like padding, margin, text color, or display mode, you apply the appropriate utility class directly in your HTML. This approach, often called "utility-first" styling, significantly reduces the amount of custom CSS a project needs and keeps styling decisions visible right in the markup.
Spacing utilities — covering margin and padding — follow a consistent naming pattern: {property}{side}-{size}. The property is either m (margin) or p (padding). The side is optional: t (top), b (bottom), s (start/left), e (end/right), x (horizontal, both sides), y (vertical, both sides), or omitted for all four sides. The size is a number from 0 to 5, where 0 removes spacing entirely and 5 applies the largest amount (3rem by default). There is also a special value auto for margins, useful for centering block elements or pushing flex children apart.
| Class | Effect |
|---|---|
m-0 |
Remove all margins |
mt-3 |
Margin-top: 1rem |
mb-5 |
Margin-bottom: 3rem |
mx-auto |
Horizontal margin auto (centers block element) |
p-4 |
Padding on all sides: 1.5rem |
px-2 |
Horizontal padding: 0.5rem |
py-1 |
Vertical padding: 0.25rem |
Color utilities apply Bootstrap's theme colors to text and backgrounds. text-primary, text-secondary, text-success, text-danger, and so on set the foreground color. text-muted applies a lighter gray for de-emphasized content. The background equivalents are bg-primary, bg-light, bg-dark, bg-white, and so on. These utility colors are tied to Bootstrap's CSS custom properties (variables), so if you customize the theme (for example changing the primary color to your brand's purple), all utility classes update automatically.
Display utilities let you change how an element is rendered in the document flow. d-none hides an element entirely (equivalent to display: none). d-block, d-inline, d-inline-block, d-flex, and d-grid switch the element to those display modes. Breakpoint-aware versions are extremely powerful: d-none d-md-block hides something on small screens and shows it as a block on medium screens and above, with no custom media queries needed.
Text utilities handle alignment, weight, style, and transformation. text-center, text-start, and text-end control horizontal alignment. fw-bold, fw-semibold, and fw-light adjust font weight. fst-italic applies italic style. text-uppercase, text-lowercase, and text-capitalize transform letter casing via CSS (the underlying HTML text is unchanged). text-truncate clips overflowing text with an ellipsis, useful inside fixed-width containers.
<p class="text-center fw-bold text-primary mt-4">
Welcome to MyApp
</p>
<span class="text-muted fst-italic">Last edited 2 hours ago</span>
<div class="d-flex justify-content-between align-items-center p-3 bg-light">
<span class="fw-semibold">Total:</span>
<span class="text-success fs-4">$49.99</span>
</div>
Responsive Grid and Component Integration
The Bootstrap grid is the backbone of page layout. It is built on CSS Flexbox and organized around a 12-column system with five responsive breakpoints. Understanding how the grid works enables you to compose every other Bootstrap component into a coherent, responsive page.
Every grid layout starts with a container. The .container class creates a centered, fixed-width wrapper whose maximum width steps up at each breakpoint. The .container-fluid class stretches to 100% of the viewport width at all times, adding only horizontal padding. There are also intermediate options like .container-md, which is fluid on small screens but becomes fixed-width at the medium breakpoint and above.
Inside a container, you create a row with .row. A row is a flexbox container with negative horizontal margins that compensate for the gutter padding on columns. Inside a row, you place column elements using .col-* classes. The number after col- specifies how many of the 12 columns that element should span. If all columns in a row add up to 12, they fill the full width evenly. If you omit the number and use just .col, Bootstrap distributes space equally among all .col siblings automatically.
The breakpoint infix makes columns responsive. .col-md-6 means "span 6 columns (half width) on medium screens and larger." On screens narrower than the medium breakpoint, that column reverts to full width (spanning all 12 columns). You can stack multiple breakpoint classes on a single element to describe different widths at different sizes: class="col-12 col-sm-6 col-lg-4" means full-width on extra-small, half-width on small and medium, one-third-width on large and above.
| Breakpoint | Infix | Minimum Width | Container Max Width |
|---|---|---|---|
| Extra small | none | <576px | 100% (fluid) |
| Small | sm |
≥576px | 540px |
| Medium | md |
≥768px | 720px |
| Large | lg |
≥992px | 960px |
| Extra large | xl |
≥1200px | 1140px |
| Extra extra large | xxl |
≥1400px | 1320px |
The grid integrates directly with all other Bootstrap components. For example, you can place a navbar inside a .container for consistent edge alignment, lay out a row of cards using column classes, and use spacing utilities to fine-tune gaps between rows. The following example shows a realistic integration: a page section with a heading and three equal-width cards that collapse to one column on mobile.
<section class="py-5 bg-light">
<div class="container">
<p class="text-center fw-bold fs-2 mb-4">Our Features</p>
<div class="row g-4">
<div class="col-12 col-md-4">
<div class="card h-100">
<div class="card-body">
<h5 class="card-title">Fast Performance</h5>
<p class="card-text">
Optimized for speed so your users never have to wait.
</p>
</div>
<div class="card-footer text-muted">Core feature</div>
</div>
</div>
<div class="col-12 col-md-4">
<div class="card h-100">
<div class="card-body">
<h5 class="card-title">Secure by Default</h5>
<p class="card-text">
End-to-end encryption and best-practice security baked in.
</p>
</div>
<div class="card-footer text-muted">Core feature</div>
</div>
</div>
<div class="col-12 col-md-4">
<div class="card h-100">
<div class="card-body">
<h5 class="card-title">Easy Integration</h5>
<p class="card-text">
Connects with your existing tools through a clean REST API.
</p>
</div>
<div class="card-footer text-muted">Core feature</div>
</div>
</div>
</div>
</div>
</section>
The g-4 class on the row is a gutter utility — a Bootstrap 5 feature that adds consistent spacing between columns both horizontally and vertically, without requiring additional margin utilities on individual cards. h-100 on each card makes cards in the same row stretch to equal height regardless of content length, creating a clean visual alignment. This combination of grid, card, and utility classes produces a production-ready feature section in pure HTML with no custom CSS.
Taken together, these Bootstrap components and utilities form a composable toolkit. The navbar keeps users oriented and provides global navigation. Buttons communicate available actions. Modals capture focused user attention for confirmations or forms. Cards organize discrete content items. Utility classes handle the typography, spacing, and color details that tie everything together. And the responsive grid ensures the whole interface adapts gracefully from mobile phones to wide desktop monitors — all with a minimal amount of hand-written CSS.