Introduction To Bootstrap 4 Flex Layout

Introduction to Bootstrap 4 Flex Layout (Flexbox for Bootstrap)

Bootstrap 4 is the latest version of Bootstrap, which is the world’s most popular front-end framework featuring a responsive grid system, Sass variables and mixins, powerful JavaScript plugins, and extensive prebuilt components for easier and faster responsive web development. Bootstrap is used for developing responsive, mobile-first sites, with a template starter page and BootstrapCDN.

 

Now, we will go through them and understand with the help of examples about Bootstrap 4. Bootstrap 4 is free to download and use! You can save plenty of time and effort with Bootstrap.

 

What is Flexbox?

Flexbox stands for the flexible box. It is a new layout mode of CSS3 which is named as CSS Flexible Box Layout Module. This cutting-edge CSS layout system is a powerful tool that makes it simple to design layouts for unknown or dynamic screen sizes. The flex container can control and adjust the size of its child elements to adapt to different viewports.

You can easily build a Flexbox layout using a set of CSS properties intended for this task. It becomes easy to create Flexbox-based layouts with the help of Bootstrap, as it provides a set of wrapper classes on top of Flexbox features, which you can easily apply to your mark-up for accomplishing the desired results.

The primary benefit of this layout mode is that it performs the following layout tasks easier:

  • Specification of the layout direction
  • Arrangement of items
  • Specification of the layout order of items

 

Bootstrap 4 Flex Layout

Flex utility helps you to manage the layout, grid columns, navigation, alignment, and other additional components of the page. It becomes easy to create a layout structure without using positioning or float.

Now, let us create and explain a simple page layout using the Bootstrap 4 grid system.

These are the core components of the Bootstrap grid:

Container

The container is the outer wrapper for the grid layout. It’s a div that has either the .container-fluid for a 100% full width or class .container for fixed width.

Row

A row works as a logical container for columns.

Column

A column is something that makes a block in the grid. It should be contained in a row.

Step 1: Firstly, you need to start by creating an HTML page and link it with the Bootstrap 4 assets.

<!doctype html>

<html lang="en">

  <head>

    <!-- Required meta tags -->

    <meta charset="utf-8">

    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xssffqQ1aoWggXA+058RXPxPgss6fy4IWvTNfh0ss263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <title>BS 4 Flexbox Examples</title>

  </head>

  <body>

    Add Content Here

    <!-- Optional JavaScript -->

    <!-- jQuery first, then Popper.js, then Bootstrap JS -->

    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENffsz456zmM7KCkRr/rE9/Qpg6aAZGJjkl9wFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgff78PxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQG98yhjiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

  </body>

</html>

 

The grid can be created using some of the predefined BS classes (.container, .row, col-*-*).

The grid should have a container, rows, and columns.

The container is simply a <div> with the .container classes. Rows provide a home for columns, and Columns are what make the cells for the grid system.

 

Step 2: Create a column, for this you simply need to use a <div> with the class(es) col-*-*

The first star for the screen breakpoint (xs, sm, md, lg, xl) and the second star for column size (1.. 12).

  <div class="row">

    <div class="col-sm-1 first-column">

      <p>.col-sm-1</p>

    </div>

    ...

    <div class="col-sm-1 second-column">

      <p>.col-sm-1</p>

    </div>   

  </div>

 

Step 3: Now, let’s create a second row with two columns

  <div class="row">

    <div class="col-sm-6 first-column">

      <p>.col-sm-6</p>

    </div>

    <div class="col-sm-6 second-column">

      <p>.col-sm-6</p>

    </div>   

  </div>

Note that .col-sm-6 + .col-sm-6 === .col-sm-12

 

Benefits of Using Bootstrap:

You might be questioning what makes Bootstrap so special. Here are the reasons why one should choose the Bootstrap framework:

  • Easy to use
  • Open Source
  • Responsive features
  • Consistent design
  • Save lots of time
  • Compatible with browsers

  

Conclusion:

In this tutorial, we have covered the features of the Bootstrap 4 framework, starting with the basics, such as grid system, common user interface elements like container, tables, lists, images, etc. Bootstrap has always been a dominant and powerful CSS framework for developers creating their websites without the need of any CSS designer in the team. Additional new features such as the default support for Flexbox serve as a great tool to create responsive layouts.