Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 58 ยท Utilities

Flex Utilities

Use Bootstrap 5.3 flex utilities: d-flex, direction, justify-content, align-items, wrap, grow and shrink, fill, auto margins and common centering patterns.

Flexbox is the layout tool you will reach for most often: toolbars, card footers, navigation bars, centered messages, form rows. The Bootstrap grid is built on it, but the flex utilities let you use flexbox directly on any element, without a row and columns.

Each class maps to one flexbox property, so this lesson doubles as a short flexbox course. You will learn how to turn a container into a flex container, set its direction, align items on both axes, wrap, control how items grow and shrink, and finish with the centering patterns you will use every week.

Making a flex container

Add d-flex (or d-inline-flex) to the parent. Its direct children become flex items and line up in a row:

d-flex

Two axes drive everything. The main axis is the direction items flow in (horizontal for a row). The cross axis runs perpendicular to it. justify-content-* works along the main axis, align-items-* along the cross axis.

flowchart LR
  P["d-flex container (row)"] --> M["Main axis, horizontal<br/>justify-content-*"]
  P --> X["Cross axis, vertical<br/>align-items-*"]

In a row, the main axis is horizontal and the cross axis vertical; a column swaps them.

Direction

  • flex-row (default), flex-row-reverse
  • flex-column, flex-column-reverse

All have breakpoint versions, such as flex-column flex-md-row, which stacks on phones and lines up from medium screens. Reverse classes change the visual order only; keyboard and screen reader order still follows the HTML, so avoid reversing meaningful content.

Justify content (main axis)

justify-content-start (default), -end, -center, -between, -around and -evenly decide where free space goes.

justify-content

between puts the first item at the start and the last at the end, with equal gaps between. around gives each item equal space on both sides, so the edge gaps are half the inner gaps. evenly makes every gap identical, edges included.

Align items (cross axis)

align-items-start, -end, -center, -baseline and -stretch (default) position items across the axis. The container needs spare cross-axis space to show a difference, so give it a height:

align-items

align-self-* is the per-item version of align-items-*, so one item can break from the group. A fourth property, align-content-*, spaces out multiple lines of wrapped items and has an effect only when flex-wrap is on and the container is taller than its content.

Wrapping

By default a flex container keeps everything on one line and shrinks items to fit. Add flex-wrap to allow new lines (flex-nowrap is the default, flex-wrap-reverse wraps upward). Combined with gap-* from Spacing Utilities it is a quick way to build tag lists and button rows that reflow on narrow screens.

Grow, shrink and fill

By default items have flex-grow: 0 (do not stretch) and flex-shrink: 1 (may shrink).

  • flex-grow-1 lets an item take all the spare space; flex-grow-0 stops that.
  • flex-shrink-1 and flex-shrink-0: use flex-shrink-0 on things that must not squash, like avatars or icons next to long text.
  • flex-fill makes items take equal-ish space based on their content (flex: 1 1 auto), so wider content gets a wider share. This is different from equal columns in the grid, where widths ignore content.
flex-grow-1 and flex-fill

Order

order-0 to order-5, plus order-first and order-last, change visual order, with breakpoint versions (order-md-first). The same accessibility caution applies as for reversed directions: the order for keyboards and screen readers does not change.

Auto margins in flex containers

In a flex container, an auto margin absorbs free space. That gives a tidy trick without justify-content: put ms-auto on the first item that should move to the end, and everything from there on is pushed right.

Toolbar with ms-auto

ms-auto on the first button pushes both buttons to the end. Use me-auto on an item to pin the following items to the end instead.

Centering patterns

Two properties center almost anything: justify-content-center on the main axis and align-items-center on the cross axis. Together they center a single child exactly. The container must have a height for vertical centering to show, for example a fixed height, min-vh-100, or h-100 inside a tall parent.

Perfect centering

Other common patterns:

  • Stack on phones, row from md up: d-flex flex-column flex-md-row.
  • Equal-height cards: a flex row stretches children by default (align-items-stretch).
  • Sticky footer: a d-flex flex-column min-vh-100 wrapper with flex-grow-1 on the <main> pushes the footer to the bottom even on short pages.
  • Media object: an image with flex-shrink-0 next to text with flex-grow-1.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which classes center a single child both horizontally and vertically inside a tall container?

    justify-content-center centers along the main axis and align-items-center along the cross axis. The container needs a height for the vertical part.
  2. 2. In a flex-row container, which property aligns items vertically?

    In a row the cross axis is vertical, and align-items-* works on the cross axis.
  3. 3. What is the effect of ms-auto on a flex item?

    Auto margins absorb free space in a flex container, so ms-auto pushes the item to the end side.
  4. 4. Why use flex-shrink-0 on an avatar next to long text?

    Flex items may shrink by default. flex-shrink-0 keeps the avatar at its set size while the text wraps.
  5. 5. Which class makes the items of a flex container wrap onto new lines?

    flex-wrap allows items to move to a new line. The default, flex-nowrap, keeps them on one line.

Summary

  • d-flex makes a container; its children become flex items along a main and a cross axis.
  • flex-row/flex-column (and -reverse) set the direction; add breakpoints, as in flex-md-row.
  • justify-content-* spaces items along the main axis, align-items-* and align-self-* along the cross axis, align-content-* between wrapped lines.
  • flex-wrap, flex-grow-*, flex-shrink-*, flex-fill and order-* control wrapping, sizing and order.
  • Auto margins (ms-auto, me-auto) push items apart; justify-content-center align-items-center centers.

Next lesson: Borders, Radius and Shadows.