🎯 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:
- Understand what Flexbox is and how it solves layout problems.
- Create a Flex Container to control the elements inside it.
- Align elements perfectly in rows or columns.
- Control the spacing and gaps between items automatically.
- Build real-world layouts like Navigation Bars effortlessly.
⏪ Quick Recap: Session 4
In our last session, we looked at how individual boxes behave:
- The Box Model: Every element has Content, Padding (inside space), Border, and Margin (outside space).
- Block vs Inline: Block elements take up the full width and stack vertically. Inline elements sit side-by-side but ignore width/height.
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:
- The Flex Container (The Parent): This is the big box. You apply
display: flex;to this box. It acts like the boss. - 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 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).
- Main Axis: The primary direction flex items are laid out in. (By default, left to right).
- Cross Axis: The direction perpendicular to the main axis. (By default, top to bottom).
🕹️ 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.
row(default): Left to right.column: Top to bottom (stacks them).row-reverse/column-reverse: Same thing, but backwards!
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).
flex-start: Pack items at the start (left).center: Pack items exactly in the middle.flex-end: Pack items at the end (right).space-between: Push the first item to the far left, the last to the far right, and space the rest evenly. (Perfect for Navbars!)space-around: Give every item equal space around itself.
3. align-items (Moving along the Cross Axis)
This tells the items how to align on the Cross Axis (vertically, if in a row).
flex-start: Align to the top.center: Align exactly in the vertical middle. (This used to be the hardest thing to do in CSS!)stretch(default): Stretch to fill the height of the container.
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.
🧒 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:
flex-grow: Dictates how much of the "leftover" empty space an item should grab. If you give one itemflex-grow: 1;, it will stretch to fill all remaining empty space!flex-shrink: Determines how an item shrinks if there isn't enough space.flex-basis: The default starting size of an element before it grows or shrinks (kind of likewidth, but smarter).
.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.
justify-content: center;align-items: center;
Exercise 3: Breathing Room
The boxes look crowded. Add 25px of space between each box without using margins.
.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.
- Create a
headerat the top. - Below it, create a
<div class="card-container">. - Inside the container, create 3 separate
<div class="feature-card">elements with some text inside. - Use Flexbox on the
.card-containerto make the three cards sit in a horizontal row. - Use
justify-content: center;andgap: 30px;to space them beautifully. - Create a
footerat the bottom.
📝 Session Summary
- Flexbox is a layout system designed for placing items in a single row or a single column.
- To use it, you must apply
display: flex;to the Parent Container. - The container controls its children using two axes: The Main Axis and the Cross Axis.
justify-contentaligns items along the Main Axis (e.g., center, space-between).align-itemsaligns items along the Cross Axis (e.g., center, stretch).flex-directionallows you to change the layout from a row to a column.gapis the modern, easy way to put space between flex items without using messy margins.