Lab 05: Responsive Design

Time: 30 minutes | Level: Practitioner | Docker: docker run -it --rm node:20-alpine sh

Overview

Build truly responsive layouts with mobile-first methodology, fluid typography via clamp(), container queries, modern viewport units, intrinsic sizing, and responsive images.


Step 1: Mobile-First Methodology

Mobile-first means: write styles for small screens first, then use min-width queries to enhance for larger screens.

/* ❌ Desktop-first: start big, scale down */
.nav { display: flex; gap: 2rem; }
@media (max-width: 768px) { .nav { flex-direction: column; } }

/* ✓ Mobile-first: start small, scale up */
.nav {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

@media (min-width: 640px) {  /* sm */
  .nav { flex-direction: row; gap: 1rem; }
}

@media (min-width: 1024px) { /* lg */
  .nav { gap: 2rem; }
}

Breakpoint system:

💡 Mobile-first is better for performance: mobile devices download the minimal CSS first, then progressive enhancements load with media queries.


Step 2: Fluid Typography with clamp()

clamp(min, preferred, max) — value is clamped between min and max, tracking preferred in between.


Step 3: Container Queries

Container queries respond to the container's size, not the viewport.


Step 4: Modern Viewport Units


Step 5: Intrinsic Sizing


Step 6: Responsive Images


Step 7: Complete Responsive Layout


Step 8: Capstone — clamp() Type Scale Calculator

📸 Verified Output:


Summary

Feature
Syntax
Purpose

Mobile-first

@media (min-width: ...)

Start small, enhance up

Fluid type

clamp(min, vw-calc, max)

Smooth font scaling

Container query

@container (min-width: ...)

Component-level responsiveness

svh

100svh

Safe height (mobile browser)

dvh

100dvh

Dynamic height

min-content

width: min-content

Shrink to content

fit-content

width: fit-content

Content width, space-aware

srcset

srcset="img-400.jpg 400w"

Resolution switching

picture

<picture><source><img>

Art direction

object-fit

cover, contain

Image sizing in box

Last updated