404 Not Found

404 Not Found


nginx

Flexbox Utility Classes

1. Introduction to This Lesson

(1) Prerequisites

(2) 🎯 What You Will Learn


(3) Pain Point

Bob wants to create a navigation bar: logo on the left, navigation links centered, and a login button on the right—using traditional layouts requires float or position, which is cumbersome and error-prone to implement.

(4) Solution

Bootstrap encapsulates common Flexbox properties into utility classes, allowing you to implement flexible layouts without writing a single line of CSS. Using Bootstrap's Flexbox utility classes only requires d-flex justify-content-between align-items-center.

Understanding: Flexbox is like workers arranging boxes—the main axis determines the direction (horizontal or vertical), the cross axis determines alignment (center/top/bottom), justify-content manages horizontal distribution, and align-items manages vertical alignment.

(5) Benefit

After using Flexbox utility classes, Bob completed the navigation bar layout with just a few concise classes—logo left, links centered, button right—the code is clear and easy to maintain.


2. Enabling the Flex Container

All Flex utility classes are based on d-flex or d-inline-flex:

▶ Example: Flex Container Basics

HTML
<div class="d-flex bg-light p-2">d-flex - block level flex container</div>
<span class="d-inline-flex bg-info p-2 text-white">d-inline-flex - inline flex</span>
▶ Try it Yourself

Output: Component effects with Bootstrap 5.3 styles applied (such as buttons, cards, carousels, collapses, etc.). The page uses the default Bootstrap theme (light) and responsive grid by default.

Class Effect CSS Equivalent
d-flex Block-level flex container display: flex
d-inline-flex Inline flex container display: inline-flex

These two classes also support responsive breakpoints: d-sm-flex, d-md-flex, etc.



3. Main Axis Direction

flex-row (default) goes from left to right, flex-row-reverse goes from right to left, flex-column goes from top to bottom.

HTML
<div class="d-flex flex-row bg-light p-2 mb-2">
  <div class="p-2 bg-primary text-white">Item 1</div>
  <div class="p-2 bg-success text-white">Item 2</div>
  <div class="p-2 bg-danger text-white">Item 3</div>
</div>

<div class="d-flex flex-column bg-light p-2">
  <div class="p-2 bg-primary text-white">Item 1</div>
  <div class="p-2 bg-success text-white">Item 2</div>
  <div class="p-2 bg-danger text-white">Item 3</div>
</div>
▶ Try it Yourself

Output: Component effects with Bootstrap 5.3 styles applied (such as buttons, cards, carousels, collapses, etc.). The page uses the default Bootstrap theme (light) and responsive grid by default.

Class Direction Scenario
flex-row Horizontal (default) Navigation links, button groups
flex-row-reverse Reverse horizontal Right-side menus
flex-column Vertical Mobile vertical menus
flex-column-reverse Reverse vertical Comment sections (newest on top)


4. Main Axis Alignment: justify-content

HTML
<div class="d-flex justify-content-start bg-light p-2 mb-2">
  <div class="p-2 bg-primary text-white">Start</div>
  <div class="p-2 bg-primary text-white">Start</div>
</div>
<div class="d-flex justify-content-center bg-light p-2 mb-2">
  <div class="p-2 bg-success text-white">Center</div>
  <div class="p-2 bg-success text-white">Center</div>
</div>
<div class="d-flex justify-content-end bg-light p-2 mb-2">
  <div class="p-2 bg-danger text-white">End</div>
  <div class="p-2 bg-danger text-white">End</div>
</div>
<div class="d-flex justify-content-between bg-light p-2 mb-2">
  <div class="p-2 bg-info text-white">Between</div>
  <div class="p-2 bg-info text-white">Between</div>
</div>
<div class="d-flex justify-content-around bg-light p-2">
  <div class="p-2 bg-warning">Around</div>
  <div class="p-2 bg-warning">Around</div>
</div>
▶ Try it Yourself

Output: Component effects with Bootstrap 5.3 styles applied (such as buttons, cards, carousels, collapses, etc.). The page uses the default Bootstrap theme (light) and responsive grid by default.

100%
graph LR
    subgraph justify-content options
        A1[start] --> A2[center]
        A2 --> A3[end]
        A3 --> A4[between]
        A4 --> A5[around]
    end

Output: Component effects with Bootstrap 5.3 styles applied (such as buttons, cards, carousels, collapses, etc.). The page uses the default Bootstrap theme (light) and responsive grid by default.

Class Effect
justify-content-start Left-aligned (default)
justify-content-center Centered
justify-content-end Right-aligned
justify-content-between Justified, equal space between items
justify-content-around Equal space around items
justify-content-evenly All spacing completely equal


5. Cross Axis Alignment: align-items

HTML
<div class="d-flex align-items-start bg-light p-2 mb-2" style="height: 80px;">
  <div class="p-2 bg-primary text-white">Start</div>
  <div class="p-2 bg-primary text-white">Start</div>
</div>
<div class="d-flex align-items-center bg-light p-2 mb-2" style="height: 80px;">
  <div class="p-2 bg-success text-white">Center</div>
  <div class="p-2 bg-success text-white">Center</div>
</div>
<div class="d-flex align-items-end bg-light p-2" style="height: 80px;">
  <div class="p-2 bg-danger text-white">End</div>
  <div class="p-2 bg-danger text-white">End</div>
</div>
▶ Try it Yourself

Output: Component effects with Bootstrap 5.3 styles applied (such as buttons, cards, carousels, collapses, etc.). The page uses the default Bootstrap theme (light) and responsive grid by default.

Class Effect CSS Equivalent
align-items-start Top-aligned align-items: flex-start
align-items-center Vertically centered align-items: center
align-items-end Bottom-aligned align-items: flex-end
align-items-stretch Stretch to fill (default) align-items: stretch

Typical Scenario: align-items-center is the most common solution for vertical centering—set d-flex + align-items-center on the parent container, and the content will be vertically centered automatically.



6. Single Item Alignment: align-self

HTML
<div class="d-flex bg-light p-2" style="height: 100px;">
  <div class="p-2 bg-primary text-white align-self-start">Start</div>
  <div class="p-2 bg-success text-white align-self-center">Center</div>
  <div class="p-2 bg-danger text-white align-self-end">End</div>
</div>
▶ Try it Yourself

Output: Component effects with Bootstrap 5.3 styles applied (such as buttons, cards, carousels, collapses, etc.). The page uses the default Bootstrap theme (light) and responsive grid by default.



7. Wrapping: flex-wrap

HTML
<div class="d-flex flex-wrap bg-light p-2">
  <div class="p-3 m-1 bg-primary text-white">Item 1</div>
  <div class="p-3 m-1 bg-primary text-white">Item 2</div>
  <div class="p-3 m-1 bg-primary text-white">Item 3</div>
  <div class="p-3 m-1 bg-primary text-white">Item 4</div>
  <div class="p-3 m-1 bg-primary text-white">Item 5</div>
  <div class="p-3 m-1 bg-primary text-white">Item 6</div>
</div>
▶ Try it Yourself

Output: Component effects with Bootstrap 5.3 styles applied (such as buttons, cards, carousels, collapses, etc.). The page uses the default Bootstrap theme (light) and responsive grid by default.

Class Behavior
flex-nowrap No wrapping, items are compressed (default)
flex-wrap Automatic wrapping
flex-wrap-reverse Reverse wrapping


8. Ordering: order

HTML
<div class="d-flex bg-light p-2">
  <div class="p-3 bg-danger text-white order-3">First in HTML (order-3)</div>
  <div class="p-3 bg-success text-white order-1">Second in HTML (order-1)</div>
  <div class="p-3 bg-primary text-white order-2">Third in HTML (order-2)</div>
</div>
▶ Try it Yourself

Output: Component effects with Bootstrap 5.3 styles applied (such as buttons, cards, carousels, collapses, etc.). The page uses the default Bootstrap theme (light) and responsive grid by default. order-0 ~ order-5, smaller numbers come first. All items default to order-0.



9. Flexing: flex-grow and flex-fill

HTML
<div class="d-flex bg-light p-2">
  <div class="p-3 bg-danger text-white">Fixed</div>
  <div class="p-3 bg-success text-white flex-fill">flex-fill</div>
  <div class="p-3 bg-primary text-white flex-fill">flex-fill</div>
</div>
▶ Try it Yourself

Output: Component effects with Bootstrap 5.3 styles applied (such as buttons, cards, carousels, collapses, etc.). The page uses the default Bootstrap theme (light) and responsive grid by default.

Class Effect CSS
flex-fill Equal share of remaining space flex: 1 1 auto
flex-grow-0 Don't grow (default) flex-grow: 0
flex-grow-1 Grow to fill flex-grow: 1
flex-shrink-0 Don't shrink flex-shrink: 0


10. Flex Utility Classes Cheat Sheet

(1) Flex Properties Quick Reference

Category Class CSS Equivalent Description
Container d-flex display: flex Block-level flex container
Container d-inline-flex display: inline-flex Inline flex container
Direction flex-row flex-direction: row Horizontal layout (default)
Direction flex-column flex-direction: column Vertical layout
Direction flex-row-reverse flex-direction: row-reverse Reverse horizontal
Direction flex-column-reverse flex-direction: column-reverse Reverse vertical
Main Axis justify-content-* justify-content: * Main axis alignment
Cross Axis align-items-* align-items: * Cross axis alignment
Single Item align-self-* align-self: * Single item alignment
Wrapping flex-wrap flex-wrap: wrap Automatic wrapping
Wrapping flex-nowrap flex-wrap: nowrap No wrapping (default)
Ordering order-{0-5} order: {0-5} Adjust display order
Flexing flex-fill flex: 1 1 auto Equal share of remaining space
Flexing flex-grow-1 flex-grow: 1 Grow to fill
Flexing flex-shrink-0 flex-shrink: 0 Prohibit shrinking

(2) justify-content Values Explained

Class Effect Diagram Use Case
justify-content-start Left-aligned (default) ████░░░░ Standard left alignment
justify-content-center Centered ░░████░░ Centered layout
justify-content-end Right-aligned ░░░░████ Right-side action bar
justify-content-between Justified █░░█░░██ Navigation bar
justify-content-around Equal space around ░█░█░█░█░ Icon toolbar
justify-content-evenly All spacing equal ░█░░█░░█░ Evenly distributed menu

(3) align-items Values Explained

Class Effect Diagram Use Case
align-items-start Top-aligned Top aligned Top baseline alignment
align-items-center Vertically centered Centered Most common for vertical centering
align-items-end Bottom-aligned Bottom aligned Bottom action bar
align-items-stretch Stretch to fill Equal height Equal height card columns
align-items-baseline Text baseline aligned Baseline Text with different font sizes

▶ Example: Navigation Bar

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Flex Navbar</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <nav class="d-flex justify-content-between align-items-center bg-dark text-white p-3">
    <div class="fw-bold fs-5">MyLogo</div>
    <ul class="d-flex list-unstyled gap-4 mb-0">
      <li><a href="#" class="text-white text-decoration-none">Home</a></li>
      <li><a href="#" class="text-white text-decoration-none">Products</a></li>
      <li><a href="#" class="text-white text-decoration-none">About</a></li>
      <li><a href="#" class="text-white text-decoration-none">Contact</a></li>
    </ul>
    <button class="btn btn-light btn-sm">Login</button>
  </nav>
  <div class="container mt-4">
    <p>Use <code>justify-content-between</code> to achieve logo on the left, navigation centered, and button on the right.</p>
  </div>
</body>
</html>
▶ Try it Yourself

Output: Component effects with Bootstrap 5.3 styles applied (such as buttons, cards, carousels, collapses, etc.). The page uses the default Bootstrap theme (light) and responsive grid by default.

▶ Example: Card Grid

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Card Grid</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <div class="container py-4">
    <h3 class="mb-4">Flex Card Grid</h3>
    <div class="d-flex flex-column flex-md-row gap-3">
      <div class="card flex-fill">
        <div class="card-body">
          <h5 class="card-title">Card One</h5>
          <p class="card-text">flex-fill makes all cards equal width.</p>
          <a href="#" class="btn btn-primary">View</a>
        </div>
      </div>
      <div class="card flex-fill">
        <div class="card-body">
          <h5 class="card-title">Card Two</h5>
          <p class="card-text">Use gap-3 to control card spacing.</p>
          <a href="#" class="btn btn-success">View</a>
        </div>
      </div>
      <div class="card flex-fill">
        <div class="card-body">
          <h5 class="card-title">Card Three</h5>
          <p class="card-text">flex-column for mobile vertical, flex-md-row for desktop horizontal.</p>
          <a href="#" class="btn btn-outline-primary">View</a>
        </div>
      </div>
    </div>
  </div>
</body>
</html>
▶ Try it Yourself

Output: Component effects with Bootstrap 5.3 styles applied (such as buttons, cards, carousels, collapses, etc.). The page uses the default Bootstrap theme (light) and responsive grid by default.

▶ Example: Centered Login Form

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Centered Login</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <div class="d-flex justify-content-center align-items-center bg-light" style="min-height: 100vh;">
    <div class="bg-white p-4 rounded shadow" style="width: 100%; max-width: 400px;">
      <h3 class="text-center mb-4">User Login</h3>
      <form>
        <div class="mb-3">
          <label class="form-label">Email</label>
          <input type="email" class="form-control" placeholder="name@example.com">
        </div>
        <div class="mb-3">
          <label class="form-label">Password</label>
          <input type="password" class="form-control" placeholder="Enter your password">
        </div>
        <button type="submit" class="btn btn-primary w-100">Login</button>
      </form>
      <p class="text-center text-muted small mt-3 mb-0">
        Don't have an account? <a href="#">Sign up now</a>
      </p>
    </div>
  </div>
</body>
</html>
▶ Try it Yourself

Output: Component effects with Bootstrap 5.3 styles applied (such as buttons, cards, carousels, collapses, etc.). The page uses the default Bootstrap theme (light) and responsive grid by default.

▶ Example: Media Object

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Media Object</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <div class="container py-4">
    <h3 class="mb-4">Flex Media Object</h3>

    <div class="d-flex gap-3 mb-4">
      <div class="bg-primary text-white d-flex justify-content-center align-items-center" style="width: 64px; height: 64px; border-radius: 50%; flex-shrink: 0;">
        A
      </div>
      <div>
        <h5 class="mb-1">Alice Wang</h5>
        <p class="mb-0 text-muted small">flex-shrink-0 ensures the avatar is not compressed. Use d-flex + gap-3 for text-image layout, which is the recommended implementation for Bootstrap media objects.</p>
      </div>
    </div>

    <div class="d-flex gap-3">
      <div class="bg-success text-white d-flex justify-content-center align-items-center" style="width: 64px; height: 64px; border-radius: 50%; flex-shrink: 0;">
        B
      </div>
      <div>
        <h5 class="mb-1">Bob Chen</h5>
        <p class="mb-0 text-muted small">align-self-center can vertically center the avatar relative to the text, useful when avatar heights vary.</p>
      </div>
    </div>
  </div>
</body>
</html>
▶ Try it Yourself

Output: Component effects with Bootstrap 5.3 styles applied (such as buttons, cards, carousels, collapses, etc.). The page uses the default Bootstrap theme (light) and responsive grid by default.


❓ Frequently Asked Questions

Q Why are my items not in a row when using d-flex?
A Flexbox defaults to flex-direction: row (horizontal layout). Check if other CSS is overriding the display value, or if the child elements are block-level elements with width: 100%.
Q What's the difference between justify-content-between and justify-content-around?
A between places the first and last items against the container edges, with equal space distributed between them. around allocates equal space on the left and right of each item, causing the end gaps to be half the middle gaps. You can modify classes in the browser DevTools to compare in real-time.
Q Must I use Flexbox for vertical centering?
A For containers with a known height, you can also use line-height or position + transform. However, Flexbox's d-flex align-items-center is the most concise and universal method—it doesn't depend on container height and has the best responsive breakpoint compatibility.
Q When should I use Flexbox, and when should I use Grid?
A Flexbox is suitable for one-dimensional layouts (row or column), such as navigation bars, button groups, and media objects. Grid is suitable for two-dimensional layouts (rows and columns simultaneously), such as page main layouts and card grids. Simple rule: use Flexbox for a single row or column, use Grid when controlling rows and columns simultaneously. Both can be nested.
Q What's the difference between align-content and align-items?
A align-items controls the alignment of items within a single line, while align-content controls the distribution of multiple lines along the cross axis. align-content only takes effect when flex-wrap: wrap is set and there are multiple lines. Bootstrap provides the align-content-* series of utility classes (e.g., align-content-center, align-content-between).

📖 Summary


📝 Assignment

  1. ⭐ Create a navigation bar: logo on the left, 4 links in the middle, login button on the right, using justify-content-between and align-items-center.
  2. ⭐⭐ Create a three-column card grid: vertical on mobile (flex-column), horizontal on tablets and above (flex-md-row), with gap-3 between cards.
  3. ⭐⭐⭐ Create a Flexbox interactive animation page: demonstrate the effects of all 6 justify-content values, each with text explanation and 3 colored blocks.

Previous Lesson: Spacing · Next Lesson: Responsive Design

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏