Bootstrap Flexbox Utilities
Introduction to Flexbox in Bootstrap
Bootstrap provides a comprehensive set of utility classes for working with Flexbox, making it easy to create flexible layouts without writing custom CSS.
- Create flexible, responsive layouts
- Easily align and distribute content
- Control the direction of elements
- Adjust spacing between elements
- Reorder elements without changing the HTML structure
Enabling Flex Containers
To create a flex container, use the .d-flex
or .d-inline-flex
utility classes:
<!-- Block-level flex container -->
<div class="d-flex">
<div>Flex item 1</div>
<div>Flex item 2</div>
<div>Flex item 3</div>
</div>
<!-- Inline flex container -->
<div class="d-inline-flex">
<div>Flex item 1</div>
<div>Flex item 2</div>
<div>Flex item 3</div>
</div>
Example:
Responsive Flex Containers
Bootstrap's flex utilities can be applied responsively using breakpoint-specific classes:
<div class="d-flex d-sm-block d-md-flex">
<!-- Flex on xs and md+, block on sm -->
</div>
<div class="d-block d-lg-flex">
<!-- Block until lg breakpoint, then flex -->
</div>
Flex Direction
Control the direction of flex items with .flex-row
, .flex-row-reverse
, .flex-column
, and .flex-column-reverse
:
<div class="d-flex flex-row">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
<div class="d-flex flex-row-reverse">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
<div class="d-flex flex-column">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
<div class="d-flex flex-column-reverse">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
Examples:
Responsive Direction
<div class="d-flex flex-column flex-sm-row">
<!-- Column on xs, row on sm and up -->
</div>
Justify Content
Control how flex items are positioned along the main axis with .justify-content-*
utilities:
<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>
Examples:
Align Items
Control how flex items are aligned along the cross axis with .align-items-*
utilities:
<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>
Examples:
Align Self
Override the alignment for individual flex items with .align-self-*
utilities:
<div class="d-flex" style="height: 100px;">
<div class="align-self-start">Start</div>
<div class="align-self-end">End</div>
<div class="align-self-center">Center</div>
<div class="align-self-baseline">Baseline</div>
<div class="align-self-stretch">Stretch</div>
</div>
Example:
Fill and Grow/Shrink
Flex Fill
Use .flex-fill
to force items to take up equal width:
<div class="d-flex">
<div class="flex-fill">Flex fill item</div>
<div class="flex-fill">Flex fill item</div>
<div class="flex-fill">Flex fill item</div>
</div>
Example:
Grow and Shrink
Use .flex-grow-*
and .flex-shrink-*
utilities to control how flex items grow and shrink:
<div class="d-flex">
<div class="flex-grow-1">Grow</div>
<div>Fixed</div>
<div>Fixed</div>
</div>
<div class="d-flex">
<div class="w-100">Large content</div>
<div class="flex-shrink-1">Shrink</div>
<div class="flex-shrink-0">Don't shrink</div>
</div>
Example:
Order
Change the visual order of flex items with .order-*
utilities:
<div class="d-flex">
<div class="order-3">First in DOM, displayed third</div>
<div class="order-2">Second in DOM, displayed second</div>
<div class="order-1">Third in DOM, displayed first</div>
</div>
Example:
Responsive Order
<div class="d-flex">
<div class="order-1 order-md-2">First on xs, second on md</div>
<div class="order-2 order-md-1">Second on xs, first on md</div>
</div>
Flex Wrap
Control how flex items wrap with .flex-wrap
, .flex-nowrap
, and .flex-wrap-reverse
:
<div class="d-flex flex-wrap">
<!-- Items will wrap to the next line when needed -->
</div>
<div class="d-flex flex-nowrap">
<!-- Items will not wrap (default) -->
</div>
<div class="d-flex flex-wrap-reverse">
<!-- Items will wrap in reverse order -->
</div>
Example:
Common Flexbox Patterns
1. Centered Content
<div class="d-flex justify-content-center align-items-center" style="height: 200px;">
<div>Perfectly centered content</div>
</div>
2. Sticky Footer
<div class="d-flex flex-column min-vh-100">
<header>Header</header>
<main class="flex-grow-1">Content that will push the footer down</main>
<footer>Footer</footer>
</div>
3. Equal-Height Cards
<div class="row row-cols-1 row-cols-md-3 g-4">
<div class="col">
<div class="card h-100">
<div class="card-body d-flex flex-column">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some content.</p>
<a href="#" class="btn btn-primary mt-auto">Button at bottom</a>
</div>
</div>
</div>
<!-- More cards... -->
</div>
Best Practices
- Use
.d-flex
on the parent container to create a flex container - Use responsive flex utilities to create layouts that adapt to different screen sizes
- Combine flex utilities with other Bootstrap utilities like spacing and sizing for more control
- Use
.flex-grow-1
to make an element take up remaining space - Use
.align-self-*
utilities to override alignment for specific items - Remember that flex utilities work with both rows and columns