Open In App

Bootstrap 5 Customizing the Grid

Bootstrap Grid system is one of the most versatile layouts which we can use and it has different responsive variants which can be used to add custom responsiveness. Bootstrap Grid system provides a responsive layout with columns and gutters. Customizing the grid is essentially changing the number of columns or gutters’ width or changing the breakpoints at which the grids will attain the specific width given to them.

Grid System Columns and Gutters

Columns are divided into 12 equal parts, allowing you to create flexible and responsive designs. Gutters are the spacing between columns, which can be adjusted using predefined classes. The number of columns and the gutter width can be changed using the SASS version. For this, you can use the $grid-columns and $grid-gutter-width for changing the maximum width occupied by the container at the various breakpoints.



Variables needed to control Grid Columns and gutters and their default values

Syntax:

$grid-columns: integer-value;
$grid-gutter-width: value;

Grid system Grid tiers

Across the six default breakpoints, the grid works smoothly, and all these breakpoints can also be called Grid Tiers which can be changed using the SASS version. For this, we can use the $grid-breakpoints and $container-max-widths for changing the maximum width occupied by the container at the various breakpoints.

Variables needed to control Grid tiers and their default values:



Syntax:

$variable_to_override: (
/* Grid Tiers and their values */
) !default;

The Only Approach to customize the grid in Bootstrap that is by using the SASS version of it.

Steps to override SASS of Bootstrap

Step 1: Install Bootstrap using the following command

npm i bootstrap sass

Step 2: Create your custom SCSS file and write the variable you want to override. Then include the bootstrap SCSS file using import.

$variable_to_override: value
@import "../node_modules/bootstrap/scss/bootstrap.scss";

Step 3: Convert the SCSS file to CSS using a live server extension or using this command in terminal

sass styles.scss styles.css

Step 4: Include the CSS file in your HTML file.

<link rel="stylesheet" href="styles.css">

Project Structure

Example 1: The code below demonstrates customizing the grid by changing the number of columns and the padding between the columns using the $grid-columns and $grid-gutter-width variables. The value of the first is changed to 20 here and the second is updated from 1.5rem to 2rem. The values of the sm, md, and lg breakpoints are given as 6, 8, and 4 columns and the padding between these columns in every breakpoint is 2rem. In the output GIF, you can see how when you set col-lg-4 to a column it takes 80% of the total grid area as there are four grid elements and 16 is 80% of 20:

styles.scss:




$grid-columns: 20 ;
$grid-gutter-width: 5rem;
  
@import "./node_modules/bootstrap/scss/bootstrap.scss";

styles.css: The above SCSS file is compiled into this CSS file(only the changes shown).




.col-20 {
    flex: 0 0 auto;
    width: 100%;
}
  
.col-sm-20 {
    flex: 0 0 auto;
    width: 100%;
}
  
.col-md-20 {
    flex: 0 0 auto;
    width: 100%;
}
  
.col-lg-20 {
    flex: 0 0 auto;
    width: 100%;
}
  
.col-xl-20 {
    flex: 0 0 auto;
    width: 100%;
}
  
.col-xxl-20 {
    flex: 0 0 auto;
    width: 100%;
}
  
.row {
    --bs-gutter-x: 5rem;
    --bs-gutter-y: 0;
    display: flex;
    flex-wrap: wrap;
    margin-top: calc(-1 * var(--bs-gutter-y));
    margin-right: calc(-0.5 * var(--bs-gutter-x));
    margin-left: calc(-0.5 * var(--bs-gutter-x));
}

index.html:




<!doctype html>
<html lang="en">
  
<head>
    <link rel="stylesheet" href="styles.css">
    <script src=
    </script>
</head>
  
<body>
    <div class="text-center mt-4">
        <h1 class="text-success">GeeksforGeeks</h1>
        <h3>Bootstrap 5 Grid system Customizing the Grid</h3>
        <div class="mt-4 p-4">
            <div class="row">
                <div class="col-sm-6 col-md-8 col-lg-4">
                    <div class="card mb-3 bg-light">
                        <div class="card-body">
                            <p class="card-text">
                                A data structure is a storage that
                                is used to store and organize data.
                            </p>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6 col-md-8 col-lg-4">
                    <div class="card mb-3 bg-light">
                        <div class="card-body">
                            <p class="card-text">
                                Therefore Algorithm refers to a 
                                sequence of finite steps to solve 
                                a particular problem.
                            </p>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6 col-md-8 col-lg-4">
                    <div class="card mb-3 bg-light">
                        <div class="card-body">
                            <p class="card-text">
                                C++ is a general-purpose programming
                                language and is widely used nowadays
                                for competitive programming.
                            </p>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6 col-md-8 col-lg-4">
                    <div class="card mb-3 bg-light">
                        <div class="card-body">
                            <p class="card-text">
                                Java is one of the most popular and
                                widely used programming languages.
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
  
</html>

Output:

Example 2: The code below demonstrates how we can customize the grid by changing the $grid-breakpoints variable and the different breakpoints values inside it. The values of the sm, md, and lg breakpoints are changed, and the columns change with the new breakpoints. In the output GIF, you can see on the top right corner of the browser window the current size of the screen which it is resized to:

styles.scss:




$grid-breakpoints: (
    xs: 0,
    sm: 476px,
    md: 650px,
    lg: 1000px,
    xl: 1200px,
    xxl: 1400px) !default;
  
@import "./node_modules/bootstrap/scss/bootstrap.scss";

styles.css: The above SCSS file is compiled into this CSS file(only the changes shown).




@media (min-width: 476px) {
  
    .container-sm,
    .container {
        max-width: 540px;
    }
}
  
@media (min-width: 650px) {
  
    .container-md,
    .container-sm,
    .container {
        max-width: 720px;
    }
}
  
@media (min-width: 1000px) {
  
    .container-lg,
    .container-md,
    .container-sm,
    .container {
        max-width: 960px;
    }
}
  
@media (min-width: 1200px) {
  
    .container-xl,
    .container-lg,
    .container-md,
    .container-sm,
    .container {
        max-width: 1140px;
    }
}
  
@media (min-width: 1400px) {
  
    .container-xxl,
    .container-xl,
    .container-lg,
    .container-md,
    .container-sm,
    .container {
        max-width: 1320px;
    }
}

index.html:




<!doctype html>
<html lang="en">
  
<head>
    <link rel="stylesheet" href="styles.css">
    <script src=
    </script>
</head>
  
<body>
    <div class="text-center mt-4">
        <h1 class="text-success">GeeksforGeeks</h1>
        <h3>Bootstrap 5 Grid system Customizing the Grid</h3>
        <div class="mt-4 p-4">
            <div class="row">
                <div class="col-sm-12 col-md-6 col-lg-3">
                    <div class="card mb-3 bg-light">
                        <div class="card-body">
                            <p class="card-text">
                                A data structure is a storage that
                                is used to store and organize data.
                            </p>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12 col-md-6 col-lg-3">
                    <div class="card mb-3 bg-light">
                        <div class="card-body">
                            <p class="card-text">
                                Therefore Algorithm refers to a sequence
                                of finite steps to solve a particular problem.
                            </p>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12 col-md-6 col-lg-3">
                    <div class="card mb-3 bg-light">
                        <div class="card-body">
                            <p class="card-text">
                                C++ is a general-purpose programming
                                language and is widely used nowadays
                                for competitive programming.
                            </p>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12 col-md-6 col-lg-3">
                    <div class="card mb-3 bg-light">
                        <div class="card-body">
                            <p class="card-text">
                                Java is one of the most popular and
                                widely used programming languages.
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
  
</html>

Output:

Reference: https://getbootstrap.com/docs/5.0/layout/grid/#customizing-the-grid


Article Tags :