Day 4 Bootstrap5 Gutter and column Tutorial
Tags: bootstrap, tutorial
Published: 2022-08-10 05:13:46


In this tutorial we will know how to modify columns with a handful of options for alignment, ordering, and offsetting thanks to our flexbox grid system. Plus, see how to use column classes to manage widths of non-grid elements.

Generally, column element within a row using column width also using Bootstrap classes.


###Syntax for assign column 
```
Col-{size}-{Number}
```
{size} = The size of the viewport which you are targeting 

{number} = Number of columns you want the element to span 

**Note: SIZES includes none(<576), sm(>=≥576), md(>=768), lg(>=992), xl(>=1200), xxl(>=1400) Number Of columns : Any integer between 1 and 12.** 

Example: “cols-xs-12”

###How it works 

Columns build on the grid’s flexbox architecture. Flexbox means we have options for changing individual columns and modifying groups of columns at the row level. You choose how columns grow, shrink, or otherwise change.

When building grid layouts, all content goes in columns. The hierarchy of Bootstrap’s grid goes from container to row to column to your content. On rare occasions, you may combine content and column, but be aware there can be unintended consequences.

Bootstrap includes predefined classes for creating fast, responsive layouts. With six breakpoints and a dozen columns at each grid tier, we have dozens of classes already built for you to create your desired layouts. This can be disabled via Sass if you wish.


###Alignment
 
Use flexbox alignment utilities to vertically and horizontally align columns.

**Vertical alignment** 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgrkbvt63jtgsw8s9tip.png)
```
<div class="d-flex align-items-start bg-light mb-3" style="height: 100px;">
  <div class="col">One of three columns</div>
  <div class="col">One of three columns</div>
  <div class="col">One of three columns</div>
</div>
<div class="d-flex align-items-center bg-light mb-3" style="height: 100px;">
  <div class="col">One of three columns</div>
  <div class="col">One of three columns</div>
  <div class="col">One of three columns</div>
</div>
<div class="d-flex align-items-end bg-light mb-3" style="height: 100px;">
  <div class="col">One of three columns</div>
  <div class="col">One of three columns</div>
  <div class="col">One of three columns</div>
</div>
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n9vzfsvu4cdiy79aftvq.png)
```
<div class="row" style="height:10rem">
  <div class="col align-self-start">
    One of three columns
  </div>
  <div class="col align-self-center">
    One of three columns
  </div>
  <div class="col align-self-end">
    One of three columns
  </div>
</div>
```

**Horizontal alignment**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zdtlnp2mcx92ouydfcrz.png) 
```
<div class="row justify-content-start">
  <div class="col-4">
    One of two columns
  </div>
  <div class="col-4">
    One of two columns
  </div>
</div>
<div class="row justify-content-center">
  <div class="col-4">
    One of two columns
  </div>
  <div class="col-4">
    One of two columns
  </div>
</div>
<div class="row justify-content-end">
  <div class="col-4">
    One of two columns
  </div>
  <div class="col-4">
    One of two columns
  </div>
</div>
<div class="row justify-content-around">
  <div class="col-4">
    One of two columns
  </div>
  <div class="col-4">
    One of two columns
  </div>
</div>
<div class="row justify-content-between">
  <div class="col-4">
    One of two columns
  </div>
  <div class="col-4">
    One of two columns
  </div>
</div>
<div class="row justify-content-evenly">
  <div class="col-4">
    One of two columns
  </div>
  <div class="col-4">
    One of two columns
  </div>
</div>
```

**Column wrapping**
 
If more than 12 columns are placed within a single row, each group of extra columns will, as one unit, wrap onto a new line.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fivulknbn2q3xfrlmhvy.png)
```
<div class="row">
  <div class="col-9">.col-9</div>
  <div class="col-4">.col-4<br>Since 9 + 4 = 13 &gt; 12, this 4-column-wide div gets wrapped onto a new line as one contiguous unit.</div>
  <div class="col-6">.col-6<br>Subsequent columns continue along the new line.</div>
</div>
```

**Column Breaks**
 
Breaking columns to a new line in flexbox requires a small hack: add an element with width: 100% wherever you want to wrap your columns to a new line. Normally this is accomplished with multiple .rows, but not every implementation method can account for this.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/epfox2q1ck4w93wpzgjf.png)
```
<div class="row">
  <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>
  <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>

  <!-- Force next columns to break to new line -->
  <div class="w-100"></div>

  <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>
  <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>
</div>
```

###Reordering

**Order classes**

Use .order- classes for controlling the visual order of your content. These classes are responsive, so you can set the order by breakpoint (e.g., .order-1.order-md-2). Includes support for 1 through 5 across all six grid tiers.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j8k7pglxweutiv500dm1.png)
```
<div class="row">
  <div class="col">
    First in DOM, no order applied
  </div>
  <div class="col order-5">
    Second in DOM, with a larger order
  </div>
  <div class="col order-1">
    Third in DOM, with an order of 1
  </div>
</div>
```

There are also responsive .order-first and .order-last classes that change the order of an element by applying order: -1 and order: 6, respectively. These classes can also be intermixed with the numbered .order-* classes as needed.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j8k7pglxweutiv500dm1.png)
```
<div class="row">
  <div class="col order-last">
    First in DOM, ordered last
  </div>
  <div class="col">
    Second in DOM, unordered
  </div>
  <div class="col order-first">
    Third in DOM, ordered first
  </div>
</div>
```

###Offsetting columns

You can offset grid columns in two ways: our responsive .offset- grid classes and our margin utilities. Grid classes are sized to match columns while margins are more useful for quick layouts where the width of the offset is variable.

**Offset classes**

Move columns to the right using .offset-md-* classes. These classes increase the left margin of a column by * columns. For example, .offset-md-4 moves .col-md-4 over four columns.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nihwgx4pa20pys8z2qsz.png)
```
<div class="row">
  <div class="col-md-4">.col-md-4</div>
  <div class="col-md-4 offset-md-4">.col-md-4 .offset-md-4</div>
</div>
<div class="row">
  <div class="col-md-3 offset-md-3">.col-md-3 .offset-md-3</div>
  <div class="col-md-3 offset-md-3">.col-md-3 .offset-md-3</div>
</div>
<div class="row">
  <div class="col-md-6 offset-md-3">.col-md-6 .offset-md-3</div>
</div>
```

In addition to column clearing at responsive breakpoints, you may need to reset offsets. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4hc0h8sq0qydhx04q784.png)
```
<div class="row">
  <div class="col-sm-5 col-md-6">.col-sm-5 .col-md-6</div>
  <div class="col-sm-5 offset-sm-2 col-md-6 offset-md-0">.col-sm-5 .offset-sm-2 .col-md-6 .offset-md-0</div>
</div>
<div class="row">
  <div class="col-sm-6 col-md-5 col-lg-6">.col-sm-6 .col-md-5 .col-lg-6</div>
  <div class="col-sm-6 col-md-5 offset-md-2 col-lg-6 offset-lg-0">.col-sm-6 .col-md-5 .offset-md-2 .col-lg-6 .offset-lg-0</div>
</div>
```

###Margin utilities

With the move to flexbox, you can use margin utilities like .me-auto to force sibling columns away from one another.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4k2cwfjye0c74c0dqoq.png)
```
<div class="row">
  <div class="col-md-4">.col-md-4</div>
  <div class="col-md-4 ms-auto">.col-md-4 .ms-auto</div>
</div>
<div class="row">
  <div class="col-md-3 ms-md-auto">.col-md-3 .ms-md-auto</div>
  <div class="col-md-3 ms-md-auto">.col-md-3 .ms-md-auto</div>
</div>
<div class="row">
  <div class="col-auto me-auto">.col-auto .me-auto</div>
  <div class="col-auto">.col-auto</div>
</div>
```

**Standalone column classes**

The .col-* classes can also be used outside a .row to give an element a specific width. Whenever column classes are used as non direct children of a row, the paddings are omitted.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3cijqm3vkfd5zd7umq5j.png)
```
<div class="col-3 p-3 border">
  .col-3: width of 25%
</div>
<div class="col-sm-9 p-3 border">
  . col-sm-9: width of 75% above sm breakpoint
</div>
```

The classes can be used together with utilities to create responsive floated images. Make sure to wrap the content in a .clear fix wrapper to clear the float if the text is shorter.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4kc7zpbhqls1ue113l8p.png)
```
<div class="clearfix">
  <img src="..." class="col-md-6 float-md-end mb-3 ms-md-3" alt="...">

  <p>
    A paragraph of placeholder text. We're using it here to show the use of the clearfix class. We're adding quite a few meaningless phrases here to demonstrate how the columns interact here with the floated image.
  </p>

  <p>
    As you can see the paragraphs gracefully wrap around the floated image. Now imagine how this would look with some actual content in here, rather than just this boring placeholder text that goes on and on, but actually conveys no tangible information at. It simply takes up space and should not really be read.
  </p>

  <p>
    And yet, here you are, still persevering in reading this placeholder text, hoping for some more insights, or some hidden easter egg of content. A joke, perhaps. Unfortunately, there's none of that here.
  </p>
</div>
```

###Gutters 

Gutters are the padding between your columns, used to responsively space and align content in the Bootstrap grid system.

**How they work**

Gutters are the gaps between column content, created by horizontal padding. We set padding-right and padding-left on each column, and use negative margin to offset that at the start and end of each row to align content.

Gutters start at 1.5rem (24px) wide. This allows us to match our grid to the padding and margin spacers scale.
Gutters can be responsively adjusted. Use breakpoint-specific gutter classes to modify horizontal gutters, vertical gutters, and all gutters.

**Horizontal gutters**

.gx-* classes can be used to control the horizontal gutter widths. The .container or .container-fluid parent may need to be adjusted if larger gutters are used too to avoid unwanted overflow, using a matching padding utility. For example, in the following example we’ve increased the padding with .px-4:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mpawzpe5xockaxvgtwbr.png)
```
<div class="container px-4">
  <div class="row gx-5">
    <div class="col">
      <div class="p-3">Custom column padding</div>
    </div>
    <div class="col">
      <div class="p-3">Custom column padding</div>
    </div>
  </div>
</div>
```

Alternative solution is to add a wrapper around the. row with the. overflow-hidden class:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7t2l5pcdks40d1xfa9b.png)
```
<div class="container overflow-hidden">
  <div class="row gx-5">
    <div class="col">
      <div class="p-3">Custom column padding</div>
    </div>
    <div class="col">
      <div class="p-3">Custom column padding</div>
    </div>
  </div>
</div>
```

**Vertical gutters**

.gy-* classes can be used to control the vertical gutter widths. Like the horizontal gutters, the vertical gutters can cause some overflow below the .row at the end of a page. If this occurs, you add a wrapper around .row with the .overflow-hidden class:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w49kx50nv3irjgslhbw6.png)
```
<div class="container overflow-hidden">
  <div class="row gy-5">
    <div class="col-6">
      <div class="p-3">Custom column padding</div>
    </div>
    <div class="col-6">
      <div class="p-3">Custom column padding</div>
    </div>
    <div class="col-6">
      <div class="p-3">Custom column padding</div>
    </div>
    <div class="col-6">
      <div class="p-3">Custom column padding</div>
    </div>
  </div>
</div>
```

**Horizontal & vertical gutters**

.g-* classes can be used to control the horizontal gutter widths, for the following example we use a smaller gutter width, so there won’t be a need to add the .overflow-hidden wrapper class.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rsxzk7mmbr528zdpxw84.png)
```
<div class="container">
  <div class="row g-2">
    <div class="col-6">
      <div class="p-3">Custom column padding</div>
    </div>
    <div class="col-6">
      <div class="p-3">Custom column padding</div>
    </div>
    <div class="col-6">
      <div class="p-3">Custom column padding</div>
    </div>
    <div class="col-6">
      <div class="p-3">Custom column padding</div>
    </div>
  </div>
</div>
```

**Row columns gutters**

Gutter classes can also be added to row columns. In the following example, we use responsive row columns and responsive gutter classes.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/shp3kxumze4m8vr4tkga.png)
```
<div class="container">
  <div class="row row-cols-2 row-cols-lg-5 g-2 g-lg-3">
    <div class="col">
      <div class="p-3">Row column</div>
    </div>
    <div class="col">
      <div class="p-3">Row column</div>
    </div>
    <div class="col">
      <div class="p-3">Row column</div>
    </div>
    <div class="col">
      <div class="p-3">Row column</div>
    </div>
    <div class="col">
      <div class="p-3">Row column</div>
    </div>
    <div class="col">
      <div class="p-3">Row column</div>
    </div>
    <div class="col">
      <div class="p-3">Row column</div>
    </div>
    <div class="col">
      <div class="p-3">Row column</div>
    </div>
    <div class="col">
      <div class="p-3">Row column</div>
    </div>
    <div class="col">
      <div class="p-3">Row column</div>
    </div>
  </div>
</div>
```

**No gutters**

The gutters between columns in our predefined grid classes can be removed with .g-0. This removes the negative margins from .row and the horizontal padding from all immediate children columns.
Need an edge-to-edge design? Drop the parent .container or .container-fluid.

In practice, here’s how it looks. Note you can continue to use this with all other predefined grid classes (including column widths, responsive tiers, reorders, and more).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5jqz6r6kwceg0w99nz0q.png)
```
<div class="row g-0">
  <div class="col-sm-6 col-md-8">.col-sm-6 .col-md-8</div>
  <div class="col-6 col-md-4">.col-6 .col-md-4</div>
</div>
```

**Change the gutters**

Classes are built from the $gutters Sass map which is inherited 
```
$grid-gutter-width: 1.5rem;
$gutters: (
  0: 0,
  1: $spacer * .25,
  2: $spacer * .5,
  3: $spacer,
  4: $spacer * 1.5,
  5: $spacer * 3,
);
```