Flex
Quickly manage and align elements with flex utilities.
Syntax
Add .d-flex or .d-inline-flex to the parent, then combine direction, alignment and wrapping utilities on that container. Apply sizing, self-alignment and order utilities to its direct children. Use Spacings for gaps between items.
<div class="d-flex flex-column flex-md-row align-items-md-center gap-2">
<div class="flex-grow-1">Project overview</div>
<div class="flex-shrink-0">Updated today</div>
</div>The main axis follows flex-direction: horizontal for a row and vertical for a column. justify-content-* works along that axis; align-items-* works across it. Row direction also follows the document’s writing direction.
Class reference
The following families are provided by the flex, display and order utilities. See the previews below for individual values.
| Class or pattern | Effect |
|---|---|
.d-flex, .d-inline-flex |
Creates a block or inline flex container. |
.flex-{direction} |
Sets row, row-reverse, column or column-reverse direction. |
.justify-content-{alignment} |
Aligns items along the main axis: start, end, center, between, around or evenly. |
.align-items-{alignment} |
Aligns items across the main axis: start, end, center, baseline or stretch. |
.align-self-{alignment} |
Overrides cross-axis alignment for one item; accepts the align-items values and auto. |
.align-content-{alignment} |
Distributes wrapped lines: start, end, center, between, around, evenly or stretch. Requires extra cross-axis space. |
.flex-wrap, .flex-nowrap |
Allows multiple lines or keeps items on one line. |
.flex-1 |
Sets flex: 1 1 0 to share available space from a zero basis. |
.flex-fill |
Sets flex: 1 1 auto to share available space while accounting for content size. |
.flex-grow-{n}, .flex-shrink-{n} |
Enables (1) or disables (0) growing or shrinking for an item. |
.order-{n}, .order-first, .order-last |
Sets visual order from 0 to 5, before other items (-1), or after them (6). Also works on grid items. |
Enable flex behavior
Use .d-flex to enable flex behavior. Direct children will become flex items.
<div class="d-flex">I am a flexbox container</div>Use .d-inline-flex to create an inline flex container.
<div class="d-inline-flex">I am an inline flexbox container</div>Direction
Use direction utilities to change the direction of flex items.
<div class="d-flex flex-row">...</div>
<div class="d-flex flex-row-reverse">...</div>
<div class="d-flex flex-column">...</div>
<div class="d-flex flex-column-reverse">...</div>Justify content
Use justify content utilities to align flex items on the main axis.
<div class="d-flex justify-content-start">...</div>
<div class="d-flex justify-content-end">...</div>
<div class="d-flex justify-content-center">...</div>
<div class="d-flex justify-content-between">...</div>
<div class="d-flex justify-content-around">...</div>
<div class="d-flex justify-content-evenly">...</div>Align items
Use align items utilities to align flex items on the cross axis.
<div class="d-flex align-items-start">...</div>
<div class="d-flex align-items-end">...</div>
<div class="d-flex align-items-center">...</div>
<div class="d-flex align-items-baseline">...</div>
<div class="d-flex align-items-stretch">...</div>Align self
Use align self utilities on individual flex items to override the container’s align-items value. Use .align-self-auto to restore the default item alignment.
<div class="d-flex align-items-stretch">
<div class="align-self-start">...</div>
<div class="align-self-center">...</div>
<div class="align-self-end">...</div>
</div>Available align self utilities:
.align-self-auto
.align-self-start
.align-self-end
.align-self-center
.align-self-baseline
.align-self-stretchAlign content
Use align content utilities to distribute multiple flex lines on the cross axis. The container must allow wrapping and have extra cross-axis space for the distribution to be visible.
<div class="d-flex flex-wrap align-content-between">
<div>...</div>
<div>...</div>
<div>...</div>
<div>...</div>
</div>Available align content utilities:
.align-content-start
.align-content-end
.align-content-center
.align-content-between
.align-content-around
.align-content-evenly
.align-content-stretchWrap
Use wrapping utilities to control whether flex items wrap onto multiple lines.
<div class="d-flex flex-wrap">...</div>
<div class="d-flex flex-nowrap">...</div>Fill
Use .flex-fill to force a flex item to fill the available space.
<div class="d-flex">
<div>Item</div>
<div class="flex-fill">flex-fill</div>
<div>Item</div>
</div>Grow and shrink
Use grow and shrink utilities to control how flex items resize inside a flex container.
<div class="d-flex">
<div>Item</div>
<div class="flex-grow-1">flex-grow-1</div>
<div>Item</div>
</div>
<div class="d-flex">
<div class="flex-shrink-0">flex-shrink-0</div>
<div>This item can shrink.</div>
</div>Available grow utilities
.flex-grow-0
.flex-grow-1Available shrink utilities
.flex-shrink-0
.flex-shrink-1Order
Use order utilities to change the visual order of flex items. Reading and keyboard focus order still follow the HTML source; keep the source order meaningful. The same caution applies to reversed flex directions.
<div class="d-flex">
<div class="order-3">1 order-3</div>
<div class="order-1">2 order-1</div>
<div class="order-2">3 order-2</div>
</div>Available order utilities
.order-0
.order-1
.order-2
.order-3
.order-4
.order-5
.order-first
.order-lastResponsive behavior
Breakpoint classes apply from their minimum width upward. Use sm, md, lg, xl or xxl; see Breakpoints for the default widths. Larger active breakpoints win regardless of class order in the markup.
Insert the breakpoint as shown below. The grow and shrink families place it before grow or shrink.
| Pattern | Example |
|---|---|
.d-{breakpoint}-flex, .d-{breakpoint}-inline-flex |
.d-md-flex |
.flex-{breakpoint}-{direction} |
.flex-md-row |
.justify-content-{breakpoint}-{alignment} |
.justify-content-md-between |
.align-items-{breakpoint}-{alignment} |
.align-items-md-center |
.align-self-{breakpoint}-{alignment} |
.align-self-md-end |
.align-content-{breakpoint}-{alignment} |
.align-content-md-between |
.flex-{breakpoint}-wrap, .flex-{breakpoint}-nowrap |
.flex-md-wrap |
.flex-{breakpoint}-fill |
.flex-md-fill |
.flex-{breakpoint}-grow-{n}, .flex-{breakpoint}-shrink-{n} |
.flex-md-grow-0, .flex-md-shrink-0 |
.order-{breakpoint}-{value} |
.order-md-0, .order-md-first, .order-md-last |
The same values listed in the class reference apply to each responsive family. .flex-1 has no breakpoint variants; use the responsive fill, grow or shrink utilities when item sizing needs to change.
<div class="d-flex flex-column flex-md-row align-items-md-center justify-content-md-between gap-2">
<div>Project overview</div>
<div>Updated today</div>
</div>The items stack below md. From md upward, they share a row, align vertically in its center and move to opposite ends of the available width.
Recipes
Responsive action bar
Stack the description and actions on small screens, then place them side by side from md upward. Let the action area wrap so long labels still fit.
Review the release notes before publishing.
<div class="d-flex flex-column flex-md-row align-items-md-center gap-3">
<p class="flex-grow-1 mb-0">Review the release notes before publishing.</p>
<div class="d-flex flex-wrap flex-shrink-0 gap-2">
<button class="btn btn-outline btn-primary" type="button">Save draft</button>
<button class="btn btn-solid btn-primary" type="button">Publish</button>
</div>
</div>The Button controls demonstrate layout; connect their actions in your application.
Equal-width summary items
Use .flex-1 on each item to divide the available width equally. Add .min-w-0 from Sizing so long content can shrink, and allow long words to wrap with Typography utilities.
<div class="d-flex flex-column flex-md-row gap-2">
<div class="flex-1 min-w-0 p-2 bg-primary rounded overflow-wrap-anywhere">
Design review: 4 tasks
</div>
<div class="flex-1 min-w-0 p-2 bg-secondary rounded overflow-wrap-anywhere">
Implementation: 12 tasks
</div>
</div>