Flexbox Utilities & Alignment
Bootstrap 5 is built on CSS Flexbox. Beyond the grid system, Bootstrap provides an exhaustive suite of Flexbox utility classes that allow you to quickly manage element distribution, direction, horizontal and vertical alignment, wrapping, and growing behaviors directly in HTML.
In this lesson, you will master d-flex, flex direction controls, content justification, cross-axis alignment, flex item growth/shrink, and responsive flex modifiers.
┌─────────────────────────────────────────────────────────────────────────────┐
│ Flexbox Alignment Quick Reference │
├─────────────────────────────────────────────────────────────────────────────┤
│ Justify Content (Main Axis): │
│ [start ] [ center ] [ end] [start end] │
│ .justify-content-start .center .end .between │
│ │
│ Align Items (Cross Axis): │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │▲ Top │ │ │ │ │ │▲ Top │ │
│ │ │ │ Center │ │ │ ││ Stretch │ │
│ │ │ │ │ │▼ Bottom │ │▼ │ │
│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │
│ .align-items-start .center .end .stretch │
└─────────────────────────────────────────────────────────────────────────────┘
1. Enabling Flexbox (d-flex & d-inline-flex)
Convert any container into a flex container with d-flex or d-inline-flex:
<!-- Full-width flex container -->
<div class="d-flex p-3 bg-light border">
<div>Flex Item 1</div>
<div>Flex Item 2</div>
<div>Flex Item 3</div>
</div>
<!-- Inline flex container -->
<div class="d-inline-flex p-2 bg-secondary text-white rounded">
<span>Inline Flex Element</span>
</div>
2. Flex Direction
Control the flow direction of flex items with flex-row and flex-column:
<!-- Horizontal row (default) -->
<div class="d-flex flex-row bg-light p-2 mb-3">
<div class="p-2 border">Item 1</div>
<div class="p-2 border">Item 2</div>
</div>
<!-- Horizontal reversed row -->
<div class="d-flex flex-row-reverse bg-light p-2 mb-3">
<div class="p-2 border">Item 1 (Rightmost)</div>
<div class="p-2 border">Item 2</div>
</div>
<!-- Vertical column -->
<div class="d-flex flex-column bg-light p-2 mb-3">
<div class="p-2 border">Top Item</div>
<div class="p-2 border">Bottom Item</div>
</div>
3. Justify Content (Main Axis Alignment)
Align flex items along the main axis using .justify-content-*:
justify-content-start(left / default)justify-content-end(right)justify-content-center(centered)justify-content-between(first item start, last item end, equal gap)justify-content-around(equal space around each item)justify-content-evenly(strictly equal space between and outside items)
<!-- Spaced navbar layout: Logo on left, navigation on right -->
<div class="d-flex justify-content-between align-items-center bg-dark text-white p-3">
<span class="fw-bold">MyBrand</span>
<nav class="d-flex gap-2">
<a href="#" class="text-white text-decoration-none">Home</a>
<a href="#" class="text-white text-decoration-none">Projects</a>
</nav>
</div>
4. Align Items & Align Self (Cross Axis Alignment)
Control cross-axis alignment for all items via .align-items-* or individually with .align-self-*:
<div class="d-flex align-items-center bg-light border" style="height: 120px;">
<div class="p-2 border bg-white">Vertically Centered</div>
<div class="p-2 border bg-white align-self-start">Top Aligned (Self)</div>
<div class="p-2 border bg-white align-self-end">Bottom Aligned (Self)</div>
</div>
5. Flex Grow, Shrink & Fill
Control how flex items expand to fill unused space or shrink under constrained widths:
<div class="d-flex bg-light p-2">
<div class="p-2 border bg-white flex-grow-1">Expands to fill all remaining space (flex-grow-1)</div>
<div class="p-2 border bg-white">Fixed Width Item</div>
</div>
<div class="d-flex bg-light p-2 mt-3">
<!-- flex-fill forces equal proportional width on all siblings -->
<div class="p-2 border bg-white flex-fill text-center">Item A</div>
<div class="p-2 border bg-white flex-fill text-center">Item B</div>
<div class="p-2 border bg-white flex-fill text-center">Item C</div>
</div>
6. Auto Margins in Flexbox
Auto margins (ms-auto, me-auto, my-auto) in flexbox push adjacent elements to opposite edges:
<div class="d-flex bg-light p-3">
<div class="p-2">Left Brand</div>
<!-- ms-auto pushes this item and subsequent items all the way to the right -->
<div class="ms-auto p-2">
<button class="btn btn-primary btn-sm">Login</button>
</div>
</div>
7. Multi-Line Flex Wrapping & align-content
When flex items wrap across multiple lines (flex-wrap), align rows along the cross-axis using .align-content-*:
<div class="d-flex flex-wrap align-content-between bg-light border p-3" style="height: 200px;">
<div class="p-2 border bg-white" style="width: 45%;">Row 1 Item A</div>
<div class="p-2 border bg-white" style="width: 45%;">Row 1 Item B</div>
<div class="p-2 border bg-white" style="width: 45%;">Row 2 Item A</div>
<div class="p-2 border bg-white" style="width: 45%;">Row 2 Item B</div>
</div>
Summary & Key Takeaways
d-flexturns any element into a flexbox container.flex-rowandflex-columnset main axis flow direction.justify-content-*aligns items horizontally along the main axis.align-items-*andalign-self-*control vertical cross-axis alignment.align-content-*controls multi-line row spacing whenflex-wrapis enabled.flex-grow-1expands an item to fill remaining container space, andms-autoacts as a powerful push spacer.
Best Practices & Senior Guidance
- Leverage
ms-autofor Navbars: Usingms-autoon the rightmost nav item is cleaner than creating multiple nested wrapper divs. - Combine
d-flexwithgap-*: Usegap-2orgap-3instead of manual margin utilities to maintain clean responsive flex child spacing. - Set
flex-shrink-0on Avatars & Fixed Badges: Prevents flex child icons from shrinking when sibling text expands.