Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 58 ยท Layout

Containers

Learn Bootstrap 5 containers: fixed-width .container, full-width .container-fluid, responsive .container-md, max widths per breakpoint, padding and nesting.

Every Bootstrap layout starts with a container. A container is a wrapper div that does three jobs: it limits how wide your content can grow, centers it horizontally, and adds a little padding on the left and right so text never touches the screen edge. Containers are also the required parent of the grid system you will meet soon, so it is worth understanding them well.

This lesson covers the three container types, the exact widths they produce, how the padding works, and what happens when you nest them.

The default container

.container is a responsive fixed-width container. Its max-width changes at each breakpoint and stays the same between breakpoints:

Screen width Breakpoint .container max-width
under 576px extra small 100% (fills the screen)
576px and up sm 540px
768px and up md 720px
992px and up lg 960px
1200px and up xl 1140px
1400px and up xxl 1320px

So on a phone the container is as wide as the screen. On a laptop it is a centered column of 960 to 1320 pixels. This keeps line lengths readable on big monitors, because text across a 2,000-pixel window is hard to read.

In CSS terms the container is roughly:

CSS
.container {
  width: 100%;
  padding-right: .75rem;  /* half of the 1.5rem gutter */
  padding-left: .75rem;
  margin-right: auto;     /* auto margins center the box */
  margin-left: auto;
}
@media (min-width: 576px) { .container { max-width: 540px; } }
@media (min-width: 768px) { .container { max-width: 720px; } }
/* ...and so on for 992px, 1200px, 1400px */

Make the browser window wider and narrower and watch the colored box jump between fixed widths. The text inside tells you nothing; the box edges do. The light-gray page background lets you see the container's edges.

The default container

Full width: container-fluid

.container-fluid is always width: 100%, at every size. It has the same side padding, but never a max-width. Use it for dashboards, toolbars, maps and any interface that should use the whole window.

Responsive containers: container-{breakpoint}

A responsive container is 100% wide until the chosen breakpoint, and then behaves like the default container. .container-md is full width on xs and sm screens, and becomes a fixed-width box from md (768px) upward.

Available classes: .container-sm, .container-md, .container-lg, .container-xl, .container-xxl. This is useful when a section should look like an app on phones but like a centered page on desktops.

Class Full width below Max-width at and above that breakpoint
.container never "fluid" (fixed from sm) 540 / 720 / 960 / 1140 / 1320
.container-sm 576px 540 / 720 / 960 / 1140 / 1320
.container-md 768px 720 / 960 / 1140 / 1320
.container-lg 992px 960 / 1140 / 1320
.container-xl 1200px 1140 / 1320
.container-xxl 1400px 1320
.container-fluid never always 100%
flowchart LR
  A["Window width"] --> B{"Is it below<br/>the chosen breakpoint?"}
  B -- "yes" --> C["container is 100% wide"]
  B -- "no" --> D["container gets a fixed max-width"]

A responsive container switches from fluid to fixed at its own breakpoint.

The next demo stacks all container types, each with a different colored bar. In a narrow preview they look alike; use a wide window to see the differences. The labels show the current window width, measured with a few lines of JavaScript.

Comparing container types

Padding and the gutter

A container has horizontal padding of half a gutter. The default gutter is 1.5rem (24px), so each side gets .75rem (12px). This padding keeps text off the edge and, together with the negative margins of rows, makes columns line up (you will see the full story in The Grid System and Gutters).

You can add vertical spacing with utilities, because containers have no top or bottom padding by default: py-4 adds 1.5rem above and below. To remove the side padding entirely, use px-0.

Nesting containers

Containers are not meant to be nested. A container inside a container just adds more side padding and a smaller max-width, with no benefit. The right pattern is full-width sections that each hold their own container. A section can have a background color across the whole screen while its content stays aligned:

Full-width bands with centered content

The pattern is: <section class="background-classes"><div class="container">...</div></section>. The background belongs to the outer element, the width limit to the inner one. You will use it in every page project, starting with the landing page.

For a layout that is a "page" on desktop but edge-to-edge on phones, .container-lg or .container-xl often feels better than .container, because the content keeps its comfortable width longer before it shrinks.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. At 1000px window width, what max-width does .container have?

    1000px is at least the lg breakpoint (992px) but below xl (1200px), so .container uses its lg max-width of 960px.
  2. 2. How does .container-md behave on a 600px-wide screen?

    Responsive containers are fluid until their breakpoint. md starts at 768px, so at 600px the container fills the screen.
  3. 3. What is the recommended way to give a full-width section a background color while keeping content aligned?

    The outer element stretches across the window and carries the background; the inner container limits and centers the content.
  4. 4. How much horizontal padding does a container have by default?

    The container pads each side by half a gutter, 0.75rem or 12px at the default root size.
  5. 5. Which container never gets a max-width?

    .container-fluid is always 100% wide.

Summary

  • .container is centered with a max-width that steps up at each breakpoint (540, 720, 960, 1140, 1320 px).
  • .container-fluid is always full width; .container-{sm|md|lg|xl|xxl} are full width until their breakpoint.
  • Containers add 0.75rem side padding; use utilities such as py-4 for vertical space.
  • Do not nest containers; use full-width sections with one container each.

Next: Breakpoints and Responsive Design.