Open In App

Bootstrap 5 Grid System SASS

Bootstrap’s grid system uses containers, rows, and columns to create responsive layouts and align content. The default Bootstrap Grid System allows a maximum of 12 columns across the page. One can use all 12 columns individually or you can group them together.

The Bootstrap allows to further customize the grid system using SASS. We can achieve this by using variables and mixins.



Bootstrap 5 Grid System SASS Variables

Syntax:

// Variables 
$grid-columns:
$grid-gutter-width:
$grid-breakpoints: ()
$container-max-widths: ();

Bootstrap 5 Grid System SASS Mixins

Syntax:

// mixins
@include make-row();
@include make-col-ready();
@include make-col();
@include make-col($size, $columns: $grid-columns);
@include make-col-offset($size, $columns: $grid-columns);

Steps to override scss of Bootstrap

Step 1: Install Bootstrap using the following command:

npm i bootstrap

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



$class_to_override: values;
@import "node_modules/bootstrap/scss/bootstrap"

Step 3: Convert the file to CSS using live server extension.

Step 4: Include the converted scss file to your HTML after the link tag of Bootstrap css

Example 1: In this example, we will use the SASS variable to customize the row and columns.

index.html




<!DOCTYPE html>
<html>
  
<head>
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0" />
    <title>GFG Example</title>
    <link 
          href=
          rel="stylesheet" />
    <link rel="stylesheet" href="style.css" />
</head>
  
<body>
    <div class="text-center">
        <h1 class="text-success">GeeksforGeeks</h1>
        <h3>BootStrap 5 Grid system SASS</h3>
    </div>
    <div class="container">
        <div class="row">
            <div class="col-md-1 col-sm-7 bg-primary">
                1
            </div>
            <div class="col-md-2 col-sm-7 bg-warning">
                2
            </div>
            <div class="col-md-4 col-sm-2 bg-info">
                3
            </div>
            <div class="col-md-5 col-sm-6 bg-danger">
                4
            </div>
            <div class="col-md-2 col-sm-6 bg-success">
                5
            </div>
        </div>
    </div>
</body>
  
</html>

style.scss




$grid-columns: 14;
$grid-breakpoints: (
    xs: 0,
    sm: 600px,
    md: 800px,
);
$grid-gutter-width: 2.5rem;
@import "./node_modules/bootstrap/scss/bootstrap";

Output:

Example 2: In this example, we are customizing the grid system using mixins.

index.html




<!DOCTYPE html>
<html>
  
<head>
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0" />
    <title>GFG Example</title>
    <link href=
          rel="stylesheet" />
    <link rel="stylesheet" href="style.css" />
</head>
  
<body>
    <div class="text-center">
        <h1 class="text-success">GeeksforGeeks</h1>
        <h3>BootStrap 5 Grid system SASS</h3>
    </div>
    <div class="custom-container">
        <div class="custom-row">
            <div class="custom-col bg-danger">1</div>
            <div class="custom-col bg-info">2</div>
            <div class="custom-col bg-warning">3</div>
            <div class="custom-col bg-success">4</div>
        </div>
    </div>
</body>
  
</html>

style.scss




@import "./node_modules/bootstrap/scss/bootstrap.scss";
.custom-container {
    @include make-container();
    width: 500px;
}
.custom-row {
    @include make-row();
}
.custom-col {
    @include make-col();
    @include media-breakpoint-up(sm) {
        @include make-col(4);
    }
    @include media-breakpoint-up(lg) {
        @include make-col(6);
    }
}

Output:

Refernce: https://getbootstrap.com/docs/5.0/layout/grid/#sass


Article Tags :