Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 58 ยท Layout

Gutters, Alignment and Ordering

Control the space between Bootstrap columns with g-*, gx-* and gy-*, align and justify columns, reorder them with order-*, and push them with offset-* and ms-auto.

Once columns have the right widths, the next questions are about the spaces and positions around them. How much room sits between the columns? What happens when one card is taller than its neighbours? How do you center a single column, push a button to the far right, or show the sidebar first on phones and last on desktops?

Bootstrap answers all of these with short classes on the row and its columns. This lesson covers gutters (g-*, gx-*, gy-*), alignment (align-items-*, justify-content-*, align-self-*), ordering (order-*), and pushing columns around with offset-* and auto margins. It builds on Responsive Columns.

Gutters: the space between columns

A gutter is the gap between neighbouring columns. Bootstrap makes it with padding: every column has horizontal padding of half the gutter on each side, and the row has an equal negative margin so the outer edges still line up with the container.

By default the gutter is 1.5 rem horizontally. Three groups of classes change it, all on the row:

Class Changes
g-0 to g-5 horizontal and vertical gutters together
gx-0 to gx-5 the horizontal gutter only
gy-0 to gy-5 the vertical gutter only

The numbers come from the same spacing scale used by margins and padding (0, 0.25, 0.5, 1, 1.5 and 3 rem, explained in Spacing Utilities). g-0 removes the gutters entirely, and every class has responsive versions such as g-md-4.

The vertical gutter matters when columns wrap. A default row has no vertical space between wrapped lines, so stacked columns touch. gy-* (or g-*) adds it.

Comparing gutter sizes

Look at the last row: g-5 leaves a wide gap both between A and B and between the two lines. If you only wanted the vertical gap you would write gy-5.

Why g-0 is useful. Edge-to-edge layouts, such as an image beside a text panel, want columns that touch. Put g-0 on the row and the padding and negative margins disappear together, so nothing overflows the container.

Under the hood, gutters are CSS variables: --bs-gutter-x and --bs-gutter-y. The g-* classes simply set them on the row. You will see how to read and override such variables in Customizing with CSS Variables.

Vertical alignment

A row is a flex container, and its columns stretch to the same height by default. When content differs in height, you choose where each column sits using flexbox alignment:

  • align-items-start, align-items-center, align-items-end on the row align all columns.
  • align-self-start, align-self-center, align-self-end on a single column override the row for that one.

The row only has spare vertical space when it is taller than its tallest column. In the example below, a min-height gives it that room.

Aligning columns vertically

Horizontal alignment

When columns do not add up to 12, the leftover space sits on the right by default. justify-content-* on the row decides where it goes:

Class Effect
justify-content-start columns hug the left (default)
justify-content-center columns centered
justify-content-end columns hug the right
justify-content-around equal space around each column
justify-content-between first and last at the edges, space between
justify-content-evenly equal space between all, including the edges
Horizontal alignment with justify-content

Centering a single column is the most common use: <div class="row justify-content-center"><div class="col-md-6">...</div></div> gives a centered form or card that is half the row from md upward.

flowchart TD
  R["Row (flex container)"] --> H["justify-content-*<br/>moves columns along the row"]
  R --> V["align-items-*<br/>moves columns up and down"]
  C["Single column"] --> S["align-self-*<br/>overrides the row for this column"]

Row classes align every column; self classes override one.

Changing the order

Columns appear in the order they are written in the HTML, and that order is also the reading order for screen readers and keyboards. The order-* classes change only the visual order, using the flexbox order property.

  • order-0 to order-5 set a position number; smaller numbers come first.
  • order-first jumps to the front and order-last to the end.
  • Every class has responsive versions, such as order-md-first or order-lg-2.

A typical use is a page where the sidebar comes after the content in HTML (better for reading order) but should appear on the left on large screens:

Sidebar first on large screens

Reordering is visual only. Keyboard focus and screen readers still follow the HTML order. If the visual and source order disagree a lot, people who navigate with a keyboard will jump around the page unexpectedly. Keep order-* for small adjustments and put the most important content first in the source.

Offsets and auto margins

An offset pushes a column to the right by leaving empty columns before it. offset-md-3 adds a left margin worth three columns from the md breakpoint up. Together with a width, it centers a column without justify-content: a col-md-6 with offset-md-3 has three empty columns on each side.

Offsets go from offset-1 to offset-11, with responsive versions, and offset-md-0 cancels an offset from a smaller breakpoint.

A more flexible tool is the auto margin. The flex utilities ms-auto (margin-start) and me-auto (margin-end) make a margin soak up all spare space, so a column or item gets pushed to the opposite side:

offset-* and ms-auto

Use offsets when you think in grid units ("leave two columns empty"), and auto margins when you think "push this to the far side". The same ms-auto trick appears again in navbars in The Navbar.

Column wrapping

If the columns in one row add up to more than 12, the extras do not shrink or overflow; they wrap onto the next line. This is by design and is how a list of cards becomes a grid.

You can also force a break. An empty element with w-100 fills the row's whole width, so whatever follows has to start on a new line:

HTML
<div class="row">
  <div class="col-6">1</div>
  <div class="col-6">2</div>
  <div class="w-100"></div>
  <div class="col-6">3</div>
  <div class="col-6">4</div>
</div>

Here the divider is not needed (6 + 6 already fills the row), but with col items of automatic width it creates a break exactly where you want. To make the break appear only on some screens, use d-none d-md-block on it; display classes are covered in Display and Visibility.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which class on a row removes both the horizontal and vertical gutters?

    g-0 sets both gutters to zero. gx-0 and gy-0 change only one direction, and no-gutters was Bootstrap 4's old name.
  2. 2. Stacked columns touch each other with no gap. What is the simplest fix?

    A default row has no vertical gutter. gy-* adds space between wrapped lines.
  3. 3. You write the sidebar after the article in the HTML but want it on the left from lg. Which class goes on the sidebar column?

    order-lg-first moves the column to the front visually from the lg breakpoint. The HTML order, which screen readers use, stays the same.
  4. 4. What does ms-auto do on a column inside a row?

    An auto margin absorbs the free space in a flex container, so the column moves to the opposite side.
  5. 5. What happens when the columns in a row add up to more than 12?

    Rows wrap their columns, which is how card lists turn into grids.

Summary

  • Gutters are the gaps between columns: g-* for both directions, gx-* horizontal, gy-* vertical, g-0 for none. Put them on the row.
  • align-items-* and justify-content-* on the row align all columns; align-self-* overrides one column.
  • order-* (including order-first and order-last) changes the visual order only, not the reading order.
  • offset-* leaves empty columns on the left, and ms-auto / me-auto push a column to a side.
  • Columns beyond 12 wrap, and a w-100 element forces a break.

Next lesson: Nested Grids and Full Layouts.