Open In App

Bootstrap 5 Customizing the Grid

Last Updated : 22 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • $grid-columns[12]: This variable is used to customize the number of grid columns in the grid.
  • $grid-gutter-width[1.5rem]: This variable is used to control the amount of padding between the columns of the grid.

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:

  • $grid-breakpoints: This variable states the grid tier values(screen sizes of the viewport) where the grid will break into the custom responsive number of columns if specified. Under this variable, the default values are 0 for xs, 576px for sm, 768px for md, 992px for lg, 1200px for xl, and 1400px for xxl.
  • $container-max-widths: This variable states the maximum width of the container at any specific given breakpoint or viewport size. Under this variable, the default values are 0 for xs, 540px for sm, 720px for md, 960px for lg, 1140px for xl, and 1320px for xxl.

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:

CSS




$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).

CSS




.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:

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:

ezgifcom-video-to-gif-(77).gif

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:

CSS




$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).

CSS




@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:

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:

ezgifcom-video-to-gif-(78).gif

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads