Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Bootstrap5 Containers

Bootstrap5 Containers

In the previous chapter, we learned that Bootstrap needs a container element to wrap the website's content.

We can use the following two container classes:


Fixed Width

The .container class is used to create fixed-width responsive pages.

**Note:** The width (max-width) scales up or down proportionally according to the screen width.

| | Extra Small Screen
<576px | Small Screen
≥576px | Medium Screen
≥768px | Large Screen
≥992px | Extra Large Screen
≥1200px | Extra Extra Large Screen
≥1400px | | --- | --- | --- | --- | --- | --- | --- | | max-width | 100% | 540px | 720px | 960px | 1140px | 1320px |

In the following example, we can try resizing the browser window to see how the container width changes across different screens:

Bootstrap5 .container Example

<div class="container">
<h1>

My First Bootstrap Page

h1

<

p

This is some text.

p

div

Try it »

**Note:** Extra extra large screens (≥1400px) are newly added in Bootstrap 5; the largest in Bootstrap 4 is extra large screens (≥1200px).


100% Width

The .container-fluid class is used to create a full-screen-size container that always spans the entire screen width (width is always 100%):

Bootstrap5 .container-fluid Example

<div class="container-fluid">
<h1>

My First Bootstrap Page

h1

<

p

Uses .container-fluid, a 100% width container that occupies the full viewport.

p

div

Try it »


Container Padding

By default, containers have left and right padding, with no top or bottom padding. Bootstrap provides some spacing classes for filling margins. For example, .pt-5 is used to fill the top padding:

Bootstrap5 Example

<div class=">

container pt-5

"

div

Try it »


Container Borders and Colors

Bootstrap also provides some border and color (bg-dark, bg-primary, etc.) classes for setting container styles:

Bootstrap5 Example

<div class=">

container p-5 my-5 border

"

div

<div class=">

container p-5 my-5 bg-dark text-white

"

div

<div class=">

container p-5 my-5 bg-primary text-white

"

div

Try it »

We will explain these styles in detail in later chapters.


Responsive Containers

You can use the .container-sm|md|lg|xl classes to create responsive containers.

The container's max-width property value changes according to the screen size.

| Class | Extra Small Screen
<576px | Small Screen
≥576px | Medium Screen
≥768px | Large Screen
≥992px | Extra Large Screen
≥1200px | Extra Extra Large Screen
≥1400px | | --- | --- | --- | --- | --- | --- | --- | | .container-sm | 100% | 540px | 720px | 960px | 1140px | 1320px | | .container-md | 100% | 100% | 720px | 960px | 1140px | 1320px | | .container-lg | 100% | 100% | 100% | 960px | 1140px | 1320px | | .container-xl | 100% | 100% | 100% | 100% | 1140px | 1320px | | .container-xxl | 100% | 100% | 100% | 100% | 100% | 1320px |

Bootstrap5 Example

<div class="container-sm" .container-sm div>
<div class="container-md" .container-md div>
<div class="container-lg" .container-lg div>
<div class="container-xl" .container-xl div>
<div class="container-xxl" .container-xxl div>

Try it »