Lesson 42 of 60 – Bootstrap Flexbox Utilities
70%

Bootstrap Flexbox Utilities

Bootstrap provides a powerful collection of Flexbox utility classes for controlling the layout, alignment, direction, ordering, and spacing of elements. Flexbox makes it easier to create responsive layouts without writing custom CSS.

Note: Bootstrap Flexbox utilities are mainly based on CSS Flexbox. The d-flex class enables Flexbox, while other utilities control direction, alignment, justification, wrapping, ordering, and flex growth.

1. What Is Flexbox?

Flexbox is a CSS layout system designed to arrange elements in a flexible row or column.

Flexbox is useful for:

  • Horizontal layouts.
  • Vertical layouts.
  • Navigation bars.
  • Button groups.
  • Card layouts.
  • Responsive components.
  • Centering content.

2. The d-flex Class

The d-flex class changes an element into a Flexbox container.

<div class="d-flex">

    <div>Item One</div>

    <div>Item Two</div>

    <div>Item Three</div>

</div>

The child elements become flex items.

3. The d-inline-flex Class

The d-inline-flex class creates an inline-level Flexbox container.

<div class="d-inline-flex">

    <div>One</div>

    <div>Two</div>

</div>

Unlike d-flex, the container behaves as an inline-level element.

4. Flex Direction

Flex direction determines whether flex items are arranged in a row or column.

Bootstrap provides:

  • flex-row
  • flex-row-reverse
  • flex-column
  • flex-column-reverse
<div class="d-flex flex-column">

    <div>First</div>
    <div>Second</div>
    <div>Third</div>

</div>

5. flex-row

The flex-row class arranges flex items horizontally in the normal row direction.

<div class="d-flex flex-row">

    <div class="p-2 bg-primary text-white">
        One
    </div>

    <div class="p-2 bg-success text-white">
        Two
    </div>

    <div class="p-2 bg-danger text-white">
        Three
    </div>

</div>

6. flex-row-reverse

The flex-row-reverse class arranges items in the reverse row direction.

<div class="d-flex flex-row-reverse">

    <div>One</div>

    <div>Two</div>

    <div>Three</div>

</div>

The visual order is reversed.

7. flex-column

The flex-column class arranges flex items vertically.

<div class="d-flex flex-column">

    <div>First</div>

    <div>Second</div>

    <div>Third</div>

</div>

This is useful for vertical menus and stacked components.

8. flex-column-reverse

The flex-column-reverse class arranges items vertically in reverse order.

<div
class="d-flex flex-column-reverse">

    <div>One</div>

    <div>Two</div>

    <div>Three</div>

</div>

9. justify-content-start

The justify-content-start class aligns flex items at the start of the main axis.

<div
class="d-flex justify-content-start">

    <div>One</div>

    <div>Two</div>

</div>

10. justify-content-center

The justify-content-center class centers flex items along the main axis.

<div
class="d-flex justify-content-center">

    <button class="btn btn-primary">
        Center
    </button>

</div>

This is commonly used to horizontally center items in a row.

11. justify-content-end

The justify-content-end class aligns flex items at the end of the main axis.

<div
class="d-flex justify-content-end">

    <button class="btn btn-primary">
        Save
    </button>

</div>

12. justify-content-between

The justify-content-between class places the first item at the start and the last item at the end, distributing remaining space between items.

<div
class="d-flex justify-content-between">

    <span>
        Logo
    </span>

    <span>
        Menu
    </span>

</div>

This is very useful for headers and navigation layouts.

13. justify-content-around

The justify-content-around class distributes space around flex items.

<div
class="d-flex justify-content-around">

    <div>One</div>

    <div>Two</div>

    <div>Three</div>

</div>

14. justify-content-evenly

The justify-content-evenly class distributes equal space between flex items and at the outer edges.

<div
class="d-flex justify-content-evenly">

    <div>One</div>

    <div>Two</div>

    <div>Three</div>

</div>

15. align-items-start

The align-items-start class aligns flex items at the start of the cross axis.

<div
class="d-flex align-items-start"
style="height:150px;">

    <div>
        Item
    </div>

</div>

16. align-items-center

The align-items-center class centers flex items along the cross axis.

<div
class="d-flex
align-items-center"
style="height:150px;">

    <div>

        Centered Item

    </div>

</div>

This is especially useful for vertical centering in a horizontal Flexbox layout.

17. align-items-end

The align-items-end class aligns flex items at the end of the cross axis.

<div
class="d-flex
align-items-end"
style="height:150px;">

    <div>
        Bottom Item
    </div>

</div>

18. align-self Utilities

The align-self-* utilities control the alignment of an individual flex item.

Common classes include:

  • align-self-start
  • align-self-center
  • align-self-end
  • align-self-baseline
  • align-self-stretch
<div class="d-flex">

    <div class="align-self-start">
        Start
    </div>

    <div class="align-self-center">
        Center
    </div>

    <div class="align-self-end">
        End
    </div>

</div>

19. flex-wrap

The flex-wrap class allows flex items to move onto multiple lines when there is not enough space.

<div
class="d-flex flex-wrap">

    <div class="p-3">One</div>
    <div class="p-3">Two</div>
    <div class="p-3">Three</div>
    <div class="p-3">Four</div>

</div>

This is useful for responsive layouts.

20. flex-nowrap

The flex-nowrap class keeps flex items on one line.

<div
class="d-flex flex-nowrap">

    <div>One</div>

    <div>Two</div>

    <div>Three</div>

</div>

When the content is wider than the available space, the items may overflow their container depending on the surrounding layout.

21. flex-fill

The flex-fill utility makes a flex item grow and fill available space relative to other flex items.

<div class="d-flex">

    <div class="flex-fill p-2">
        One
    </div>

    <div class="flex-fill p-2">
        Two
    </div>

</div>

22. flex-grow-1

The flex-grow-1 utility allows an item to grow and use available free space.

<div class="d-flex">

    <div>
        Fixed
    </div>

    <div class="flex-grow-1">
        Growing Item
    </div>

</div>

The growing item takes up the available remaining space.

23. flex-shrink-0

The flex-shrink-0 utility prevents a flex item from shrinking when space becomes limited.

<div class="d-flex">

    <div class="flex-shrink-0">

        Fixed Width Content

    </div>

    <div>

        Other Content

    </div>

</div>

24. Order Utilities

Bootstrap provides order utilities for changing the visual order of flex items.

<div class="d-flex">

    <div class="order-3">
        First HTML Element
    </div>

    <div class="order-1">
        Second HTML Element
    </div>

    <div class="order-2">
        Third HTML Element
    </div>

</div>

The visual order follows the order values rather than the original HTML order.

25. Responsive Flexbox Utilities

Flexbox utilities can be combined with responsive breakpoints.

<div
class="d-flex
flex-column
flex-md-row">

    <div>
        Left
    </div>

    <div>
        Right
    </div>

</div>

In this example, items are stacked vertically by default and arranged in a row from the medium breakpoint and above.

26. Gap with Flexbox

Bootstrap gap utilities can be combined with Flexbox to create consistent spacing between items.

<div
class="d-flex gap-3">

    <button class="btn btn-primary">
        Home
    </button>

    <button class="btn btn-success">
        Courses
    </button>

    <button class="btn btn-danger">
        Contact
    </button>

</div>

This avoids adding individual margin classes to every item.

27. Centering Content with Flexbox

Flexbox is one of the easiest ways to center content horizontally and vertically.

<div
class="d-flex
justify-content-center
align-items-center"
style="height:200px;">

    <h3>
        Centered Content
    </h3>

</div>

justify-content-center centers the item on the main axis, while align-items-center centers it on the cross axis.

28. Common Flexbox Mistakes

Avoid these common mistakes:

  • Forgetting to add d-flex.
  • Confusing the main axis with the cross axis.
  • Using justify-content when you actually need align-items.
  • Forgetting flex-wrap in responsive layouts.
  • Using too many order utilities unnecessarily.
  • Expecting flex items to wrap when flex-nowrap is used.
  • Using fixed widths without considering smaller screens.
  • Ignoring gaps and spacing between items.

29. Practical Flexbox Navigation

The following example creates a simple responsive navigation layout using Bootstrap Flexbox utilities.

<div
class="d-flex
flex-column
flex-md-row
justify-content-between
align-items-center
gap-3
p-3
bg-light
rounded">

    <div>

        <h4 class="mb-0">
            Soopro Pathshala
        </h4>

    </div>


    <div
    class="d-flex
    flex-column
    flex-md-row
    gap-2">

        <a href="#"
        class="btn btn-primary">
            Home
        </a>

        <a href="#"
        class="btn btn-success">
            Courses
        </a>

        <a href="#"
        class="btn btn-info">
            Contact
        </a>

    </div>

</div>

30. Complete Bootstrap Flexbox Example

This complete example combines Flexbox direction, alignment, justification, gap, responsive utilities, and buttons.

<section class="py-5">

    <div class="container">

        <div
        class="d-flex
        flex-column
        flex-lg-row
        justify-content-between
        align-items-center
        gap-4
        p-4
        p-md-5
        bg-light
        rounded">


            <div>

                <h2 class="mb-3">

                    Learn Bootstrap

                </h2>


                <p class="mb-0">

                    Build responsive websites
                    using Bootstrap Flexbox
                    utilities.

                </p>

            </div>


            <div
            class="d-flex
            flex-column
            flex-sm-row
            gap-2">


                <button
                class="btn btn-primary">

                    Start Learning

                </button>


                <button
                class="btn btn-success">

                    View Course

                </button>


            </div>


        </div>

    </div>

</section>

Learn Bootstrap

Build responsive websites using Bootstrap Flexbox utilities.

📌 Key Points

  • d-flex creates a Flexbox container.
  • d-inline-flex creates an inline Flexbox container.
  • flex-row arranges items in a row.
  • flex-row-reverse reverses the row direction.
  • flex-column arranges items vertically.
  • flex-column-reverse reverses the column direction.
  • justify-content-* controls alignment on the main axis.
  • align-items-* controls alignment on the cross axis.
  • align-self-* controls an individual item's alignment.
  • flex-wrap allows items to wrap onto multiple lines.
  • flex-nowrap keeps items on one line.
  • flex-fill makes an item fill available space.
  • flex-grow-1 allows an item to grow.
  • flex-shrink-0 prevents an item from shrinking.
  • order-* changes the visual order of flex items.
  • Flexbox utilities can be combined with responsive breakpoints.
  • gap-* provides spacing between flex items.
  • Flexbox is useful for responsive navigation and component layouts.
  • Flexbox can easily center content horizontally and vertically.

🧠 Quick Quiz

Question: Which Bootstrap class enables Flexbox on an element?