Intermediate CSS - Grid

Intermediate CSS

CSS Grid Layout

CSCI 4513

Week 2, Lecture 3

Today's Learning Objectives

Norse Mythology Connection

The Building of Asgard's Wall

Poor planning nearly cost the gods everything, just as CSS Grid requires careful specification before implementation.

The Tale:

After a devastating war, Asgard's walls lay in ruins. A mysterious builder offered to construct an impenetrable wall in three seasons for the sun, moon, and goddess Freyja. The gods agreed, confident the task was impossible, setting one constraint: the builder must work alone. They allowed him his horse Svadilfari—not realizing this concession's power.

The builder worked with astonishing efficiency. As winter ended, the gods realized in horror that the wall was nearly complete. They had failed to properly scope the project and were about to lose everything. Loki desperately lured Svadilfari away, disrupting work just enough that the builder missed his deadline. The lesson remained: inadequate planning nearly cost the gods everything.

Planning Prevents Problems

Asgard's Wall Failure

  • Failed to scope the project
  • Didn't understand constraints
  • Nearly lost everything
  • Needed desperate last-minute fix

CSS Grid Planning

  • Define columns/rows needed
  • Plan for screen sizes
  • Avoid layout breakage
  • Prevent maintenance headaches
/* Proper planning prevents problems */
.container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 1rem;
}

Reflection Questions

Click each card to reveal thoughts!

❓ Question 1

The gods allowed the builder his horse without understanding its power. What CSS Grid features have you used without fully understanding them?

❓ Question 2

What requirements should you define before starting a grid layout?

❓ Question 3

The wall was nearly complete but broke at the deadline—like layouts that work on desktop but break on mobile. How can you prevent this?

🎮 Let's Explore Grid Together!

Before we dive into theory, let's visualize Grid in action!

This will feel foreign—that's okay! Hands-on exploration is the fastest way to build intuition about how Grid works.

How This Works:

🚀 Goal: Build intuition about grid lines and positioning before we explain the theory!

A Look Back at Flexbox

Flexbox Strengths

  • One-dimensional layouts (row OR column)
  • Content-first design
  • Flexible item sizing
  • Great for navigation, cards in a row

Flexbox Limitations

  • Two-dimensional layouts get complex
  • Items wrap unpredictably
  • Hard to align rows AND columns
  • Uneven item sizes difficult
💡 Remember: Flexbox wraps items to new lines, but can't easily control how items in different rows align with each other.

What is CSS Grid?

CSS Grid is a two-dimensional layout system designed for precise placement of items in both rows AND columns simultaneously.

Why Grid?

📰 Fun Fact: Grid was designed to bring print layout techniques to the web!

Creating a Grid Container

<div class="container">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
  <div>Item 4</div>
</div>
.container {
  display: grid;
  /* or display: inline-grid; */
}

Defining Columns and Rows

display: grid vs display: inline-grid

  • display: grid - Grid container behaves as a block-level element (takes full width, starts on new line)
  • display: inline-grid - Grid container behaves as an inline-level element (only as wide as content, flows with text)
  • Both create the same grid layout for children—only the container's external behavior differs!
/* Define 3 columns and 2 rows */
.container {
  display: grid;
  grid-template-columns: 50px 50px 50px;
  grid-template-rows: 50px 50px;
}
/* Shorthand: rows / columns */
.container {
  display: grid;
  grid-template: 50px 50px / 50px 50px 50px;
}
/* Different sizes for each column */
.container {
  grid-template-columns: 250px 50px 50px;
}

Grid Terminology

Grid Lines

The dividing lines between rows and columns. Numbered from 1.

Grid Track

The space between two grid lines (a single row or column).

Grid Cell

The space where a row and column intersect (like a spreadsheet cell).

Grid Area

Any rectangular section of the grid (one or more cells).

Explicit vs Implicit Grid

.container {
  display: grid;
  grid-template-columns: 50px 50px;
  grid-template-rows: 50px 50px;
  /* Only defines 2x2 grid (4 items) */
}

What if we have 5 items?

.container {
  grid-auto-rows: 50px; /* New rows match explicit rows */
}

🎮 Practice Time: Grid Garden

Let's practice positioning and spanning!

Your Task:

💡 Focus: The "span" keyword lets you span across tracks without counting line numbers!

Grid Gap (Gutters)

/* Separate row and column gaps */
.container {
  display: grid;
  grid-template: 50px 50px / 50px 50px;
  row-gap: 20px;
  column-gap: 10px;
}
/* Shorthand: row-gap column-gap */
.container {
  gap: 20px 10px;
}
/* Same gap for rows and columns */
.container {
  gap: 1rem;
}

Positioning Items by Line Numbers

/* Make item span multiple columns */
.item {
  grid-column-start: 1;
  grid-column-end: 3;   /* Spans columns 1-2 */
}

/* Make item span multiple rows */
.item {
  grid-row-start: 1;
  grid-row-end: 3;      /* Spans rows 1-2 */
}
/* Shorthand: start / end */
.item {
  grid-column: 1 / 3;
  grid-row: 1 / 3;
}

grid-area: Two Uses

Method 1: Line-based positioning

/* row-start / col-start / row-end / col-end */
.item {
  grid-area: 1 / 1 / 3 / 3;
}

Method 2: Named areas

/* Give item a name */
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }

/* Use names in template */
.container {
  grid-template-areas:
    "header  header"
    "sidebar content"
    "sidebar content";
}

grid-template-areas: Visual Layout

.container {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-rows: 100px 1fr 100px;
  grid-template-areas:
    "header  header"
    "sidebar content"
    "footer  footer";
}

.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }
.footer  { grid-area: footer; }

Use . to indicate empty cells:

grid-template-areas:
  "header header header"
  ".      content   ."
  "footer footer footer";

Advanced: repeat() Function

/* Instead of this tedious approach... */
.container {
  grid-template-columns: 150px 150px 150px 150px 150px;
}

/* Use repeat() */
.container {
  grid-template-columns: repeat(5, 150px);
}
/* Can mix different sizes */
.container {
  grid-template-columns: repeat(2, 2fr) repeat(3, 1fr);
  /* First 2 columns = 2fr, last 3 columns = 1fr */
}

Fractional Units (fr)

fr distributes remaining space proportionally.

/* Equal columns */
.container {
  grid-template-columns: 1fr 1fr 1fr 1fr;
  /* Each column gets 1/4 of available space */
}

/* Unequal distribution */
.container {
  grid-template-columns: 2fr 1fr 1fr;
  /* First column gets 2/4, others get 1/4 each */
}

/* Mix static and dynamic */
.container {
  grid-template-columns: 200px 1fr 1fr;
  /* First column = 200px, rest split remaining */
}

🎮 Practice Time: Grid Garden

Time to master grid areas and ordering!

Your Task:

💡 Focus: grid-area is a shorthand that positions items using start/end for both rows and columns!

min() and max() Functions

/* min() returns smallest value */
.container {
  grid-template-rows: min(100px, 50%);
  /* Whichever is smaller: 100px or 50% of container */
}

/* max() returns largest value */
.container {
  grid-template-columns: max(120px, 15%);
  /* Whichever is larger: 120px or 15% of container */
}
💡 Use case: Set minimum/maximum sizes while allowing flexibility.

minmax() Function

minmax(min, max) - Grid-specific function for setting track size boundaries.

/* Columns between 150px and 200px */
.container {
  grid-template-columns: repeat(5, minmax(150px, 200px));
}

/* Minimum width with flexible growth */
.container {
  grid-template-columns: minmax(200px, 1fr) minmax(300px, 2fr);
}

Tracks grow/shrink between min and max values as container resizes.

clamp() Function

/* clamp(minimum, ideal, maximum) */
.container {
  grid-template-columns: repeat(5, clamp(150px, 20%, 200px));
  /* Each column tries to be 20% width */
  /* But never smaller than 150px */
  /* And never larger than 200px */
}

clamp() vs minmax()

  • clamp(): Can be used anywhere in CSS
  • minmax(): Grid-specific only
  • Both set boundaries for flexible sizing

auto-fit and auto-fill

/* Responsive columns without media queries! */
.container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
}

How it works:

🔥 Pro Tip: This is the most powerful responsive Grid pattern!

auto-fit vs auto-fill

auto-fit

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

Collapses empty tracks. Items expand to fill container.

auto-fill

repeat(auto-fill,
  minmax(200px, 1fr)
)

Keeps empty tracks. Items stay at minimum size.

In most cases, auto-fit is what you want for responsive layouts.

When to Use Grid vs Flexbox

Use Flexbox When:

  • Layout is one-dimensional (row OR column)
  • Content should determine layout (content-first)
  • Items need to wrap naturally
  • Examples: navigation bars, card rows, button groups

Use Grid When:

  • Layout is two-dimensional (rows AND columns)
  • Layout structure comes first (layout-first)
  • Need precise control over both axes
  • Examples: page layouts, image galleries, dashboards

🎮 Practice Time: Grid Garden

Advanced challenges ahead!

Your Task:

💡 Focus: You'll need to combine multiple properties and think carefully about grid structure!

Combining Flexbox and Grid

/* Grid for overall page layout */
.page {
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar content"
    "footer footer";
}

/* Flex for items within grid cells */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.sidebar nav {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

Grid items can be flex containers! Use the best tool for each job.

Project: Admin Dashboard

Your Assignment:

Build a full admin dashboard layout using CSS Grid. This project will test everything you've learned about Grid!

Key Features:

Dashboard Project Steps

1. Layout (Grid)

2. Nesting (More Grids!)

3. Styling

Dashboard Project Tips

📌 Deploy: Push to GitHub and use GitHub Pages to publish your dashboard!

🎮 Final Grid Garden Challenge!

Finish strong—complete the final levels!

Your Task:

🎉 Congratulations! You've now practiced every major Grid concept. You're ready to build the Admin Dashboard!

Today's Takeaways

You Can Now:

📅 Next Class: JS Objects and Constructors

📝 Homework: Admin Dashboard