Flexbox Basics

Back

Loading concept...

Bootstrap Flexbox Basics: The Magic Box Organizer

The Story of the Magic Shelf

Imagine you have a magic bookshelf. This shelf can arrange your toys, books, and treasures in any way you want - in a row, stacked up, spread apart, or squished together. That’s exactly what Bootstrap Flexbox does for things on a webpage!


What is Flexbox?

Flexbox is like having a super-smart container that knows how to organize everything inside it perfectly. Instead of pushing things around yourself, you just tell the container what you want, and it does the work!

Real Life Example:

  • Your lunchbox has compartments that hold food neatly = Flexbox
  • A jewelry box with sections for rings, necklaces = Flexbox
  • A TV remote control holder that fits remotes perfectly = Flexbox

1. Flex Container: The Boss Box

The Flex Container is the parent box that controls everything inside it. Without it, nothing works!

How to Make a Box the Boss

<div class="d-flex">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>

Think of it like this: The d-flex class is like putting on a superhero cape. Once the parent wears it, it gains special powers to organize its children!

Inline Flex: The Shy Boss

Sometimes you want a flex container that only takes as much space as it needs:

<div class="d-inline-flex">
  <div>Small</div>
  <div>Team</div>
</div>
Class What It Does
d-flex Full-width flex container
d-inline-flex Width of content only

2. Flex Direction: Which Way Do We Go?

Once you have a flex container, you decide which direction items flow. It’s like choosing whether to line up your toys left-to-right or stack them top-to-bottom!

The Four Directions

graph TD A["flex-direction"] --> B["row - Left to Right"] A --> C["row-reverse - Right to Left"] A --> D["column - Top to Bottom"] A --> E["column-reverse - Bottom to Top"]

Examples

Row (Default): Items sit side by side

<div class="d-flex flex-row">
  <div>🍎</div>
  <div>🍊</div>
  <div>πŸ‹</div>
</div>
<!-- Result: 🍎 🍊 πŸ‹ -->

Row Reverse: Items sit side by side, but backwards!

<div class="d-flex flex-row-reverse">
  <div>🍎</div>
  <div>🍊</div>
  <div>πŸ‹</div>
</div>
<!-- Result: πŸ‹ 🍊 🍎 -->

Column: Items stack on top of each other

<div class="d-flex flex-column">
  <div>πŸŽ‚</div>
  <div>🍰</div>
  <div>🧁</div>
</div>
<!-- Result: πŸŽ‚
            🍰
            🧁 -->

Column Reverse: Items stack, but upside down!

<div class="d-flex flex-column-reverse">
  <div>πŸŽ‚</div>
  <div>🍰</div>
  <div>🧁</div>
</div>
<!-- Result: 🧁
            🍰
            πŸŽ‚ -->
Class Direction
flex-row Left β†’ Right
flex-row-reverse Right β†’ Left
flex-column Top β†’ Bottom
flex-column-reverse Bottom β†’ Top

3. Justify Content: Spreading Things Out

Justify Content controls how items spread out along the main direction (left-right for rows, up-down for columns).

Think of it Like This:

Imagine you have 3 goldfish in a long fish tank. How do you want them positioned?

graph TD A["justify-content"] --> B["start - All swim to the left"] A --> C["end - All swim to the right"] A --> D["center - Meet in the middle"] A --> E["between - Spread out evenly"] A --> F["around - Equal space around each"] A --> G["evenly - Perfect equal spacing"]

Examples

Start (Default): Everything hugs the beginning

<div class="d-flex justify-content-start">
  <div>🐟</div>
  <div>🐟</div>
  <div>🐟</div>
</div>
<!-- [🐟🐟🐟           ] -->

End: Everything hugs the ending

<div class="d-flex justify-content-end">
  <div>🐟</div>
  <div>🐟</div>
  <div>🐟</div>
</div>
<!-- [           🐟🐟🐟] -->

Center: Meet in the middle

<div class="d-flex justify-content-center">
  <div>🐟</div>
  <div>🐟</div>
  <div>🐟</div>
</div>
<!-- [     🐟🐟🐟     ] -->

Between: First and last touch edges, space in between

<div class="d-flex justify-content-between">
  <div>🐟</div>
  <div>🐟</div>
  <div>🐟</div>
</div>
<!-- [🐟     🐟     🐟] -->

Around: Equal space around each item

<div class="d-flex justify-content-around">
  <div>🐟</div>
  <div>🐟</div>
  <div>🐟</div>
</div>
<!-- [  🐟    🐟    🐟  ] -->

Evenly: Perfect equal spacing everywhere

<div class="d-flex justify-content-evenly">
  <div>🐟</div>
  <div>🐟</div>
  <div>🐟</div>
</div>
<!-- [   🐟   🐟   🐟   ] -->
Class Result
justify-content-start Pack at beginning
justify-content-end Pack at end
justify-content-center Center all items
justify-content-between Space between items
justify-content-around Space around items
justify-content-evenly Equal space everywhere

4. Align Items: The Vertical Organizer

While justify-content handles the main direction, align-items handles the cross direction (up-down for rows, left-right for columns).

Picture This:

You have three photos of different heights on a clothesline. How should they hang?

graph TD A["align-items"] --> B["start - Hang from top"] A --> C["end - Touch bottom"] A --> D["center - Middle of line"] A --> E["baseline - Text lines up"] A --> F["stretch - Fill entire height"]

Examples

Start: Items touch the top

<div class="d-flex align-items-start"
     style="height: 100px;">
  <div>Short</div>
  <div>Tall<br>Box</div>
</div>

End: Items touch the bottom

<div class="d-flex align-items-end"
     style="height: 100px;">
  <div>Short</div>
  <div>Tall<br>Box</div>
</div>

Center: Items float in the middle

<div class="d-flex align-items-center"
     style="height: 100px;">
  <div>Short</div>
  <div>Tall<br>Box</div>
</div>

Baseline: Text lines up perfectly

<div class="d-flex align-items-baseline">
  <h1>Big</h1>
  <p>Small</p>
</div>

Stretch (Default): Items stretch to fill height

<div class="d-flex align-items-stretch"
     style="height: 100px;">
  <div>I stretch!</div>
  <div>Me too!</div>
</div>
Class Result
align-items-start Top aligned
align-items-end Bottom aligned
align-items-center Vertically centered
align-items-baseline Text baselines match
align-items-stretch Fill container height

5. Align Self: The Rebel Item

What if ONE item wants to be different from the group? That’s where align-self comes in!

The Story:

Imagine a choir where everyone stands in a line. Most singers stand normally, but one singer wants to stand on their tiptoes to be taller. Align-self lets individual items break the rules!

Example

<div class="d-flex align-items-start"
     style="height: 100px;">
  <div>Normal</div>
  <div class="align-self-center">I'm centered!</div>
  <div class="align-self-end">I'm at bottom!</div>
</div>
Class Result
align-self-start This item goes to top
align-self-end This item goes to bottom
align-self-center This item centers itself
align-self-baseline This item aligns by text
align-self-stretch This item stretches

Key Difference:

  • align-items = Boss tells ALL items where to go
  • align-self = ONE item says β€œI want something different!”

6. Flex Fill: Share Space Equally

Flex Fill makes items grow to fill ALL available space and share it equally.

Think About It Like This:

You have a pizza and 3 friends. flex-fill cuts the pizza so everyone gets an equal slice, no matter who came first!

Example

<div class="d-flex">
  <div class="flex-fill">Third</div>
  <div class="flex-fill">Third</div>
  <div class="flex-fill">Third</div>
</div>

Each box takes exactly one-third of the space!

Mix and Match

<div class="d-flex">
  <div class="flex-fill">I grow!</div>
  <div>I stay small</div>
  <div class="flex-fill">I grow too!</div>
</div>

The two flex-fill items share the leftover space equally.

Class Result
flex-fill Grow to fill space equally
flex-grow-0 Don’t grow at all
flex-grow-1 Grow if there’s space
flex-shrink-0 Don’t shrink
flex-shrink-1 Shrink if needed

Quick Reference Card

graph TD A["Flexbox"] --> B["Container: d-flex"] B --> C["Direction: flex-row/column"] B --> D["Justify: justify-content-*"] B --> E["Align: align-items-*"] B --> F["Individual: align-self-*"] B --> G["Fill: flex-fill"]

Real World Example: Navigation Bar

Let’s build a navigation bar using everything we learned!

<nav class="d-flex
            justify-content-between
            align-items-center">
  <div>🏠 Logo</div>
  <div class="d-flex">
    <a class="flex-fill">Home</a>
    <a class="flex-fill">About</a>
    <a class="flex-fill">Contact</a>
  </div>
  <button>Login</button>
</nav>

What’s happening:

  1. d-flex - Makes nav a flex container
  2. justify-content-between - Logo left, button right
  3. align-items-center - Everything vertically centered
  4. Inner d-flex + flex-fill - Links share space equally

You Did It! πŸŽ‰

You now understand the 6 superpowers of Bootstrap Flexbox:

  1. Flex Container - The boss that controls everything
  2. Flex Direction - Which way items flow
  3. Justify Content - How items spread out
  4. Align Items - How items align on cross-axis
  5. Align Self - How ONE item can be different
  6. Flex Fill - How items share space equally

Go build something amazing! πŸš€

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.