Lesson 7 of 60 – Bootstrap Containers
12%

Bootstrap Containers

Bootstrap containers are used to contain, center, and provide horizontal padding for the content of a website. Containers are one of the most important building blocks of the Bootstrap layout system.

They are commonly used together with Bootstrap rows and columns to create responsive page layouts.

Note: Bootstrap provides .container, .container-fluid, and responsive container classes for controlling the width of content.

1. What is a Bootstrap Container?

A Bootstrap container is a wrapper used to hold website content. It provides horizontal padding and controls the maximum width of content at different responsive breakpoints.

<div class="container">
    My Website Content
</div>

2. Why Use Containers?

Containers help keep website content organized and readable. They also make it easier to create responsive layouts.

  • Controls content width
  • Adds horizontal padding
  • Centers content on larger screens
  • Works with the Bootstrap grid
  • Supports responsive layouts

3. The .container Class

The .container class creates a responsive fixed-width container. Its maximum width changes at Bootstrap's responsive breakpoints.

<div class="container">

    <h1>
        Welcome
    </h1>

    <p>
        This content is inside a container.
    </p>

</div>

4. Container is Responsive

A standard Bootstrap container changes its maximum width at different screen sizes.

This allows content to remain readable on large screens while still working properly on smaller devices.

5. The .container-fluid Class

The .container-fluid class creates a container that uses the full available width of the viewport.

<div class="container-fluid">

    Full Width Content

</div>

6. Container vs Container-Fluid

Class Purpose
.container Responsive maximum-width container
.container-fluid Full-width container

7. Basic Container Example

<div class="container">

    <h1 class="text-primary">
        My Website
    </h1>

    <p>
        Learn Bootstrap step by step.
    </p>

</div>

The content is placed inside a responsive Bootstrap container.

8. Container with Background

<div class="container bg-light p-4">

    <h2>
        Bootstrap Container
    </h2>

    <p>
        This container has a background.
    </p>

</div>

Bootstrap utility classes can be combined with the container class.

9. Container with Padding

You can use Bootstrap spacing utilities to add additional padding to a container.

<div class="container p-5">

    Content

</div>

Here, p-5 adds padding around the content.

10. Container with Margin

Bootstrap spacing utilities can also be used to add margin around container content.

<div class="container mt-5">

    Content

</div>

The mt-5 class adds top margin.

11. Container and Row

Containers are commonly used as the outer wrapper for Bootstrap rows.

<div class="container">

    <div class="row">

        <div class="col">
            Column 1
        </div>

        <div class="col">
            Column 2
        </div>

    </div>

</div>

12. Container and Columns

A container can contain a row, and the row can contain columns. This is the basic structure of many Bootstrap layouts.

<div class="container">

    <div class="row">

        <div class="col-md-4">
            Left
        </div>

        <div class="col-md-8">
            Right
        </div>

    </div>

</div>

13. Container Breakpoints

Bootstrap provides responsive container classes that can be used to control the maximum width at specific breakpoints.

Examples include:

  • .container-sm
  • .container-md
  • .container-lg
  • .container-xl
  • .container-xxl

14. .container-sm

The .container-sm class provides a responsive container whose maximum width applies from the small breakpoint and above.

<div class="container-sm">

    Content

</div>

15. .container-md

The .container-md class uses the container's maximum width from the medium breakpoint and above.

<div class="container-md">

    Medium Container

</div>

16. .container-lg

The .container-lg class applies its maximum width from the large breakpoint and above.

<div class="container-lg">

    Large Container

</div>

17. .container-xl

The .container-xl class applies its maximum width from the extra-large breakpoint and above.

<div class="container-xl">

    Extra Large Container

</div>

18. .container-xxl

The .container-xxl class applies its maximum width from the extra-extra-large breakpoint and above.

<div class="container-xxl">

    XXL Container

</div>

19. Container Breakpoint Example

<div class="container-md">

    <h2>
        Responsive Container
    </h2>

    <p>
        This container uses the medium breakpoint.
    </p>

</div>

This type of container is useful when you want a specific responsive behavior.

20. Full Width Container Example

<div class="container-fluid bg-dark text-white p-5">

    <h1>
        Full Width Section
    </h1>

</div>

This is useful for full-width sections such as banners and hero areas.

21. Container with Navbar

A container is commonly placed inside a navbar to control the width of the navbar's content.

<nav class="navbar navbar-dark bg-dark">

    <div class="container">

        <a class="navbar-brand"
        href="#">
            My Website
        </a>

    </div>

</nav>

22. Container for Hero Section

Containers are useful inside hero sections to keep headings and paragraphs aligned with the rest of the website.

<section class="bg-primary text-white p-5">

    <div class="container">

        <h1>
            Welcome
        </h1>

        <p>
            Build responsive websites.
        </p>

    </div>

</section>

23. Container for Footer

A container can also be used inside a footer to keep footer content aligned and responsive.

<footer class="bg-dark text-white p-4">

    <div class="container">

        <p>
            © 2026 My Website
        </p>

    </div>

</footer>

24. Multiple Containers

A page can contain multiple containers. For example, different sections of a website can each have their own container.

<section>

    <div class="container">
        Section 1
    </div>

</section>

<section>

    <div class="container">
        Section 2
    </div>

</section>

25. Container Inside a Section

A common Bootstrap page structure places a container inside a semantic section element.

<section class="py-5">

    <div class="container">

        <h2>
            About Us
        </h2>

        <p>
            Learn more about our company.
        </p>

    </div>

</section>

26. Container and Responsive Design

Containers help create responsive layouts by adapting their maximum content width according to the viewport.

This allows large screens to have controlled content widths while smaller screens can use the available space effectively.

27. Container Width Concept

The standard .container does not simply mean a fixed width on every device. Its maximum width changes according to Bootstrap's responsive breakpoints.

This is why the same container can look different on a mobile phone, tablet, and desktop screen.

28. Container with Three Columns

<div class="container">

    <div class="row">

        <div class="col-md-4">
            Course 1
        </div>

        <div class="col-md-4">
            Course 2
        </div>

        <div class="col-md-4">
            Course 3
        </div>

    </div>

</div>

The container holds the row, and the row contains three responsive columns.

29. Container Best Practices

  • Use containers to organize page content.
  • Use .container for responsive maximum-width content.
  • Use .container-fluid for full-width layouts.
  • Use responsive container classes when specific breakpoint behavior is needed.
  • Use containers together with rows and columns.
  • Use spacing utilities to control padding and margin.
  • Avoid unnecessary deeply nested containers.

30. Complete Container Example

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta
    name="viewport"
    content="width=device-width,
    initial-scale=1.0">

    <title>
        Bootstrap Containers
    </title>

    <link
    href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
    rel="stylesheet">

</head>

<body>

<div class="container py-5">

    <h1 class="text-primary">
        Bootstrap Containers
    </h1>

    <p class="lead">
        Learn how Bootstrap containers work.
    </p>

    <div class="row mt-4">

        <div class="col-md-4">

            <div class="card">

                <div class="card-body">

                    <h5>
                        HTML
                    </h5>

                    <p>
                        Learn HTML basics.
                    </p>

                </div>

            </div>

        </div>

        <div class="col-md-4">

            <div class="card">

                <div class="card-body">

                    <h5>
                        CSS
                    </h5>

                    <p>
                        Learn CSS styling.
                    </p>

                </div>

            </div>

        </div>

        <div class="col-md-4">

            <div class="card">

                <div class="card-body">

                    <h5>
                        Bootstrap
                    </h5>

                    <p>
                        Learn responsive design.
                    </p>

                </div>

            </div>

        </div>

    </div>

</div>

<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js">
</script>

</body>

</html>

This example demonstrates a container, spacing utilities, a row, three responsive columns, and Bootstrap cards.

📌 Key Points

  • .container creates a responsive maximum-width container.
  • .container-fluid creates a full-width container.
  • Containers provide horizontal padding and organize content.
  • Containers are commonly used with rows and columns.
  • Bootstrap provides responsive container classes such as .container-sm, .container-md, .container-lg, .container-xl, and .container-xxl.
  • Containers can be used inside sections, navbars, and footers.
  • Spacing utilities can be combined with containers.
  • Containers are an important part of responsive Bootstrap layouts.

🧠 Quick Quiz

Question: Which Bootstrap class creates a full-width container?