-->

Bootstrap - Grid System

The Bootstrap 5 Grid System is a powerful and flexible layout system for designing responsive and mobile-first web pages. It is based on a 12-column layout that allows you to create various layouts for different screen sizes with ease.

Key Features of the Grid System

12-Column System: The layout divides the width of the page into 12 columns.

Responsive Design: Provides breakpoints for different screen sizes.

Flexbox-Based: Uses Flexbox for alignment and distribution of content.

Classes for Layout Control:

.container, .container-fluid

.row for grouping columns

.col, .col-{size}-{number} for defining columns

Customizable: Combine and adjust columns for unique layouts.

Containers

The grid system uses containers to define the layout boundaries. There are three types of containers:

.container: Fixed-width container with responsive breakpoints.

.container-fluid: Full-width container across all screen sizes.

.container-{breakpoint}: Fixed-width container specific to a breakpoint.

Example: Containers

<div class="container">

  Fixed-width container

</div>

<div class="container-fluid">

  Full-width container

</div>

<div class="container-lg">

  Fixed-width container for large screens and up

</div>

Rows and Columns

.row: Rows are wrappers for columns and help align them.

.col: Defines the column in a row.

Example: Rows and Columns

<div class="container">

  <div class="row">

    <div class="col">Column 1</div>

    <div class="col">Column 2</div>

    <div class="col">Column 3</div>

  </div>

</div>

Column Widths

Bootstrap allows columns to span multiple grid spaces using .col-{size}-{number}.

Example: Custom Column Widths

<div class="container">

  <div class="row">

    <div class="col-4">4 columns wide</div>

    <div class="col-8">8 columns wide</div>

  </div>

</div>

Responsive Breakpoints

Bootstrap provides five responsive breakpoints:

Extra small (xs): <576px

Small (sm): ≥576px

Medium (md): ≥768px

Large (lg): ≥992px

Extra large (xl): ≥1200px

XXL (xxl): ≥1400px

Example: Responsive Layout

<div class="container">

  <div class="row">

    <div class="col-sm-6 col-md-4 col-lg-3">Responsive Column</div>

    <div class="col-sm-6 col-md-4 col-lg-3">Responsive Column</div>

    <div class="col-sm-6 col-md-4 col-lg-3">Responsive Column</div>

    <div class="col-sm-6 col-md-4 col-lg-3">Responsive Column</div>

  </div>

</div>

Auto Layout Columns

You can let columns automatically adjust their width using .col.

Example: Auto Layout

<div class="container">

  <div class="row">

    <div class="col">Equal Width</div>

    <div class="col">Equal Width</div>

    <div class="col">Equal Width</div>

  </div>

</div>

Nesting Columns

You can nest rows and columns inside other columns.

Example: Nested Columns

<div class="container">

  <div class="row">

    <div class="col">

      Parent Column

      <div class="row">

        <div class="col">Nested Column 1</div>

        <div class="col">Nested Column 2</div>

      </div>

    </div>

  </div>

</div>

Column Offsets

Use .offset-{size}-{number} to add space before a column.

Example: Offset Columns

<div class="container">

  <div class="row">

    <div class="col-4 offset-4">Centered Column</div>

  </div>

</div>

Column Alignment

Align columns vertically and horizontally with Flexbox utilities.

Example: Column Alignment

<div class="container">

  <div class="row align-items-center">

    <div class="col">Aligned Center</div>

    <div class="col">Aligned Center</div>

  </div>

</div>

Gutters

Control the spacing between columns using .g-{number}.

Example: Gutter Spacing

<div class="container">

  <div class="row g-3">

    <div class="col">Column with space</div>

    <div class="col">Column with space</div>

  </div>

</div>

Tips and Best Practices

Use Containers: Always wrap rows in containers for proper alignment.

Combine Classes: Use combinations like .col-sm-6 col-lg-4 for responsive designs.

Test Responsiveness: Check layouts on various screen sizes.

Nest Carefully: Avoid deep nesting for better readability and performance.

Conclusion

The Bootstrap 5 Grid System provides a robust framework for building responsive and mobile-first layouts. By mastering its classes and features, you can create flexible designs that work seamlessly across all devices.