Episode 1 — Fundamentals / 1.9 — CSS Responsive Design

1.9 — CSS Responsive Design: Quick Revision

Compact cheat sheet. Print-friendly.

How to use this material (instructions)

  1. Skim top-to-bottom in one pass before interviews or exams.
  2. If a row feels fuzzy — reopen the matching lesson: README.md1.9.a1.9.g.
  3. Drills1.9-Exercise-Questions.md.
  4. Polished answers1.9-Interview-Questions.md.

1.9.a Mobile-First Strategy

Core Idea

Base CSS (no query) = mobile styles
@media (min-width: …) = add complexity for wider viewports
ApproachQuery typeMental model
Mobile-firstmin-width"What do I add?"
Desktop-firstmax-width"What do I remove?"

Touch Targets

StandardMinimum size
Google / Material48 × 48 px
WCAG 2.5.8 (AAA)44 × 44 px

Performance

  • Less CSS parsed on mobile (complex rules inside queries phones don't match)
  • Fewer overrides = smaller file = faster first paint

1.9.b Media Queries

Syntax

@media <type>? and (<feature>: <value>) { … }

Media Types

TypeTarget
allEverything (default)
screenScreens
printPrinted pages

Key Media Features

FeatureExample
min-width / max-width(min-width: 768px)
orientationportrait / landscape
prefers-color-schemelight / dark
prefers-reduced-motionreduce / no-preference
hoverhover / none
pointerfine / coarse / none

Combining

OperatorMeaning
andAll must be true
,Any can be true (OR)
notNegates entire query (to comma)

Level 4 Range Syntax

/* Old */  @media (min-width: 768px) and (max-width: 1023px) { … }
/* New */  @media (768px <= width < 1024px) { … }

Reduced Motion Pattern

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

1.9.c Breakpoint Planning

Common Breakpoints

NameMin-widthTypical
xs0Small phones
sm480pxLarge phones
md640pxTablets
lg1024pxLaptops
xl1280pxDesktops
2xl1536pxUltra-wide

Rules

  • Content-driven > device-driven — add breakpoint where layout breaks
  • Major breakpoints = layout change; Minor = spacing tweak
  • Cap at 3–5 for most projects
  • Fluid techniques firstauto-fit, clamp() can eliminate breakpoints

Zero-Breakpoint Grid

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1.5rem;
}

1.9.d Responsive Images

object-fit Values

ValueBehavior
fillStretch to fill (distorts)
containFit inside (may show gaps)
coverCover box (may crop)
noneNatural size
scale-downSmaller of contain / none

srcset vs <picture>

srcset + sizes<picture>
PurposeResolution switchingArt direction
Who decidesBrowserAuthor

Format Priority

AVIF → WebP → JPEG

Image Checklist

1. Right format (AVIF > WebP > JPEG)
2. Right size (≤ display width × DPR)
3. Compress (80% quality for lossy)
4. srcset + sizes
5. loading="lazy" (below fold)
6. width + height or aspect-ratio (CLS)
7. fetchpriority="high" (hero/LCP)

Aspect Ratio

.thumb { width: 100%; aspect-ratio: 16 / 9; object-fit: cover; }

1.9.e Fluid Typography

The Formula

font-size: clamp(MIN, PREFERRED, MAX);
h1 { font-size: clamp(1.5rem, 2.5vw + 0.5rem, 3rem); }

How It Works

         max ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
              ╱‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
preferred    ╱
            ╱
  min ─ ─ ─
       small ──────────── viewport ──────────── large

Accessibility Rule

  • Always use rem for min and max — respects user font-size settings
  • Never px bounds on text

Fluid Type Scale Example

:root {
  --text-sm:   clamp(0.8rem,   0.17vw + 0.75rem,  0.875rem);
  --text-base: clamp(1rem,     0.34vw + 0.91rem,   1.125rem);
  --text-lg:   clamp(1.25rem,  0.56vw + 1.08rem,   1.5rem);
  --text-xl:   clamp(1.563rem, 0.89vw + 1.3rem,    2rem);
  --text-2xl:  clamp(1.953rem, 1.36vw + 1.56rem,   2.75rem);
}

Line Length

.prose { max-width: 65ch; } /* 45–75 characters optimal */

1.9.f Spacing & Rhythm

4px Base Spacing Scale

Tokenrempx
--space-10.25rem4
--space-20.5rem8
--space-30.75rem12
--space-41rem16
--space-61.5rem24
--space-82rem32
--space-123rem48
--space-164rem64

Key Patterns

/* Stack (vertical spacing) */
.stack { display: flex; flex-direction: column; gap: var(--space-4); }

/* Cluster (horizontal wrap) */
.cluster { display: flex; flex-wrap: wrap; gap: var(--space-3); }

Responsive Spacing

.section { padding-block: clamp(2rem, 5vw, 6rem); }

Golden Rule

Use gap instead of margin on children. No :last-child hacks. No magic numbers.


1.9.g CSS Frameworks

Categories

CategoryExampleApproach
Utility-firstTailwindSmall classes composed in HTML
Component-basedBootstrapPre-designed components, class API
HybridFoundationMix of both

When to Use

ScenarioChoice
Prototype / MVPFramework
Unique brand / marketingCustom CSS
Learning CSSCustom CSS first
Large team, admin dashboardFramework
Design system / component libraryCustom or token library

Quick Comparison

BootstrapTailwind
Pre-built components50+None (compose your own)
Prod CSS (purged)~50 KB~10–15 KB
Design uniquenessLow by defaultHigh
JS includedYesNo

Quick-Fire Recall

Mobile-first    → min-width queries, progressive enhancement
Media features  → width, color-scheme, reduced-motion, hover, pointer
Breakpoints     → content-driven, 3–5 max, fluid first
Images          → srcset (resolution) vs <picture> (art direction)
                → AVIF > WebP > JPEG, lazy + aspect-ratio
Fluid type      → clamp(rem-min, vw + rem, rem-max)
Spacing         → 4px base, tokens, gap, no magic numbers
Frameworks      → utility-first vs component-based, purge unused CSS

Navigation: ← 1.9 Overview · 1.9-Exercise-Questions · 1.9-Interview-Questions