Day 14 - CSS Flexbox

CSS Foundations: Flexbox

Day 14 ðŸ“Ķ

CSCI 3403 - Web Programming

Master modern layouts with Flexbox!

⚠ïļ Reminder: Landing Page Project due next Thursday night!

Today's Agenda 📋

Learning Objectives

By the end of this lecture, you will:

Part 1: Quick Review 🔄

The Flex Shorthand

Remember: flex is actually THREE properties!

/* This shorthand... */
flex: 1;

/* ...equals these three properties: */
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0;
Key Point: When you see flex: 1, think "grow evenly from 0"

Flex Properties Recap

flex-grow
â€Ē Controls how items grow to fill space
â€Ē Default: 0 (don't grow)
â€Ē flex-grow: 1 = all items grow equally
flex-shrink
â€Ē Controls how items shrink when space is tight
â€Ē Default: 1 (shrink evenly)
â€Ē flex-shrink: 0 = never shrink
flex-basis
â€Ē Sets the starting size before growing/shrinking
â€Ē Default: auto (check for width/height)
â€Ē With flex: 1 it becomes 0

Important Gotcha! ⚠ïļ

/* These are different! */

flex: 1; /* flex: 1 1 0 */
flex-grow: 1; /* flex: 0 1 auto */
flex: auto; /* flex: 1 1 auto */
Remember: flex: 1 sets basis to 0, but using flex-grow: 1 alone keeps basis at auto

Part 2: Flexbox Axes 🧭

The Core Concept

Every flex container has:

Main → Cross ↓

Flex Direction

.container {
  flex-direction: row; /* Default: left to right */
  flex-direction: column; /* Top to bottom */
}

Row (default)

â€Ē Main axis: horizontal (→)
â€Ē Cross axis: vertical (↓)

Column

â€Ē Main axis: vertical (↓)
â€Ē Cross axis: horizontal (→)

Visual Example: Row vs Column

Row Direction (default):

Box 1
Box 2
Box 3

Column Direction:

Box 1
Box 2
Box 3

Critical Concept: Basis Changes!

When flex-direction changes, so does what flex-basis refers to:

In Row Direction:

  • flex-basis = width
  • Items fill container width

In Column Direction:

  • flex-basis = height
  • Items need explicit height or content

Common Column Pitfall ðŸŠĪ

.container {
  display: flex;
  flex-direction: column;
}

.item {
  flex: 1; /* Might collapse! */
}
Why? flex: 1 means flex-basis: 0, and empty divs have 0 height!
Solution: Use flex: 1 1 auto or set container height

Part 3: Alignment ðŸŽŊ

Positioning Flex Items

Two main properties for positioning:

justify-content
â€Ē Aligns items along the main axis
â€Ē Think: "justifying text" (horizontal)
align-items
â€Ē Aligns items along the cross axis
â€Ē Think: "aligning to baseline" (vertical)

Justify-Content Values

Along the main axis:

.container {
  justify-content: flex-start; /* Default */
  justify-content: flex-end;
  justify-content: center;
  justify-content: space-between;
  justify-content: space-around;
  justify-content: space-evenly;
}

Justify-Content Visual

flex-start (default)

Box
Box
Box

center

Box
Box
Box

space-between

Box
Box
Box

Align-Items Values

Along the cross axis:

.container {
  align-items: stretch; /* Default */
  align-items: flex-start;
  align-items: flex-end;
  align-items: center;
  align-items: baseline;
}

Perfect Centering ðŸŽŊ

The holy grail of CSS - centering a div!

.container {
  display: flex;
  justify-content: center; /* Center on main axis */
  align-items: center; /* Center on cross axis */
}

This centers items both horizontally AND vertically!

Direction Changes Everything!

Remember: When flex-direction: column:

.container {
  flex-direction: column;
  justify-content: center; /* Now vertical! */
  align-items: center; /* Now horizontal! */
}
This is opposite of the default row behavior!

The Gap Property 🌉

Modern spacing solution - adds space BETWEEN items:

.container {
  display: flex;
  gap: 16px; /* Space between all items */
}

Benefits over margin:

Part 4: Practice Examples ðŸ’ŧ

Let's see flexbox in action with real-world patterns!

Example 1: Navigation Bar

.nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
}

.nav-links {
  display: flex;
  gap: 2rem;
}
Logo on left, links on right, everything vertically centered

Example 2: Card Layout

.card-container {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap; /* Bonus property! */
}

.card {
  flex: 1 1 300px; /* Grow, shrink, min 300px */
}
Responsive cards that grow and wrap as needed

Example 3: Centered Modal

.modal-overlay {
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  inset: 0;
}

.modal {
  /* No flex needed - stays its size */
  width: 500px;
  max-width: 90%;
}
Perfect centering for popups and modals

Example 4: Footer with Columns

.footer {
  display: flex;
  justify-content: space-around;
  gap: 2rem;
}

.footer-column {
  flex: 1;
}
Equal-width columns with consistent spacing

Common Patterns to Remember

  1. Center everything: justify-content: center; align-items: center;
  2. Space between items: justify-content: space-between;
  3. Sticky footer: flex-direction: column; flex: 1; on main content
  4. Equal columns: flex: 1; on all children
  5. Prevent shrinking: flex-shrink: 0;

Debugging Flexbox 🐛

Tips when things go wrong:

  1. Check flex-direction - Are your axes what you think?
  2. Check flex-basis - Is it 0 or auto?
  3. Check container height - Column direction needs it!
  4. Use browser DevTools - Firefox has amazing flexbox inspector
  5. Add borders - Visualize what's happening

Quick Quiz ðŸĪ”

  1. What's the difference between flex: 1 and flex-grow: 1?
  2. How do you center a div completely?
  3. What happens to justify-content when you use flex-direction: column?
  4. When would you use flex-shrink: 0?

Think about these - we'll discuss answers together!

Resources for Practice 📚

Interactive Games:

References:

Key Takeaways ðŸŽŊ

Homework Reminder 📌

Landing Page Project

DUE: Next Thursday Night

Requirements:

Start early! Use what we learned today.

Questions? ðŸĪš

Next Class:

Office Hours:

Let's Code! ðŸ’ŧ

Time for live practice - open your editors!

We'll build:

  1. A navigation bar
  2. A hero section with centered content
  3. A card layout with flex

Tip: Follow along and ask questions!

Thank You! 🙏

Remember:

See you next class! 👋