Bootstrap 5 Customizing the Grid
Last Updated :
25 Jul, 2024
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=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js">
</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:
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=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js">
</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
Similar Reads
How to Customize Bootstrap 5?
To customize Bootstrap 5, adjust its appearance by using custom CSS to modify default styles or by utilizing Sass variables to customize design elements like colors, typography, and spacing. These methods enable you to control your project's visual presentation. Table of Content Custom CSSModifying
2 min read
Bootstrap 5 Grid System Row Columns
Bootstrap 5 grid system uses rows and columns to structure content. Rows create horizontal groups, while columns divide the page width. The system offers responsive classes for layout adjustments across different screen sizes.Bootstrap 5 Grid system Row Columns Classes:Bootstrap ClassDescriptionrow-
2 min read
Bootstrap 5 Sizing
Bootstrap 5 Sizing utility classes adjust element dimensions relative to the viewport or parent container. Options include percentage-based widths, and viewport-relative dimensions, facilitating responsive design for flexible layouts across various screen sizes. iframe { width: 100%; height: 400px;}
3 min read
Bootstrap 5 Grid Columns and Gutters
Bootstrap 5 is a very popular CSS Framework, that offers a complete range of tools and elements for creating responsive and mobile-first websites. Its robust grid structure, which enables developers to make flexible and adaptable layouts, is one of its main characteristics. In this article, we will
3 min read
Bootstrap 5 Change the gutters
Bootstrap 5 Change the gutters uses $spacer to make the $gutters SASS map. The gutters are used to give padding between the columns, responsively space, and align content to the grid system. Below is the list of default values of the $gutters map: $grid-gutter-width: 1.5rem; $gutters: ( 0: 0, 1: $sp
3 min read
Bootstrap 5 Layout Form Grid
Bootstrap 5 Layout Form Grid is used to create a complex form using grid classes. The form grid layout required multiple columns of different widths and alignments. To create a form grid, we use .row and .col classes. Form Grid Layout used Classes: .row: This class is used to create a row of a form
2 min read
Bootstrap 5 Grid Cards
Bootstrap 5 Grid Cards is a way to have a grid of cards put beside one another and they completely act like a grid. It is created by using the row-cols and col classes and the row-col classes are responsive, so we can specify how many cards will be there in a row in any specified screen size.Bootstr
4 min read
Bootstrap 5 Utilities
Bootstrap 5 provides a lot of utilities to make a stylish, mobile-friendly, and responsive front-end website without using any CSS code. The utility layout is used for showing, hiding, aligning, and spacing content. Bootstrap 5 utilities for layout: Changing display: Bootstrap 5 provides some displa
2 min read
Bootstrap 5 Cards Sizing
Bootstrap Cards Sizing refers to the ability to control the dimensions of cards in Bootstrap by using predefined classes such as col, col-sm, col-md, col-lg, col-xl, and col-xxl. This ensures consistency and responsiveness across various screen sizes, maintaining a uniform appearance for card elemen
2 min read
Bootstrap 5 Grid Large
Bootstrap is a free and open-source tool for creating responsive websites and web applications. It is the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites. Nowadays, the websites are perfect for all browsers (IE, Firefox, and Chrome) and all sizes of
3 min read