Responsive Web Design and Media Queries
Creating adaptive mobile-first layouts: @media queries, viewport breakpoints, fluid clamp() typography, and modern container queries (@container).
Responsive Web Design and Media Queries
Responsive Web Design (RWD) ensures that web applications look and function optimally across all device form factors—from mobile smartphones and tablets to high-resolution desktop monitors.
1. The Mobile-First Approach
In mobile-first design, default styles outside media queries target small mobile screens. Media queries using min-width progressively enhance the layout for wider viewports:
/* 1. Base Mobile Styles (Default) */
.container {
padding: 1rem;
font-size: 1rem;
}
.nav-links {
display: none; /* Hide on mobile behind hamburger button */
}
/* 2. Tablet Breakpoint (≥ 768px) */
@media (min-width: 768px) {
.container {
padding: 2rem;
}
.nav-links {
display: flex;
gap: 1.5rem;
}
}
/* 3. Desktop Breakpoint (≥ 1024px) */
@media (min-width: 1024px) {
.container {
max-width: 1200px;
margin: 0 auto;
}
}2. Standard Industry Breakpoints
| Device Category | Breakpoint Range | Tailwind Equivalent |
|---|---|---|
| Mobile Phones | < 640px | Default (no prefix) |
| Large Phones / Phablets | ≥ 640px | sm: |
| Tablets / iPads | ≥ 768px | md: |
| Laptops / Small Desktops | ≥ 1024px | lg: |
| High-Resolution Desktops | ≥ 1280px | xl: |
| Ultra-Wide Displays | ≥ 1536px | 2xl: |
3. Fluid Typography with clamp()
CSS clamp(min, preferred, max) calculates dynamic typography that scales smoothly with the viewport width without requiring multiple media query steps:
h1 {
/* Minimum 1.75rem (28px), scales with 4vw, maximum 3.5rem (56px) */
font-size: clamp(1.75rem, 4vw + 1rem, 3.5rem);
}4. Modern Container Queries (@container)
While media queries check the browser viewport, Container Queries evaluate the width of the parent component:
.card-wrapper {
container-type: inline-size;
}
@container (min-width: 400px) {
.card-item {
display: flex; /* Switch from vertical stack to horizontal row */
}
}Related Topics
CSS Grid Layout
Mastering two-dimensional CSS Grid: grid-template-columns, fr unit, repeat, auto-fit, minmax, and grid-template-areas.
CSS Custom Properties (Variables)
Mastering CSS Variables: declaration syntax (--var), var() fallback values, scoping, dark mode theming, and runtime JavaScript manipulation.