2. Modern CSS Layout & Design Systems

CSS Grid & Mobile-First Responsive Design

Build 2-dimensional layouts effortlessly with CSS Grid (fr units, auto-fit, minmax, spanning cards) and structure mobile-first media queries that adapt from phones to widescreens.

3 min readUpdated 2026-11-29
In This Lesson
  • Build structured multi-column page and card layouts using CSS Grid
  • Create self-adapting responsive columns in one line with repeat(auto-fit, minmax())
  • Write mobile-first @media (min-width: ...) breakpoints without horizontal overflow

Flexbox excels at aligning items along one axis—a top navbar, a button group, or a vertical sidebar menu. The moment you need rows and columns working together (such as a course catalog, dashboard metrics, or a magazine layout), CSS Grid is the cleaner tool.

Defining Columns & Tracks with fr Units

CSS Grid: Responsive Tracks with repeat(auto-fit, minmax(220px, 1fr))
Card 11fr Track
Card 21fr Track
Card 31fr Track

gap: 1rem creates spacing ONLY between tracks—never on outer edges

1fr divides remaining track space evenly. Combined with auto-fit and minmax(), cards wrap cleanly from 3 columns on desktop to 1 column on mobile with zero media queries.

CSS Grid introduces the fr (fraction) unit—which divides leftover free space proportionally after fixed gaps are subtracted:

css
.dashboard {
  display: grid;
  grid-template-columns: 240px 1fr; /* Fixed 240px sidebar + fluid main panel */
  gap: 24px;
}

Want three equal columns that automatically account for the 20px gaps between them without needing math like width: 33.33%? Just write:

css
.three-cols {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

And if a hero card inside that grid should stretch across two columns, add grid-column: span 2; to just that item.

Self-Adapting Responsive Grids in One Line

Instead of manually writing three separate media queries to switch from 1 column on phones → 2 columns on tablets → 3 columns on laptops, CSS Grid calculates the optimal column count automatically:

css
.course-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 20px;
}

Read repeat(auto-fit, minmax(220px, 1fr)) in plain English: "Fit as many columns as safely belong in this container, keeping every column at least 220px wide and letting all columns share leftover width equally (1fr)!"

Mobile-First Media Queries (@media)

When a page layout genuinely needs custom adjustments across devices (for instance, hiding a desktop-only sidebar or switching typography scale), build mobile styles first as your default, then layer wider rules upward using min-width:

css
/* Base styles apply cleanly to mobile phones (320px+) */
.hero-title {
  font-size: 1.75rem;
}

/* Tablet and larger (768px+) */
@media (min-width: 768px) {
  .hero-title {
    font-size: 2.5rem;
  }
}

/* Desktop screens (1024px+) */
@media (min-width: 1024px) {
  .hero-title {
    font-size: 3.25rem;
  }
}
BreakpointTypical DeviceCommon Layout Adjustment
Base (no query)Phones (320px+)Single column stack, roomy tap buttons
min-width: 640pxLarge phones / small tablets2-column summary cards, side-by-side form inputs
min-width: 768pxTablets (iPad)Desktop navbar replaces mobile toggle, larger type
min-width: 1024pxLaptops & DesktopsSidebar + main reading column (2–3fr)
Fixed Pixel Widths Break Mobile Screens

Writing width: 800px; forces mobile browsers to overflow sideways with an ugly horizontal scrollbar! Always write width: 100%; max-width: 800px; margin-inline: auto; so containers shrink down smoothly on small viewports.

Try It Live: Auto-Fitting Card Grid

Change minmax(140px, 1fr) to minmax(110px, 1fr) or add grid-column: span 2; on .featured to watch the 2D grid reorganize automatically:

HTML / CSS / JSLive CSS Grid — Responsive Cards & Span
Live Browser Preview

Quick Checkpoints

Check Your Understanding

Why is width: 100%; max-width: 800px; better than width: 800px; for page containers?

Check Your Understanding

When writing CSS Grid grid-template-columns: 220px 1fr;, what does 1fr mean?