Flexbox is a modern CSS layout system that provides a flexible and efficient way to design responsive layouts. Bootstrap 5 leverages Flexbox to simplify layout creation with its built-in Flex utilities, giving developers control over alignment, distribution, and ordering of elements.
In this guide, we’ll cover everything you need to know about using Flex utilities in Bootstrap 5, complete with practical examples to help you master responsive layouts.
Why Use Bootstrap 5 Flex Utilities?
Bootstrap 5 Flex utilities allow you to:
- Build responsive layouts without writing custom CSS.
- Align, justify, and reorder elements with ease.
- Simplify the creation of complex layouts.
These utilities are built on the Flexbox module, ensuring cross-browser compatibility and a smooth development process.
Core Bootstrap 5 Flex Classes
Here are the key Flex utilities available in Bootstrap 5:
1. Display Classes
Use these classes to define the Flex container.
d-flex
: Makes an element a Flex container.d-inline-flex
: Creates an inline Flex container.
Example:
<div class="d-flex">
<div class="p-2 bg-primary text-white">Item 1</div>
<div class="p-2 bg-secondary text-white">Item 2</div>
<div class="p-2 bg-success text-white">Item 3</div>
</div>
2. Flex Direction
Control the direction of the Flex items with the flex-{direction}
class.
flex-row
: Align items horizontally (default).flex-row-reverse
: Reverse the row direction.flex-column
: Align items vertically.flex-column-reverse
: Reverse the column direction.
Example:
<div class="d-flex flex-column">
<div class="p-2 bg-primary text-white">Item 1</div>
<div class="p-2 bg-secondary text-white">Item 2</div>
<div class="p-2 bg-success text-white">Item 3</div>
</div>
3. Justify Content
Align items horizontally within the Flex container using justify-content-{alignment}
.
justify-content-start
(default)justify-content-end
justify-content-center
justify-content-between
justify-content-around
justify-content-evenly
Example:
<div class="d-flex justify-content-between">
<div class="p-2 bg-primary text-white">Item 1</div>
<div class="p-2 bg-secondary text-white">Item 2</div>
<div class="p-2 bg-success text-white">Item 3</div>
</div>
4. Align Items
Align items vertically within the Flex container using align-items-{alignment}
.
align-items-start
align-items-end
align-items-center
align-items-baseline
align-items-stretch
(default)
Example:
<div class="d-flex align-items-center" style="height: 150px;">
<div class="p-2 bg-primary text-white">Item 1</div>
<div class="p-2 bg-secondary text-white">Item 2</div>
</div>
5. Align Self
Override the alignment of individual Flex items using align-self-{alignment}
.
align-self-start
align-self-end
align-self-center
align-self-baseline
align-self-stretch
(default)
Example:
<div class="d-flex">
<div class="p-2 bg-primary text-white align-self-start">Item 1</div>
<div class="p-2 bg-secondary text-white align-self-center">Item 2</div>
<div class="p-2 bg-success text-white align-self-end">Item 3</div>
</div>
6. Flex Grow, Shrink, and Wrap
Flex Grow:
Control how much an item grows relative to the rest with flex-grow-{value}
.
flex-grow-0
: Prevent growth.flex-grow-1
: Allow growth.
<div class="d-flex">
<div class="p-2 bg-primary text-white flex-grow-1">Grow Item</div>
<div class="p-2 bg-secondary text-white">Static Item</div>
</div>
Flex Shrink:
Control how much an item shrinks relative to the rest with flex-shrink-{value}
.
flex-shrink-0
: Prevent shrinking.flex-shrink-1
: Allow shrinking.
Flex Wrap:
Enable wrapping of items with flex-wrap
or flex-nowrap
.
<div class="d-flex flex-wrap">
<div class="p-2 bg-primary text-white">Item 1</div>
<div class="p-2 bg-secondary text-white">Item 2</div>
<div class="p-2 bg-success text-white">Item 3</div>
</div>
7. Flex Order
Change the order of Flex items with order-{value}
.
order-0
(default)order-1
,order-2
, … (higher numbers appear later)order-last
: Places the item last.
Example:
<div class="d-flex">
<div class="p-2 bg-primary text-white order-3">Item 1</div>
<div class="p-2 bg-secondary text-white order-1">Item 2</div>
<div class="p-2 bg-success text-white order-2">Item 3</div>
</div>
8. Gap Utilities
Bootstrap 5 introduces the gap-{size}
utility for adding spacing between Flex items.
Example:
<div class="d-flex gap-3">
<div class="p-2 bg-primary text-white">Item 1</div>
<div class="p-2 bg-secondary text-white">Item 2</div>
<div class="p-2 bg-success text-white">Item 3</div>
</div>
Combining Flex Utilities
You can combine multiple Flex utilities for more complex layouts.
Example: Responsive Navigation Bar
<nav class="d-flex justify-content-between align-items-center bg-dark text-white p-3">
<div>Logo</div>
<ul class="d-flex list-unstyled mb-0">
<li class="px-3">Home</li>
<li class="px-3">About</li>
<li class="px-3">Contact</li>
</ul>
</nav>
Use Cases for Bootstrap Flex
- Horizontal and Vertical Centering: Use Flex utilities for centering elements without extra CSS.
- Custom Grids: Create grids with flexible item sizing and alignment.
- Responsive Menus: Design dynamic navigation bars.
- Cards and Galleries: Organize content in rows and columns easily.
FAQs About Bootstrap 5 Flex
1. How is Bootstrap 5 Flex different from CSS Flexbox?
Bootstrap 5 Flex simplifies Flexbox usage by providing utility classes, eliminating the need to write raw CSS.
2. Can I customize Flex utilities?
Yes, you can customize Flex utilities using Sass variables or the Bootstrap Utility API.
3. Are Flex utilities responsive?
Yes, you can make Flex utilities responsive by using breakpoint-specific classes, like d-md-flex
.
Conclusion
Bootstrap 5 Flex utilities empower developers to create responsive and efficient layouts without writing custom CSS. Whether you’re building navigation bars, cards, or complex grids, these utilities streamline the process and improve consistency.