Bootstrap 5 Utilities
Utilities are single-purpose classes: each one does exactly one thing, like mt-3 (margin-top) or text-center. Combining them lets you style and lay out almost anything without writing CSS. Many have responsive versions with a breakpoint infix, e.g. d-md-none.
Spacing: margin and padding
The pattern is {property}{sides}-{size}, or {property}{sides}-{breakpoint}-{size} for responsive spacing.
| Part | Values |
|---|---|
| Property | m (margin), p (padding) |
| Sides | t top, b bottom, s start, e end, x left+right, y top+bottom, (none) all |
| Size | 0 = 0, 1 = .25rem, 2 = .5rem, 3 = 1rem, 4 = 1.5rem, 5 = 3rem, auto (margin only) |
Negative margins (mt-n2) are disabled by default. gap-* sets the gap in flex and grid containers.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<div class="bg-primary-subtle border mb-2"><div class="bg-primary text-white p-1">p-1</div></div>
<div class="bg-primary-subtle border mb-2"><div class="bg-primary text-white p-3">p-3</div></div>
<div class="bg-primary-subtle border mb-2"><div class="bg-primary text-white px-5 py-2 ms-4">px-5 py-2 ms-4</div></div>
<div class="bg-warning-subtle border"><div class="bg-warning w-50 mx-auto p-2 text-center">w-50 mx-auto (centred)</div></div>
</body>
</html>Borders, rounding and shadows
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>.demo { width: 80px; height: 60px; display: grid; place-items: center; background: var(--bs-tertiary-bg); font-size: .75rem; }</style>
</head>
<body class="p-3">
<div class="d-flex flex-wrap gap-3">
<div class="demo border">border</div>
<div class="demo border border-3 border-danger">border-3</div>
<div class="demo border-top border-bottom border-primary">top+bottom</div>
<div class="demo border rounded-4">rounded-4</div>
<div class="demo border rounded-circle">circle</div>
<div class="demo border rounded-pill">pill</div>
<div class="demo shadow-sm">shadow-sm</div>
<div class="demo shadow">shadow</div>
<div class="demo shadow-lg">shadow-lg</div>
</div>
</body>
</html>Display and visibility
d-{value} sets display; add a breakpoint to change it responsively. This is how you show or hide things on certain screen sizes.
| Class | Effect |
|---|---|
d-none |
Hidden |
d-block, d-inline, d-inline-block |
Block / inline / inline-block |
d-flex, d-inline-flex, d-grid |
Flex / grid containers |
d-md-none |
Hidden from md up |
d-none d-lg-block |
Hidden below lg, shown from lg up |
d-print-none |
Hidden when printing |
invisible / visible |
visibility (keeps its space) |
visually-hidden |
Hidden on screen, available to screen readers |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<p>This frame is currently:
<span class="badge text-bg-danger d-sm-none">xs (<576px)</span>
<span class="badge text-bg-warning d-none d-sm-inline d-md-none">sm</span>
<span class="badge text-bg-success d-none d-md-inline d-lg-none">md</span>
<span class="badge text-bg-info d-none d-lg-inline d-xl-none">lg</span>
<span class="badge text-bg-primary d-none d-xl-inline d-xxl-none">xl</span>
<span class="badge text-bg-dark d-none d-xxl-inline">xxl</span>
</p>
<div class="p-2 border mb-2 d-md-none">📱 I'm only visible below md.</div>
<div class="p-2 border mb-2 d-none d-md-block">🖥 I'm only visible from md up.</div>
<script>console.log('frame width:', window.innerWidth + 'px');</script>
</body>
</html>Note: Resize your browser window (the frame follows the lesson column's width) and run the example again to see the badge change.
Sizing, position and more
| Group | Examples |
|---|---|
| Width / height | w-25, w-50, w-75, w-100, w-auto, h-100, mw-100, vh-100, min-vh-100 |
| Position | position-relative, position-absolute, position-fixed, position-sticky, top-0, start-50, bottom-0, end-0, translate-middle |
| Fixed helpers | fixed-top, fixed-bottom, sticky-top |
| Overflow | overflow-auto, overflow-hidden, overflow-x-auto |
| Opacity | opacity-25, opacity-50, opacity-75, opacity-100 |
| Interaction | user-select-all, user-select-none, pe-none (pointer-events) |
| Vertical align | align-baseline, align-middle, align-top (inline and table cells) |
| Float | float-start, float-end, float-none, clearfix |
| Z-index | z-0, z-1, z-2, z-3, z-n1 |
| Aspect ratio | ratio ratio-16x9, ratio-4x3, ratio-1x1 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<div class="position-relative bg-body-tertiary border rounded" style="height: 140px;">
<span class="position-absolute top-0 start-0 badge text-bg-primary m-1">top-0 start-0</span>
<span class="position-absolute top-0 end-0 badge text-bg-success m-1">top-0 end-0</span>
<span class="position-absolute top-50 start-50 translate-middle badge text-bg-danger">centre</span>
<span class="position-absolute bottom-0 start-0 badge text-bg-dark m-1 opacity-50">opacity-50</span>
<span class="position-absolute bottom-0 end-0 m-1 small user-select-all border bg-white px-1">click selects all</span>
</div>
<div class="ratio ratio-21x9 mt-2 rounded" style="background: linear-gradient(90deg, #7952b3, #0dcaf0); max-width: 300px;">
<div class="d-flex align-items-center justify-content-center text-white">ratio-21x9</div>
</div>
</body>
</html>Tip: Utilities use
!importantso they always win over component styles. That's intentional: when you writemt-0you mean it. If you find yourself adding ten utilities to many elements, consider a small custom CSS class instead.
Test yourself
What does px-3 mean?
- 3px padding
- Padding left and right of 1rem
- Padding top and bottom of 1rem
- Margin left and right
Which classes show an element only from the lg breakpoint up?
- .d-lg-block
- .d-none .d-lg-block
- .d-lg-none
- .visible-lg
What is the difference between .invisible and .d-none?
- None
- .invisible keeps the element's space; .d-none removes it from layout
- .d-none keeps the space
- .invisible hides it from screen readers only