🎯 Session Overview
Welcome to Session 6! In the last session, we learned Flexbox, which is amazing for organizing items in a single row or a single column.
But what if you need to build an entire webpage layout? You need a Header at the top, a Sidebar on the left, Main Content in the middle, and a Footer at the bottom. Doing that with Flexbox is messy.
Today, you will learn the ultimate layout tool: CSS Grid. By the end of this session, you will learn to:
- Understand what CSS Grid is and why it's better for 2D layouts.
- Create a Grid Container and define precise Rows and Columns.
- Use the powerful
fr(fraction) unit. - Command grid items to stretch and span across multiple cells.
- Build a complete, professional website layout easily!
⏪ Quick Recap: Session 5 (Flexbox)
Let's remember how Flexbox works:
display: flex;turns the parent into a flex container.- It aligns items along a Main Axis (row or column) and a Cross Axis.
- We used
justify-contentto space items out horizontally andalign-itemsto center them vertically.
Rule of thumb: Use Flexbox for 1-Direction layouts (like a navigation bar). Use Grid for 2-Direction layouts (the whole page grid).
📏 What is CSS Grid?
CSS Grid is a two-dimensional layout system. That means it can handle both Columns (vertical) and Rows (horizontal) at the exact same time!
Just like Flexbox, it starts by defining a parent container. Any element placed directly inside this container automatically becomes a Grid Item.
.container {
display: grid;
}
But unlike Flexbox, simply typing display: grid; doesn't do much visually. You have to tell the Grid exactly what its columns and rows look like!
📐 Defining Columns and Rows
To draw the grid, we use two properties on the container: grid-template-columns and grid-template-rows.
You can use pixels (px) or percentages (%), but Grid gives us a brand new, magical unit called the fr (fraction) unit. It means "one piece of the available free space".
.container {
display: grid;
/* Creates 3 columns that are all exactly equal in width */
grid-template-columns: 1fr 1fr 1fr;
/* Creates 2 rows that are exactly 100px tall */
grid-template-rows: 100px 100px;
/* Adds 10px of space between all cells! */
gap: 10px;
}
If you use 33.3% 33.3% 33.3% and then add a gap: 10px;, the total width becomes 100% PLUS 20px of gap. Your grid will break and overflow the page!
The fr unit is incredibly smart. It calculates the gap FIRST, and then takes whatever space is left over and divides it into equal fractions. It never breaks!
✨ Interactive Demo: The Grid Maker
Change the column and gap settings below. Notice how the items automatically wrap to the next row when they run out of columns!
🏗️ Placing Items & Spanning Cells
By default, Grid places one item per cell, reading left to right. But what if you are building a website layout, and you want the Header to stretch across the entire top row?
We apply properties to the Grid Item to tell it where to start and stop using grid lines.
.header {
/* Start at grid line 1, stretch until grid line 4 */
grid-column: 1 / 4;
}
.main-content {
/* Start at grid line 2, stretch until line 4 */
grid-column: 2 / 4;
}
Pro Tip: If you want an item to span all the way to the end, but you don't know how many columns there are, you can use grid-column: 1 / -1;
🏋️♂️ Practice Exercises
Open your code editor. Create an HTML file with a <div class="grid-container"> containing 4 boxes (<div class="box">).
Exercise 1: Three Equal Columns
In your CSS, turn the .grid-container into a grid. Define 3 equal columns using the fr unit.
display: grid;grid-template-columns: 1fr 1fr 1fr;
Exercise 2: Fixed and Flexible
Change your columns so that the first column is exactly 200px wide, and the second column takes up the rest of the space (1fr). Remove the third column entirely.
grid-template-columns: 200px 1fr;Since there are 4 boxes and only 2 columns, Grid will automatically create a second row for the extra 2 boxes!
Exercise 3: Mind the Gap
The boxes look squished. Add a 15px gap between all the grid items.
.grid-container: gap: 15px;
🔥 Mini Challenge: The Photo Gallery
Create a grid container with 6 boxes inside it.
Your Goal: Write CSS to create a gallery layout that looks like this:
- The grid has 3 equal columns (
1fr 1fr 1fr). - The very first box stretches across the entire top row (spanning from line 1 to line 4).
- The remaining 5 boxes sit normally in the rows underneath it.
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 10px;
}
.box:first-child { /* Selects only the 1st box! */
grid-column: 1 / 4;
}
🏠 Homework Project: The Holy Grail Layout
Your task is to build the classic website structure using CSS Grid.
- Create a
.containerwith 4 items inside: Header, Sidebar, Main Content, and Footer. - Set the grid to have 2 columns:
grid-template-columns: 250px 1fr; - Target the Header and tell it to span both columns (
grid-column: 1 / 3;). - Target the Footer and tell it to span both columns too!
- The Sidebar and Main Content will automatically sit perfectly in the middle row!
Give each section a different background color so you can see the layout clearly.
📝 Session Summary
- CSS Grid is a 2-Dimensional layout system perfect for creating whole page structures (Rows AND Columns).
- You define the structure on the Parent using
display: grid;,grid-template-columns, andgrid-template-rows. - The
fr(fraction) unit is the best way to divide space.1fr 1fr 1frcreates three equal columns. gapadds space between the grid cells.- You can command a child item to stretch across multiple cells using
grid-column: start-line / end-line;.