TechTicket Logo
CSS Fundamentals and Modern Layouts

Session 5 — Flexbox Layout System

🎯 Session Overview

Welcome to Session 5! Before modern CSS, placing two boxes side-by-side on a webpage was a nightmare. Developers had to use hacks and tricks just to center text.

Then came Flexbox. It changed everything.

In this session, you will learn the absolute best way to arrange elements on a web page. You will learn how to:

⏪ Quick Recap: Session 4

In our last session, we looked at how individual boxes behave:

But what if we have 3 Block elements (like div boxes) and we want them to sit beautifully side-by-side in a neat row? That's what Flexbox is for!

📦 What is Flexbox?

Flexbox (Flexible Box Layout) is a CSS layout system designed specifically to lay out items in a single direction—either in a row (horizontally) or a column (vertically).

It is brilliant because it automatically figures out how to distribute space, shrink items so they don't break the page, and stretch items to fill empty gaps.

The Two Rules of Flexbox

To use Flexbox, you must understand the relationship between two things:

  1. The Flex Container (The Parent): This is the big box. You apply display: flex; to this box. It acts like the boss.
  2. The Flex Items (The Children): These are the elements directly inside the container. The moment the parent becomes a flex container, the children automatically become flex items and follow the boss's rules!
HTML & CSS
<!-- HTML Structure -->
<div class="container"> <!-- The Parent / Boss -->
  <div class="item">Box 1</div> <!-- Child -->
  <div class="item">Box 2</div> <!-- Child -->
</div>

/* CSS Styling */
.container {
  display: flex; /* Magic happens here! */
}

By default, the moment you say display: flex;, all the children will jump up and sit side-by-side in a row!

⚔️ The Secret Architecture: The Two Axes

To control where your items go, you need to understand that Flexbox operates on a grid of two invisible lines (axes).

.container (flex-direction: row) Item 1 Item 2 Main Axis Cross Axis

🕹️ Controlling the Layout (Flex Properties)

We use specific properties on the Container to command the children how to align along these axes.

1. flex-direction (Changes the Main Axis)

Do you want your items in a row, or stacked in a column? This property defines the direction of the Main Axis.

2. justify-content (Moving along the Main Axis)

This tells the items how to distribute themselves along the Main Axis (horizontally, if in a row).

3. align-items (Moving along the Cross Axis)

This tells the items how to align on the Cross Axis (vertically, if in a row).

4. gap (Breathing Room)

Instead of doing complicated math with margins, just use gap! It automatically adds space exactly between flex items.

.container {
  display: flex;
  gap: 20px; /* Adds 20px of space between all items */
}

✨ Interactive Flexbox Playground

Try changing the settings below to see how the container commands the items! Notice how justify-content and align-items react when you change the flex-direction.

1
2
3

🧒 Flex Item Properties (For the Children)

Usually, the parent container does all the work. But sometimes, you want a specific child item to break the rules or take up more space.

You apply these properties directly to the child element:

.container {
  display: flex;
}
.greedy-child {
  flex-grow: 1; /* I will eat all the remaining space! */
}

🏋️‍♂️ Practice Exercises

Open your code editor. Create an HTML file with a <div class="container"> that contains three <div class="box"> elements.

Exercise 1: Activate Flexbox

In your CSS, target the .container and activate the Flexbox system. What happens to the boxes?

.container { display: flex; }
The boxes should immediately jump into a horizontal row side-by-side!

Exercise 2: Perfect Centering

Give your .container a height of 300px. Now, use Flexbox properties to center the boxes perfectly in the middle of the container, both horizontally and vertically.

You need two properties! One for the main axis, one for the cross axis.
justify-content: center;
align-items: center;

Exercise 3: Breathing Room

The boxes look crowded. Add 25px of space between each box without using margins.

Apply this to the .container:
gap: 25px;

🔥 Mini Challenge: The Modern Navbar

Almost every modern website navigation bar is built with Flexbox. Let's build one!

HTML: Create a <nav class="navbar">. Inside it, place an <h1> for the Logo on the left, and a <div class="links"> on the right containing three <a> tags.

CSS Goal: Use Flexbox on the .navbar to push the Logo to the far left edge, and the Links div to the far right edge.

The secret is space-between!

.navbar {
  display: flex;
  justify-content: space-between; /* Pushes items to opposite edges */
  align-items: center; /* Centers them vertically */
  padding: 20px;
  background-color: #1e293b;
}

/* Bonus: Use flexbox on the links container to space them out too! */
.links {
  display: flex;
  gap: 15px;
}

🏠 Homework Project: Card Layout

Your task is to build a responsive "Features" section for a website.

  1. Create a header at the top.
  2. Below it, create a <div class="card-container">.
  3. Inside the container, create 3 separate <div class="feature-card"> elements with some text inside.
  4. Use Flexbox on the .card-container to make the three cards sit in a horizontal row.
  5. Use justify-content: center; and gap: 30px; to space them beautifully.
  6. Create a footer at the bottom.

📝 Session Summary

⬅ Previous Session Next Session ➡