Bootstrap 5 Containers
A container is the outermost layout box in Bootstrap. It adds horizontal padding and, depending on the type, limits and centres the content. The grid system (next lesson) must live inside a container.
The three kinds of container
| Class | Width | Use it for |
|---|---|---|
.container |
Fixed max-width that steps up at each breakpoint; full width below 576px |
Classic centred page layouts |
.container-fluid |
Always 100% wide | Dashboards, full-bleed sections |
.container-{bp} |
100% wide until the breakpoint, then behaves like .container |
Mixed behaviour, e.g. .container-md |
Fixed vs fluid
Run the example and compare the two coloured boxes. The fixed container stops growing at its breakpoint width and is centred; the fluid one always touches the edges (minus its padding).
<!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="bg-light py-3">
<div class="container bg-primary-subtle border border-primary py-3 mb-3">
<strong>.container</strong> — fixed max-width, centred
</div>
<div class="container-fluid bg-success-subtle border border-success py-3">
<strong>.container-fluid</strong> — always 100% wide
</div>
</body>
</html>The max-widths
.container uses these maximum widths. Note how each is a bit smaller than its breakpoint, which leaves a margin on both sides.
| Viewport width | .container |
.container-sm |
.container-md |
.container-lg |
.container-xl |
.container-xxl |
.container-fluid |
|---|---|---|---|---|---|---|---|
| < 576px | 100% | 100% | 100% | 100% | 100% | 100% | 100% |
| ≥ 576px | 540px | 540px | 100% | 100% | 100% | 100% | 100% |
| ≥ 768px | 720px | 720px | 720px | 100% | 100% | 100% | 100% |
| ≥ 992px | 960px | 960px | 960px | 960px | 100% | 100% | 100% |
| ≥ 1200px | 1140px | 1140px | 1140px | 1140px | 1140px | 100% | 100% |
| ≥ 1400px | 1320px | 1320px | 1320px | 1320px | 1320px | 1320px | 100% |
Note: The preview frame is only as wide as the lesson column, so you'll probably see the 540px or 720px step. Resize your browser window and run the example again to watch the container jump between widths.
Responsive containers
A responsive container such as .container-lg stays fluid on small screens and becomes fixed from its breakpoint upward. The example prints the container's current width with a few lines of JavaScript so you can see which rule applies.
<!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="py-3">
<div class="container-sm bg-warning-subtle py-2 mb-2" id="a">.container-sm</div>
<div class="container-lg bg-info-subtle py-2 mb-2" id="b">.container-lg</div>
<div class="container-xxl bg-danger-subtle py-2" id="c">.container-xxl</div>
<script>
console.log('frame width:', window.innerWidth + 'px');
for (const id of ['a', 'b', 'c']) {
const el = document.getElementById(id);
console.log(el.textContent, '→', el.offsetWidth + 'px');
}
</script>
</body>
</html>Padding and colours
Containers come with padding-left/padding-right of 0.75rem (the grid's half-gutter). Everything else is up to you: combine containers with utility classes for spacing, colour and borders.
<!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>
<div class="container-fluid bg-dark text-white py-2">Full-width header bar</div>
<div class="container p-4 my-3 border rounded-3 shadow-sm">
<h4>Article</h4>
<p class="mb-0">A centred container with padding (<code>p-4</code>), vertical margin (<code>my-3</code>), a border and rounded corners.</p>
</div>
</body>
</html>| Utility | Effect |
|---|---|
p-0 … p-5 |
Padding on all sides |
py-* / px-* |
Vertical / horizontal padding |
my-* / mt-* / mb-* |
Vertical / top / bottom margin |
bg-*, text-* |
Background and text colour |
border, rounded-*, shadow-* |
Borders, rounding, shadows |
Tip: You can nest containers, but you rarely need to. A common layout is a full-width coloured band (
container-fluidor a plain<section>) with a.containerinside it to keep the text centred.
Test yourself
Which container is always 100% wide?
- .container
- .container-xl
- .container-fluid
- .container-full
On a 900px wide viewport, how wide is .container-lg?
- 720px
- 960px
- 100%
- 540px
What is the max-width of .container at 1200px and above (below 1400px)?
- 1140px
- 1200px
- 960px
- 1320px