Understanding code made easy

Post Top Ad

Your Ad Spot

Tuesday, June 9, 2020

Bootstrap 4 - Grid System & Container Classes


Bootstrap 4
Grid system:

The BS 4 grid system built with flexbox which is fully responsive and scales up to 12 columns according to the size of device by creating layout with rows and columns across the page.
It provides responsive, mobile first fluid grid system which scales the columns as the device or viewport size increases.
If you do not want to use all 12 columns individually, you can group the columns together to create wider columns:

Grid System
The Grid System

Working of the Bootstrap 4 Grid System:
1. Rows must be placed within a '.container' class for proper alignment and padding.
2. For responsive width use '.container' class and for fixed width across all viewport use the '.container-fluid' class.
3. Use rows to create horizontal groups of columns.
4. Content should be placed within the columns, and only columns may be the  child content of the rows.
5. Columns contain padding for controlling the space between them. If you place more than 12 columns in a row, then the columns will be placed in a new line.
6. Columns create gaps between column content via padding. Therefore, you can remove the margin from rows and padding from columns with .no-gutters class on the row.
7. You can make grid system responsive by using five grid breakpoints such as extra small, small, medium, large, and extra large.


Classes of screen sizes:
The Bootstrap 4 grid system has five classes as follows.
1) .col- (extra small devices - screen width less than 576px)
2) .col-sm- (small devices - screen width equal to or greater than 576px)
3) .col-md- (medium devices - screen width equal to or greater than 768px)
4) .col-lg- (large devices - screen width equal to or greater than 992px)
5) .col-xl- (xlarge devices - screen width equal to or greater than 1200px)


Basic Bootstrap 4 Grid Structure:
Following is basic structure of Bootstrap 4 grid −
<!-- Control the column width, and how they should appear on different devices -->

<div class="row">
  <div class="col-*-*"></div>
  <div class="col-*-*"></div>
</div>
<div class="row">
  <div class="col-*-*"></div>
  <div class="col-*-*"></div>
  <div class="col-*-*"></div>
</div>

<!-- Or let Bootstrap automatically handle the layout -->
<div class="row">
  <div class="col"></div>
  <div class="col"></div>
  <div class="col"></div>
</div>

  • First example: create a row (<div class="row">). Then, add the desired number of columns (tags with appropriate .col-*-* classes). The first star (*) represents the responsiveness: sm, md, lg or xl, while the second star represents a number, which should add up to 12 for each row.
  • Second example: instead of adding a number to each col, let bootstrap handle the layout, to create equal width columns: two "col" elements = 50% width to each col. three cols = 33.33% width to each col. four cols = 25% width, etc. You can also use .col-sm|md|lg|xl to make the columns responsive.

Responsive Columns:
The following example shows how to create four equal-width columns starting at tablets and scaling to extra large desktops. On mobile phones or screens that are less than 576px wide, the columns will automatically stack on top of each other.
<div class="row">
    <div class="col-sm-3">.col-sm-3</div>
    <div class="col-sm-3">.col-sm-3</div>
    <div class="col-sm-3">.col-sm-3</div>
    <div class="col-sm-3">.col-sm-3</div>
</div>


Output:



Two Unequal Responsive Columns:

The following example shows how to get two various-width columns starting at tablets and scaling to large extra desktops.
<div class="row">
    <div class="col-sm-4">.col-sm-4</div>
    <div class="col-sm-8">.col-sm-8</div>
</div>


Output:



Bootstrap 4 Container Classes:
Containers are used to pad the contents inside them, and there are two container classes available.
1. container class
The '.container' class provides a responsive fixed width container.

2. container-fluid class
The '.container-fluid' class provides a full width container, spanning the entire width of the screen.

Generally the row & col classes are wrapped inside a '.container' OR
'.container-fluid' class for greater feasibility. Take a look a the code below.

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


Conclusion:
The Bootstrap Grid System is the first concept which we need to learn in order to start with using this framework. This grid system is a must for the developer to learn at the start of learning bootstrap 4 framework.

Happy Learning !


No comments:

Post a Comment

Comments