0
0
BootstrapHow-ToBeginner · 3 min read

How to Use d-flex in Bootstrap for Flexible Layouts

In Bootstrap, use the d-flex class to apply CSS flexbox display to an element, making it a flexible container. This enables easy alignment and distribution of child elements horizontally or vertically. Combine d-flex with other flex utility classes for layout control.
📐

Syntax

The d-flex class sets an element's display property to flex. This makes the element a flex container, allowing its children to be arranged using flexbox rules.

You can combine d-flex with other Bootstrap flexbox utility classes like justify-content-center or align-items-center to control alignment and spacing.

html
<div class="d-flex"> ... </div>
Output
A container with flex display applied, arranging its children in a row by default.
💻

Example

This example shows a flex container with three boxes arranged in a row, centered horizontally and vertically.

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>d-flex Example</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>
    .box {
      width: 100px;
      height: 100px;
      background-color: #0d6efd;
      color: white;
      display: flex;
      justify-content: center;
      align-items: center;
      margin: 5px;
      border-radius: 8px;
      font-weight: bold;
    }
  </style>
</head>
<body>
  <div class="d-flex justify-content-center align-items-center" style="height: 150px;">
    <div class="box">Box 1</div>
    <div class="box">Box 2</div>
    <div class="box">Box 3</div>
  </div>
</body>
</html>
Output
A horizontal row of three blue boxes labeled 'Box 1', 'Box 2', and 'Box 3', centered vertically and horizontally in the page area.
⚠️

Common Pitfalls

One common mistake is forgetting that d-flex sets display: flex which changes the layout flow from block to flex. This can affect width and height if not handled properly.

Another pitfall is not combining d-flex with alignment classes, which can cause child elements to stack awkwardly or not align as expected.

html
<!-- Wrong: No alignment classes, children stack at start -->
<div class="d-flex" style="height: 100px; background: #eee;">
  <div>Item 1</div>
  <div>Item 2</div>
</div>

<!-- Right: Add alignment classes for centering -->
<div class="d-flex justify-content-center align-items-center" style="height: 100px; background: #eee;">
  <div>Item 1</div>
  <div>Item 2</div>
</div>
Output
First container: items aligned to the top-left. Second container: items centered horizontally and vertically.
📊

Quick Reference

ClassEffect
d-flexSets display to flex, making the element a flex container
justify-content-startAligns children to the left (default)
justify-content-centerCenters children horizontally
justify-content-endAligns children to the right
align-items-startAligns children to the top
align-items-centerCenters children vertically
align-items-endAligns children to the bottom
flex-columnStacks children vertically instead of horizontally

Key Takeaways

Use d-flex to make any element a flex container for flexible layouts.
Combine d-flex with alignment classes like justify-content-center and align-items-center for precise control.
Remember d-flex changes layout flow, so check child element sizing and alignment.
Use flex-column to stack items vertically instead of the default horizontal row.
Bootstrap flex utilities make responsive and clean layouts easy without custom CSS.