Bootstrap Tutorials: Containers
Tags: bootstrap, tutorial, html, ui
Published: 2024-06-27 08:00:00

## Containers

Containers are the most basic layout element in Bootstrap and are required when using default grid system. Containers are used to contain, pad, and (sometimes) center the content within them. Although containers can be nested, most layouts do not require a nested container.

This is what the container looks like in code:

**HTML**
```
<div class="container">

</div>
```
Nothing special. Just a div with class container.

But now let's check how containers will behave in our Bootstrap project that we started in the previous lesson.

Add the following code to your project, directly below the opening _body_ tag.

**HTML**
```
<div class="container" style="height: 500px; background-color: red">

</div>
```

_**Note:** For demonstration purposes, we added an inline CSS to the container that gives it a height of 500px and a red color._

_This is only to allow you to visually observe the change in the behavior of the container, because by default it would be invisible (by default the container has no color and its height is adjusted to its content - and if there is no content, it has no height). We'll remove this inline CSS later._

After adding this code to your project, save the file and refresh your browser. You should see a red rectangle with white margins on the sides. This is our container. Isn't it beautiful? :)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1swoq7vo3bg5ljf2q82s.png)

**Now slowly reduce the browser window size.** When you get below 576 pixels you will see that the margins are completely gone and the container is 100% of the available width.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3y9y9mnto2saezr5ytat.gif)

This is a very desirable behavior that allows us to create responsive layouts, adapted to both large desktop screens and small ones for mobile devices.

As you can easily guess, margins are needed on large screens, but there is no room for them on small ones - that's why Bootstrap containers adjust their width to the width of the screen.

This boundary point of **576 pixels** (px), below which the margins disappear and the container stretches to full width, is called a **breakpoint**. This is a very important term and we will refer to it often.

_**Breakpoints** are the triggers in Bootstrap for how your layout responsive changes across device or viewport sizes._

Thanks to breakpoints, Bootstrap gives us a lot of flexibility and allows us to decide from what screen width our container (as well as other layout elements, which we will learn later) remove the margins and stretch to full width.

Have a look at the table below:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/55rm3a4y8iq09vuhrw8h.png)

Can you see this parts -sm , -md, -lg etc., added to the container class? They represent breakpoints (sm for small, md for medium, lg for large, etc.) and define below which width the margins are removed and the container begins to stretch the full available width (i.e. 100% of the width given in the table).

The default container (i.e. the container class, without any additional characters) has a breakpoint of 576px wide. If you want to make the container convert to full width on screens less than, for example, 992px wide, you need to add xl fragment to the container class.

Then it should looks like this:

**HTML**
```
<div class="container-xl">

</div>
```

Now in your project change our container to an container with the xl breakpoint and again gradually reduce the width of the screen. You will see the margins disappear much sooner than before.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kvhwjbf70l32nm8y9b17.gif)

**HTML**
```
<div class="container-xl" style="height: 500px; background-color: red">

</div>
```

And if you want your container to stretch to full width always, regardless of the breakpoint (i.e. on both small and large screens), use the container-fluid class.

**HTML**
```
<div class="container-fluid">

</div>
```

And that's it for now when it comes to containers. Wasn't that hard for such a useful thing, don't you think?

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/84d40tyozy5uv250a1t2.gif)
Now change the container in your project to the default one. Leave the inline CSS for now, as we'll need it in the next lesson.

**HTML**
```
<div class="container" style="height: 500px; background-color: red">

</div>
```

And we're done! :)