← Back to Bootstrap Course | Chapter 9: Layout Utilities | Lesson 1 of 7

Flexbox utilities

Flex utilities turn an element into a flexible row or column and control alignment and spacing.

In this page:

  1. Flexbox utilities
Syntax
markup
<div class="d-flex flex-row justify-content-between align-items-center">
  <div>item</div>
  <div>item</div>
</div>

Flexbox utilities

Add .d-flex to make a flex container. Direction is set with .flex-row or .flex-column, main-axis alignment with .justify-content-*, cross-axis alignment with .align-items-*, and wrapping with .flex-wrap.

Children can use .flex-fill, .flex-grow-1, .align-self-* and .ms-auto.

Note: justify-content controls the main axis, align-items the cross axis.

Example: Flexbox utilities

markup
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<div class="container">
  <div class="d-flex justify-content-between align-items-center bg-light border p-2 mb-2">
    <span>Left</span><span>Centre-ish</span><span>Right</span>
  </div>
  <div class="d-flex flex-column align-items-end bg-light border p-2" style="height:8rem"><span>Item 1</span><span class="mt-auto">Pushed to bottom</span></div>
</div>
</body>
</html>
Live Example
Related Topics
Common Mistakes
  1. Forgetting d-flex
  2. Mixing up justify and align
  3. Expecting flex utilities on children to work without a container
Chapter Summary
  • d-flex creates a flex container
  • flex-row and flex-column set direction
  • justify-content-* and align-items-* align
  • flex-wrap allows wrapping
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.