Lab 08: CSS Grid

Objective

Master CSS Grid to build two-dimensional layouts β€” controlling both rows and columns simultaneously for complex page structures.

Background

CSS Grid is the most powerful layout system in CSS. Unlike Flexbox (one dimension), Grid works in two dimensions simultaneously. It excels at page-level layout, dashboards, image galleries, and magazine-style designs. Grid and Flexbox are complementary β€” use Grid for 2D layouts, Flexbox for 1D alignment.

Time

35 minutes

Prerequisites

  • Lab 07: CSS Flexbox Layout

Tools

docker run --rm -it -v /tmp:/workspace zchencow/innozverse-htmlcss:latest bash

Lab Instructions

Step 1: Grid Container Basics

Write this file:

πŸ’‘ fr units (fractional) are Grid's superpower. 1fr 2fr means the second column is twice as wide as the first. grid-template-columns: 200px 1fr 2fr creates a fixed sidebar + flexible content area. The fr unit distributes remaining space after fixed sizes.

πŸ“Έ Verified Output:


Step 2: Grid Placement β€” Column & Row Spanning

Write this file:

πŸ’‘ Grid lines are numbered from 1. grid-column: 1 / 3 means "start at line 1, end at line 3" β€” spanning 2 columns. grid-column: 1 / -1 spans the full width. You can also use span: grid-column: span 2 means "take 2 columns from wherever I am."

πŸ“Έ Verified Output:


Step 3: Named Template Areas

Write this file:

πŸ’‘ grid-template-areas is like a visual map of your layout in CSS. Each quoted string is a row, each word is a cell. A . creates an empty cell. This ASCII-art approach makes layouts self-documenting β€” you can literally see the page structure in the CSS.

πŸ“Έ Verified Output:


Step 4: auto-fill, auto-fit & minmax()

Write this file:

πŸ’‘ repeat(auto-fit, minmax(150px, 1fr)) is the most powerful one-liner in CSS. It creates as many columns as fit, each at least 150px wide, stretching to fill. This replaces complex media query breakpoints. auto-fill creates empty columns; auto-fit collapses them β€” prefer auto-fit.

πŸ“Έ Verified Output:


Step 5: Responsive Grid Without Media Queries

Write this file:

πŸ’‘ Intrinsic responsiveness β€” no media queries needed! The grid automatically creates columns as space allows. On mobile: 1 column. On tablet: 2-3 columns. On desktop: 4 columns. All from one line of CSS. This is modern CSS at its most powerful.

πŸ“Έ Verified Output:


Step 6: Magazine Layout with Grid

Write this file:

πŸ’‘ Magazine-style layouts used to require JavaScript masonry libraries. With Grid's explicit placement, you can span items across rows and columns directly in CSS. grid-column: 1 / -1 is especially handy β€” -1 always refers to the last line.

πŸ“Έ Verified Output:


Step 7: Dashboard Grid Layout

Write this file:

πŸ’‘ Grid within Grid (subgrid): The stats section is itself a 4-column grid inside the dashboard grid. Grid layouts naturally nest. Named areas make the code read like a wireframe β€” you can see the exact layout from the CSS alone.

πŸ“Έ Verified Output:


Write this file:

πŸ’‘ Grid masonry patterns β€” span 2 and span 2 on items create the Pinterest-like layout. Combined with auto-fit + minmax, the gallery reflows perfectly on any screen size. The hover overlay uses CSS transitions (covered in Lab 10) β€” these techniques compound beautifully.

πŸ“Έ Verified Output:


Verification

Summary

Concept
Syntax
Use Case

Basic grid

display: grid

Enable grid on container

Fixed columns

grid-template-columns: 200px 1fr

Sidebar + content

Responsive columns

repeat(auto-fit, minmax(250px, 1fr))

Card grids

Named areas

grid-template-areas

Page layout

Item placement

grid-column: 1 / 3

Spanning cells

Fractional units

fr

Proportional sizing

Further Reading

Last updated