Skip to content

Bootstrap Containers

Containers are the outermost wrapper of a Bootstrap layout. This lesson explains the fixed, fluid, and responsive container variants and how they control page width and padding.

Bootstrap Containers Overview

A container centers your content horizontally and applies responsive left and right padding so content never touches the edge of the viewport. Bootstrap ships three container types: .container has a max-width that changes at each breakpoint, .container-fluid always spans 100% of the viewport width, and .container-{breakpoint} is fluid until it reaches that breakpoint, then becomes fixed.

Containers do not create a grid by themselves; you still nest a .row and .col-* elements inside them. Choosing the right container type is usually the first layout decision you make on any new page or section.

Concept Description Common Usage
.container Fixed max-width that changes per breakpoint Standard page content wrapper
.container-fluid Always 100% width with side padding Full-width sections, dashboards
.container-{bp} Fluid below breakpoint, fixed above it Full-width on mobile, centered on desktop
Responsive padding px-* utilities applied automatically Consistent edge spacing at every size
Nesting containers Rare pattern, generally avoided Rebuilding max-width inside a fluid section

Bootstrap Containers Example

<div class="container">
  <p>Fixed-width, centered content with responsive max-width.</p>
</div>

<div class="container-fluid bg-light">
  <p>Always full width, useful for banners or full-bleed sections.</p>
</div>

<div class="container-md">
  <p>Fluid until the md breakpoint, then becomes fixed-width.</p>
</div>

The first container caps width at each breakpoint's max-width value. The container-fluid spans the full viewport regardless of screen size. The container-md example stays full width on small screens but locks to a fixed width once the viewport reaches md.

Top 10 Bootstrap Containers Examples

These are the container variants and patterns you'll use across nearly every Bootstrap page.

# Example Syntax
1 Standard fixed container <div class="container">...</div>
2 Full-width fluid container <div class="container-fluid">...</div>
3 Fluid until sm <div class="container-sm">...</div>
4 Fluid until md <div class="container-md">...</div>
5 Fluid until lg <div class="container-lg">...</div>
6 Fluid until xl <div class="container-xl">...</div>
7 Fluid until xxl <div class="container-xxl">...</div>
8 Container with vertical padding <div class="container py-5">...</div>
9 Container with background color <div class="container bg-body-secondary p-4">...</div>
10 Nested fixed container in fluid section <div class="container-fluid"><div class="container">...</div></div>

Best Practices

  • Use .container for typical marketing and content pages that benefit from a max-width.
  • Use .container-fluid for dashboards, banners, or app shells that should fill the viewport.
  • Reach for .container-{breakpoint} when you want full width on mobile but centered on desktop.
  • Add vertical spacing utilities like py-5 directly on the container instead of extra wrapper divs.
  • Avoid nesting a .container inside another .container; nest inside .container-fluid instead if needed.
  • Keep container choice consistent across similar page types for visual rhythm.
  • Combine containers with background utilities to create full-bleed color sections.

Common Mistakes

  • Expecting .container-fluid to have the same max-width behavior as .container.
  • Forgetting that a container alone does not create a grid without a row and columns.
  • Nesting two .container elements directly, which doubles the horizontal padding.
  • Using inline styles to fake a max-width instead of the built-in container classes.
  • Applying container classes to elements that should stay full width, like a fixed navbar background.
  • Not testing how container padding interacts with custom CSS margins.

Key Takeaways

  • Containers set horizontal padding and, optionally, a responsive max-width.
  • .container is fixed-width per breakpoint; .container-fluid is always full width.
  • .container-{breakpoint} blends both behaviors at a chosen breakpoint.
  • Rows and columns must be nested inside a container to form a grid.
  • Container choice is usually the first layout decision on a new page.

Pro Tip

When a design calls for a full-width background with centered content, wrap a .container inside a .container-fluid rather than fighting the grid with custom CSS.