🔥 0
0
Lesson 5 of 10 18 min +100 XP

Flexbox Layouts

Flexbox is the go-to for one-dimensional layouts. Tailwind makes it incredibly intuitive.

Enabling Flex

<!-- Create a flex container -->
<div class="flex">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>
See it live:
Item 1
Item 2
Item 3

Flex Direction

<!-- Row (default) - horizontal -->
<div class="flex flex-row">...</div>

<!-- Column - vertical -->
<div class="flex flex-col">...</div>
See it live:

flex-row

1
2
3

flex-col

1
2
3

Justify Content (Main Axis)

Control horizontal alignment in a row:

<div class="flex justify-start">Start (default)</div>
<div class="flex justify-center">Center</div>
<div class="flex justify-end">End</div>
<div class="flex justify-between">Space between</div>
See it live:

justify-start

1
2
3

justify-center

1
2
3

justify-between

1
2
3

Align Items (Cross Axis)

Control vertical alignment in a row:

<div class="flex items-start h-24">Top</div>
<div class="flex items-center h-24">Center</div>
<div class="flex items-end h-24">Bottom</div>
See it live:

items-start

A
B

items-center

A
B

items-end

A
B

Gap

Add space between flex items:

<div class="flex gap-4">
  <div>1</div><div>2</div><div>3</div>
</div>
See it live:

gap-2

1
2
3

gap-6

1
2
3

Flex Grow & Shrink

Control how items size:

<!-- flex-1: Grow to fill space equally -->
<div class="flex">
  <div class="flex-1">Grows</div>
  <div class="flex-1">Grows</div>
  <div class="w-32">Fixed</div>
</div>
See it live:

flex-1 items grow equally

flex-1
flex-1
w-24

One flex-1 takes remaining space

w-20
flex-1
w-20

Practical Examples

Navigation Bar

<nav class="flex items-center justify-between px-6 py-4 bg-white shadow">
  <div class="text-xl font-bold">Logo</div>
  <div class="flex items-center gap-6">
    <a href="#">Home</a>
    <a href="#">About</a>
  </div>
  <button class="bg-blue-600 text-white px-4 py-2 rounded-lg">
    Sign Up
  </button>
</nav>
See it live:

Card with Footer

<div class="flex flex-col h-64 bg-white rounded-lg shadow">
  <div class="flex-1 p-6">
    <h3 class="text-lg font-semibold">Card Title</h3>
    <p class="mt-2 text-gray-600">Content goes here...</p>
  </div>
  <div class="flex items-center justify-between px-6 py-4 bg-gray-50">
    <span class="text-sm text-gray-500">Updated 2 hours ago</span>
    <button class="text-blue-600 text-sm font-medium">View More</button>
  </div>
</div>
See it live:

Card Title

Card content goes here. The flex-1 class makes this section grow to fill available space.

Updated 2 hours ago

Centering Content

<!-- Center both horizontally and vertically -->
<div class="flex items-center justify-center h-64 bg-gray-100">
  <div class="bg-white p-8 rounded-xl shadow-lg text-center">
    <h1 class="text-2xl font-bold">Welcome Back</h1>
  </div>
</div>
See it live:

Welcome Back

Please sign in to continue

Key Takeaways

  • flex: Creates a flex container
  • justify-: Aligns on main axis (horizontal in row)
  • items-: Aligns on cross axis (vertical in row)
  • gap-*: Adds space between items
  • flex-1: Makes item grow to fill space
  • flex-col: Switches to vertical layout

Next, let's explore CSS Grid layouts!

🧠 Quick Quiz

Test your understanding of this lesson.

1

What does 'justify-between' do?

2

How do you vertically center items in a flex container?

3

What does 'flex-1' do to a flex item?

Colors & Typography