Bootstrap container and container-fluid: what is the difference?

The Bootstrap container and container-fluid classes

For developing a web project in Bootstrap framework, you need to specify a containing element that houses the Bootstrap grid system. The grid system is used to define the layout of the web pages by using a series of rows and columns.

For the containing element, you may specify either of these two classes:

  • container
  • container-fluid

The difference between these two containing classes is:

The .container class is the fixed width container. That does not mean it is not responsive. It is responsive; however, it is fixed based on screen size. The screen sizes include:

  • xs for extra small devices (used for less than 768 e.g smart phones, mobile etc.)
  • sm for small screens (From 768 pixels and up e.g. tablets)
  • md for the medium screen (>= 992 pixels. Desktops/Laptops)
  • lg for large screens (>= 1200. Pixels e.g. large desktops)

If you use the container class and check the webpage in a browser, it will adjust according to the screen and browser size. For example, if your current browser width is more than 1200, it will adjust to the 1170px wide. If you resize the browser to small size, it will remain same until it reaches 992px. At this point, the container class will adjust to 970px wide.

On the other hand, the container-fluid class will take the full width of the viewport. If you use the container-fluid and resize the browser, you may notice the content inside it will adjust with every pixel to take the full available width.

To make it clearer, have a look at a demo below.

A demo to understand the difference between container and container-fluid Bootstrap

To demonstrate that, I have used the same number of Bootstrap buttons in two rows. The first div is assigned the container class while the second div is given the container-fluid class.

For distinction, I am using an outer div with a non-Bootstrap class. It is just given the border and margin. See the difference initially, as well as, by resizing the screen if you are using a desktop/notebook:

bootstrap container

See online demo and code

The markup for this example:

You can see, the buttons contained in the container class div moves and adjusts after certain resize or as the width of browser changes while buttons used inside the container-fluid div stays the same.