Open In App

Bootstrap 5 Grid Small

Last Updated : 08 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Bootstrap Grid is a layout system that uses a 12-column grid to style a website. When the screen size is ≥576 px, a small grid is applied, and the elements align accordingly to the screen size using an auto-column layout.

We can also specify the variable width of each element by mentioning the number along with the class, for example, .container-sm-4. If the screen size is less than 576 px, then the element takes up 100% width of the screen.

Breakpoint

Description

Size (px)

xs

extra small

<576

sm

small

≥ 576

md

medium

≥ 768

lg

large

≥ 992

xl

extra large

≥ 1200

xxl

extra extra large

≥ 1400

Syntax

<tag_name  class="class-sm">Content of the element </tag_name>

Grid Small with Equal Size

Example: This code is a basic HTML document that uses Bootstrap 5 to create a responsive grid layout with equal-width columns. The grid consists of three rows, each containing four columns

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Grid Small</title>
    <script src=
            integrity="sha256-YMa+wAM6QkVyz999odX7lPRxkoYAan8suedu4k2Zur8="
            crossorigin="anonymous">
    </script>
    <link rel="stylesheet" href=
        integrity="sha256-MBffSnbbXwHCuZtgPYiwMQbfE7z+GOZ7fBPCNB06Z98="
        crossorigin="anonymous">
    <style>
        [class*="col"],
        h2,
        p {
            padding: 1rem;
            background-color: green;
            border: 2px dashed #f3f3f3;
            color: #fff;
        }
    </style>
</head>
 
<body>
    <div class="container text-center ">
        <h2>Welcome to GeeksforGeeks</h2>
        <p>Equal width </p>
        <div class="row">
            <div class="col-sm">HTML</div>
            <div class="col-sm">HTML</div>
            <div class="col-sm">HTML</div>
            <div class="col-sm">HTML</div>
        </div>
        <div class="row">
            <div class="col-sm">CSS</div>
            <div class="col-sm">CSS</div>
            <div class="col-sm">CSS</div>
            <div class="col-sm">CSS</div>
        </div>
        <div class="row">
            <div class="col-sm">JavaScript</div>
            <div class="col-sm">JavaScript</div>
            <div class="col-sm">JavaScript</div>
            <div class="col-sm">JavaScript</div>
        </div>
    </div>
</body>
 
</html>


Output:

edr

Auto Layout Column

Example: Illustration of Auto Layout column using Boostrap

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1">
    <link href=
          rel="stylesheet">
    <script src=
      </script>
</head>
 
<body>
 
    <div class="container-fluid mt-3">
        <h1>Responsive Auto Layout Columns</h1>
        <p>
          In Bootstrap 5, you can easily create equal
          width columns by using the <code>.col-sm</code>
          class on a specified number of col elements.
          Bootstrap will automatically adjust the width
          of each column based on the
          number of columns specified.
          </p>
        <p>
          On extra small screens (<strong>less than 576px</strong>),
          the columns will stack horizontally.
          </p>
        <div class="container-fluid">
            <div class="row">
                <div class="col-sm bg-success text-white">
                      1 of 2
                  </div>
                <div class="col-sm bg-info text-white">
                      2 of 2
                  </div>
            </div>
        </div>
        <br>
 
        <div class="container-fluid">
            <div class="row">
                <div class="col-sm bg-primary text-white">
                      1 of 4
                  </div>
                <div class="col-sm bg-secondary text-white">
                      2 of 4
                  </div>
                <div class="col-sm bg-danger text-white">
                      3 of 4
                  </div>
                <div class="col-sm bg-warning text-white">
                      4 of 4
                  </div>
            </div>
        </div>
        <br>
 
        <div class="container-fluid">
            <div class="row">
                <div class="col-sm bg-dark text-white">
                      1 of 3
                  </div>
                <div class="col-sm bg-light text-dark">
                      2 of 3
                  </div>
                <div class="col-sm bg-dark text-white">
                      3 of 3
                  </div>
            </div>
        </div>
        <br>
 
        <div class="container-fluid">
            <div class="row">
                <div class="col-sm bg-primary text-white">
                      1 of 6
                  </div>
                <div class="col-sm bg-secondary text-white">
                      2 of 6
                  </div>
                <div class="col-sm bg-danger text-white">
                      3 of 6
                  </div>
                <div class="col-sm bg-warning text-white">
                      4 of 6
                  </div>
                <div class="col-sm bg-success text-white">
                      5 of 6
                  </div>
                <div class="col-sm bg-info text-white">
                      6 of 6
                  </div>
            </div>
        </div>
    </div>
 
</body>
 
</html>


Output:

gty

Explanation:

The provided HTML code demonstrates the use of Bootstrap 5’s grid system to create responsive layouts with equal-width columns. The .col-sm class is used to specify the number of columns on small screens (≥576px), and the columns automatically adjust their width based on the screen size. The columns stack horizontally on extra small screens (less than 576px).



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads