DEV Community

Cover image for Day 3 Bootstrap 5 Grid system
Brijesh Dobariya
Brijesh Dobariya

Posted on

Day 3 Bootstrap 5 Grid system

In these tutorial we have discuss Bootstrap 5 Grid system.

Bootstrap 5 Grid system

Bootstrap grid is a powerful system for building mobile-first layouts. It's very extensive tool with a great number of options.

Use of Bootstrap Gid system:

The bootstrap grid system allows users to create responsive layout.

We can use it to rearrange layout of the website depending on the screen size.

Example:

Using bootstrap grid system, we can create a page which will have different layout on desktop (top right corner) and tablet (bottom right corner).

Alt Text

Basic example of Bootstrap grid system

Bootstrap’s grid system uses a series of containers, rows, and columns to layout and align content. It’s built with flexbox and is fully responsive. Below is an example and an in-depth explanation for how the grid system comes together.

Alt Text



<div class="container">
  <div class="row">
    <div class="col-md">
      One of three columns
    </div>
    <div class="col-md">
      One of three columns
    </div>
    <div class="col-md">
      One of three columns
    </div>
  </div>
</div>


Enter fullscreen mode Exit fullscreen mode

Explanation:

The above example creates three equal-width columns across all devices and viewports using our predefined grid classes. Those columns are centred in the page with the parent .container.

Container

Bootstrap required container for properly working grid system.

Rows

Rows create horizontal groups of columns. Therefore, if you want to split your layout horizontally, use. row.

Columns

Bootstrap's grid system allows up to 12 columns across the page.

How it works

Breaking it down, here’s how the grid system comes together:
Our grid supports six responsive breakpoints.

Breakpoints are based on min-width media queries, meaning they affect that breakpoint and all those above it (e.g.,. col-sm-4 applies to sm, md, lg, xl, and xxl). This means you can control container and column sizing and behavior by each breakpoint.

Containers center and horizontally pad your content.
Use .container for a responsive pixel width, .container-fluid for width: 100% across all viewports and devices, or a responsive container (e.g., .container-md) for combination of fluid and pixel widths.

Rows are wrappers for columns.

Each column has horizontal padding (called a gutter) for controlling the space between them. This padding is then counteracted on the rows with negative margins to ensure the content in your columns is visually aligned down the left side. Rows also support modifier classes to uniformly apply column sizing and gutter classes to change the spacing of your content.

Columns are incredibly flexible.

There are 12 template columns available per row, allowing you to create different combinations of elements that span any number of columns. Column classes indicate the number of template columns to span (e.g., col-4 spans four). widths are set in percentages so you always have the same relative sizing.

Gutters are also responsive and customizable.

Gutter classes are available across all breakpoints, with all the same sizes as our margin and padding spacing. Change horizontal gutters with .gx-* classes, verical gutters with .gy-, or all gutters with .g- classes. .g-0 is also availble to remove gutters.

Sass variables, maps, and mixins power the grid.

If you don’t want to use the predefined grid classes in Bootstrap, you can use our grid’s source Sass to create your own with more semantic markup. We also include some CSS custom properties to consume these Sass variables for even greater flexibility for you.

Types of most common grid system

Five grid tires
There are five tiers to the Bootstrap grid system, one for each range of devices we support. Each tier starts at a minimum viewport size and automatically applies to the larger devices unless overridden.

Alt Text



<div class="row">
  <div class="col-4">.col-4</div>
  <div class="col-4">.col-4</div>
  <div class="col-4">.col-4</div>
</div>

<div class="row">
  <div class="col-sm-4">.col-sm-4</div>
  <div class="col-sm-4">.col-sm-4</div>
  <div class="col-sm-4">.col-sm-4</div>
</div>

<div class="row">
  <div class="col-md-4">.col-md-4</div>
  <div class="col-md-4">.col-md-4</div>
  <div class="col-md-4">.col-md-4</div>
</div>

<div class="row">
  <div class="col-lg-4">.col-lg-4</div>
  <div class="col-lg-4">.col-lg-4</div>
  <div class="col-lg-4">.col-lg-4</div>
</div>

<div class="row">
  <div class="col-xl-4">.col-xl-4</div>
  <div class="col-xl-4">.col-xl-4</div>
  <div class="col-xl-4">.col-xl-4</div>
</div>


Enter fullscreen mode Exit fullscreen mode

Three equal columns

Get three equal-width columns starting at desktops and scaling to large desktops. On mobile devices, tablets and below, the columns will automatically stack.

Alt Text



<div class="row">
  <div class="col-md-4">.col-md-4</div>
  <div class="col-md-4">.col-md-4</div>
  <div class="col-md-4">.col-md-4</div>
</div>


Enter fullscreen mode Exit fullscreen mode

Three unequal columns

Get three columns starting at desktops and scaling to large desktops of various widths. Remember, grid columns should add up to twelve for a single horizontal block. More than that, and columns start stacking no matter the viewport.

Alt Text



<div class="row">
  <div class="col-md-3">.col-md-3</div>
  <div class="col-md-6">.col-md-6</div>
  <div class="col-md-3">.col-md-3</div>
</div>


Enter fullscreen mode Exit fullscreen mode

Two columns

Get two columns starting at desktops and scaling to large desktops.

Alt Text



<div class="row">
  <div class="col-md-8">.col-md-8</div>
  <div class="col-md-4">.col-md-4</div>
</div>


Enter fullscreen mode Exit fullscreen mode

Two columns with two nested columns

Per the documentation, nesting is easy—just put a row of columns within an existing column. This gives you two columns starting at desktops and scaling to large desktops, with another two (equal widths) within the larger column.

At mobile device sizes, tablets and down, these columns and their nested columns will stack.

Alt Text



<div class="row">
  <div class="col-md-8">
    <div class="pb-3">
      .col-md-8
    </div>
    <div class="row">
      <div class="col-md-6">.col-md-6</div>
      <div class="col-md-6">.col-md-6</div>
    </div>
  </div>
  <div class="col-md-4">.col-md-4</div>
</div>


Enter fullscreen mode Exit fullscreen mode

Mixed: Mobile and desktop

The Bootstrap v5 grid system has five tiers of classes: xs (extra small, this class infix is not used), sm (small), md (medium), lg (large), and xl (extra large). You can use nearly any combination of these classes to create more dynamic and flexible layouts.
Each tier of classes scales up, meaning if you plan on setting the same widths for md, lg and xl, you only need to specify md.

Alt Text



<div class="row">
  <div class="col-md-8">.col-md-8</div>
  <div class="col-6 col-md-4">.col-6 .col-md-4</div>
</div>
<div class="row">
  <div class="col-6 col-md-4">.col-6 .col-md-4</div>
  <div class="col-6 col-md-4">.col-6 .col-md-4</div>
  <div class="col-6 col-md-4">.col-6 .col-md-4</div>
</div>
<div class="row">
  <div class="col-6">.col-6</div>
  <div class="col-6">.col-6</div>
</div>


Enter fullscreen mode Exit fullscreen mode

Mixed: Mobile, Tablet and Desktop

Alt Text



<div class="row">
  <div class="col-sm-6 col-lg-8">.col-sm-6 .col-lg-8</div>
  <div class="col-6 col-lg-4">.col-6 .col-lg-4</div>
</div>
<div class="row">
  <div class="col-6 col-sm-4">.col-6 .col-sm-4</div>
  <div class="col-6 col-sm-4">.col-6 .col-sm-4</div>
  <div class="col-6 col-sm-4">.col-6 .col-sm-4</div>
</div>


Enter fullscreen mode Exit fullscreen mode

Top comments (0)