CSS Mobile-First Design
Mobile-first is not just a media query order — it is a design philosophy. You start with the constraints of the smallest screen: limited space, touch input, slower connections. Then you progressively enhance for wider screens. This produces leaner CSS, better performance on mobile devices, and forces better design thinking. This chapter covers the full strategy with real-world layout patterns for navigation, cards, heroes and typography.
The Mobile-First Strategy
The core idea is simple: write your default CSS for mobile, then use min-width media queries to add complexity as the screen gets larger. Compare
the two approaches:
/* Default: complex desktop styles */
.grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 2rem;
}
/* Then strip back for mobile */
@media (max-width: 767px) {
.grid {
grid-template-columns: 1fr;
gap: 1rem;
}
}
@media (max-width: 480px) {
.grid { gap: .75rem; }
}
Mobile must download ALL desktop CSS + overrides
/* Default: simple mobile styles */
.grid {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
}
/* Add complexity for larger screens */
@media (min-width: 640px) {
.grid {
grid-template-columns: 1fr 1fr;
gap: 1.5rem;
}
}
@media (min-width: 1024px) {
.grid {
grid-template-columns: 1fr 1fr 1fr;
gap: 2rem;
}
}
Mobile gets minimal CSS · desktop gets everything it needs
The Viewport Meta Tag — Required Foundation
Before any CSS media query will work correctly on a mobile browser, you must include
the viewport meta tag in your HTML <head>. Without it, mobile
browsers render the page at a virtual 980px wide and scale it down — bypassing your media queries entirely:
<!-- REQUIRED — put in <head> of every responsive page -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- width=device-width — use the real device width, not 980px
initial-scale=1.0 — don't zoom in or out on load -->
<!-- Optional additions -->
<meta name="viewport"
content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<!-- viewport-fit=cover: extend into notch/rounded corners on iPhones -->
<!-- NEVER add maximum-scale=1 or user-scalable=no
— it prevents accessibility zooming and breaks WCAG -->
Pattern 2 — Responsive Card Grid
Cards start as a single-column stacked list on mobile and expand to multi-column grids on wider screens. Two approaches: CSS Grid with explicit breakpoints, or the auto-fill technique that needs no media queries at all:
/* ── Approach 1: explicit breakpoints ── */
.card-grid {
display: grid;
grid-template-columns: 1fr; /* mobile: 1 column */
gap: 1rem;
}
@media (min-width: 640px) {
.card-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 1024px) {
.card-grid { grid-template-columns: repeat(3, 1fr); }
}
@media (min-width: 1280px) {
.card-grid { grid-template-columns: repeat(4, 1fr); }
}
/* ── Approach 2: auto-fill — no media queries needed! ── */
.card-grid-auto {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
/* ↑ Creates as many 280px columns as fit, all grow equally */
gap: 1.5rem;
}
/* On 320px screen: 1 column (280px fits once)
On 640px screen: 2 columns (280px fits twice)
On 900px screen: 3 columns — fully automatic! */
/* ── Card internals ── */
.card {
display: flex;
flex-direction: column;
border-radius: .75rem;
overflow: hidden;
}
.card-body { padding: clamp(1rem, 3vw, 1.5rem); flex: 1; }
.card img { width: 100%; aspect-ratio: 16/9; object-fit: cover; }
Pattern 3 — Responsive Hero Section
A hero starts as a stacked, centred column on mobile. On wider screens it becomes a two-column side-by-side layout with the text on the left and image on the right:
/* ── Mobile: stacked, centred ── */
.hero {
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
gap: 2rem;
padding: clamp(2rem, 8vw, 5rem) 1rem;
min-height: 100dvh;
}
.hero-content { max-width: 600px; }
.hero-image { width: 100%; max-width: 400px; }
.hero h1 {
font-size: clamp(2rem, 5vw + 1rem, 4rem);
line-height: 1.1;
}
/* ── Desktop: two columns ── */
@media (min-width: 768px) {
.hero {
flex-direction: row;
text-align: left;
min-height: unset;
}
.hero-content { flex: 1; }
.hero-image { flex: 1; max-width: none; }
}
/* ── XL: more padding, image offset ── */
@media (min-width: 1024px) {
.hero { gap: 4rem; padding-inline: clamp(1.5rem, 5vw, 4rem); }
}
Pattern 4 — Responsive Typography
Typography should be legible on small screens and proportionally larger on big ones —
without jarring jumps at breakpoints. Use clamp() for fluid scaling and
a sensible type scale:
/* Base — set once, rem units respect user preference */
html { font-size: 100%; } /* = browser default (16px) */
body {
font-size: clamp(1rem, 1.5vw + .25rem, 1.125rem);
line-height: 1.7;
color: #1e293b;
}
/* Fluid heading scale — clamp(mobile, fluid, desktop) */
h1 { font-size: clamp(1.75rem, 4vw + .5rem, 3rem); line-height: 1.1; }
h2 { font-size: clamp(1.5rem, 3vw + .5rem, 2.25rem); line-height: 1.2; }
h3 { font-size: clamp(1.25rem, 2.5vw + .25rem, 1.75rem); }
h4 { font-size: clamp(1.125rem, 2vw + .25rem, 1.375rem); }
/* Readable line length — 65ch is ~optimal */
p, li, blockquote { max-width: 65ch; }
/* Spacing that scales with font size */
h1, h2, h3 { margin-bottom: .5em; }
p { margin-bottom: 1em; }
/* Avoid px for font sizes — use rem or clamp */
Pattern 5 — Responsive Images
Images need three things to be responsive: they should never overflow their container, maintain aspect ratio, and fit their context:
img {
max-width: 100%;
height: auto;
display: block;
}
/* max-width: 100% = never
wider than container
height: auto = preserve
aspect ratio */
/* Modern approach */
.img-16-9 {
aspect-ratio: 16/9;
width: 100%;
object-fit: cover;
}
/* aspect-ratio is
well-supported since 2021 */
/* ── Global image reset (put in your base CSS) ── */
img, video, svg {
max-width: 100%;
height: auto;
display: block;
}
/* ── Aspect ratio (modern) ── */
.img-card { aspect-ratio: 16/9; width: 100%; object-fit: cover; }
.img-square{ aspect-ratio: 1/1; width: 100%; object-fit: cover; }
.img-portrait{ aspect-ratio: 3/4; width: 100%; object-fit: cover; }
/* ── Avatar ── */
.avatar {
width: clamp(40px, 8vw, 80px);
aspect-ratio: 1;
border-radius: 50%;
object-fit: cover;
}
/* ── Different image size per breakpoint (art direction) ── */
.hero-img {
width: 100%;
aspect-ratio: 4/3; /* square-ish on mobile */
}
@media (min-width: 768px) {
.hero-img { aspect-ratio: 16/9; } /* widescreen on tablet+ */
}
/* ── HTML: srcset for different resolutions ── */
<img src="img-800.jpg"
srcset="img-400.jpg 400w, img-800.jpg 800w, img-1600.jpg 1600w"
sizes="(min-width: 768px) 50vw, 100vw"
alt="Description" loading="lazy">
Touch Targets & Mobile UX
Mobile users tap with fingers, not mouse pointers. A tap target that's too small causes mis-taps and frustration. Google and Apple recommend a minimum touch target of 44×44px:
/* ── Minimum touch target size ── */
button, a, input, select, textarea {
min-height: 44px; /* Apple / WCAG recommendation */
min-width: 44px;
}
/* ── Touch vs mouse hover — don't break hover on mobile ── */
@media (hover: hover) and (pointer: fine) {
.btn:hover { background: darkblue; }
.nav-link:hover { color: blue; }
}
/* ↑ Hover styles only on real pointer devices — not touch */
/* ── Pointer coarse = touch device ── */
@media (pointer: coarse) {
.btn { padding: .875rem 1.5rem; } /* larger tap area */
.nav-link{ padding: .75rem 1rem; }
}
/* ── Prevent horizontal scroll ── */
html, body { overflow-x: hidden; }
/* ── Text size — no auto-inflate on iOS ── */
html { -webkit-text-size-adjust: 100%; }
/* ── Smooth scrolling (respect reduced motion) ── */
@media (prefers-reduced-motion: no-preference) {
html { scroll-behavior: smooth; }
}
/* ── Focus visible for keyboard users ── */
:focus-visible {
outline: 3px solid #264de4;
outline-offset: 2px;
}
Mobile-First CSS Boilerplate
A production-ready mobile-first CSS reset and foundation you can use as the starting point for any project:
/* ── 1. Box sizing reset ── */
*, *::before, *::after { box-sizing: border-box; }
/* ── 2. Base — respects user font preference ── */
html {
font-size: 100%;
-webkit-text-size-adjust:100%;
scroll-behavior: smooth;
}
@media (prefers-reduced-motion: reduce) {
html { scroll-behavior: auto; }
}
/* ── 3. Body ── */
body {
margin: 0;
font-family: system-ui, -apple-system, sans-serif;
font-size: clamp(1rem, 1.5vw + .25rem, 1.125rem);
line-height: 1.6;
overflow-x: hidden;
}
/* ── 4. Responsive images ── */
img, video, canvas, svg {
max-width: 100%;
height: auto;
display: block;
}
/* ── 5. Fluid container ── */
.container {
width: min(100% - 2rem, 1200px);
margin-inline: auto;
}
/* ── 6. Touch targets ── */
button, a { min-height: 44px; min-width: 44px; }
/* ── 7. Focus ring ── */
:focus-visible { outline: 3px solid currentColor; outline-offset: 2px; }
/* ── 8. Reduced motion ── */
@media (prefers-reduced-motion: reduce) {
* { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
Mobile-First CSS Checklist
| Item | How |
|---|---|
| Viewport meta tag | <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| Box sizing reset | *, *::before, *::after { box-sizing: border-box } |
| No horizontal overflow | html, body { overflow-x: hidden } |
| Responsive images | img { max-width: 100%; height: auto } |
| Fluid typography | font-size: clamp(1rem, 2vw + .5rem, 1.5rem) |
| Touch targets | min-height: 44px on all interactive elements |
| Base styles first | All default CSS without @media targets mobile |
| min-width queries | Use @media (min-width: …) — never max-width |
| Hover only on mouse | @media (hover: hover) and (pointer: fine) { :hover rules }
|
| Reduced motion | @media (prefers-reduced-motion: reduce) disables animations
|
| Focus visible | :focus-visible { outline: 3px solid … } |
| Full-screen height | min-height: 100dvh — not 100vh |
Try It Yourself
A complete mobile-first page layout — resize the preview pane to see the responsive patterns in action:
Quick Check — Chapter 41
The problem: when a browser renders at a virtual 980px and scales it to a 375px phone screen, all your
@media (max-width: 640px) rules are evaluated against the virtual
980px — so the browser never enters your mobile breakpoints.The viewport meta tag tells the browser: "Do not use a virtual viewport — use the real device width."
<meta name="viewport" content="width=device-width, initial-scale=1.0">Now a 375px phone renders at 375px, and your
@media (max-width: 640px) correctly activates. Without this tag, all
your responsive CSS is effectively ignored on mobile.
1. Hidden checkbox — A
<input type="checkbox" id="menu-toggle"> is visually hidden with
display: none. It maintains its checked state in the
browser.2. Label as button — A
<label for="menu-toggle"> styled as the hamburger icon. Clicking a
label toggles its associated checkbox automatically — no JavaScript needed.3. :checked + sibling selector — When the checkbox is checked,
#menu-toggle:checked ~ .nav-links { display: flex } reveals the
navigation menu using the general sibling combinator ~.The HTML structure must have the checkbox as a sibling of (or ancestor sibling of) the nav-links for the
~ selector to work. When checked: menu is visible. When unchecked:
menu is hidden. The browser handles the toggle state natively.
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr))Breaking this down:
—
auto-fill — create as many columns as will fit in the available
space—
minmax(280px, 1fr) — each column must be at least 280px wide, but
can grow to fill available space equallyThe browser does the math automatically:
— 320px container: 280px fits once → 1 column
— 640px container: 280px fits twice → 2 columns (each stretches to 320px)
— 960px container: 280px fits 3 times → 3 columns
— 1200px container: 280px fits 4 times → 4 columns
The layout adapts fluidly at every viewport size with zero media queries. This is the preferred approach when you don't need to control the exact column count — the content itself drives the layout. Use explicit breakpoints when you need "exactly 3 columns on desktop" regardless of content.
The solution is to only apply hover styles on devices that have a real pointing device (mouse, trackpad):
@media (hover: hover) and (pointer: fine) { .btn:hover { ... } }—
hover: hover — the primary input mechanism supports hover—
pointer: fine — the pointer is precise (mouse/trackpad, not
finger)Together these target only actual mouse/trackpad users. Touch devices get no hover styles, so there's no "stuck hover" effect. This is an important UX improvement particularly for navigation links, cards, and buttons.
The recommended minimum size is 44×44px — both Apple's Human Interface Guidelines and Google's Material Design specify this value. WCAG 2.5.8 (Level AA in WCAG 2.2) recommends a minimum of 24×24px but 44×44px is a better practice.
Why 44px? An average adult fingertip is approximately 44px wide on screen at standard DPI. Elements smaller than this are difficult to tap accurately and cause mis-taps on adjacent elements.
In CSS:
button, a { min-height: 44px; min-width: 44px; }For small icons (like social icons), you can make the visual smaller but increase the tap area with padding:
padding: 12px on a 20px icon creates a 44px tap area. Or
use ::before { content: ''; position: absolute; inset: -10px; } to
extend the hit area invisibly.
width: min(100% - 2rem, 1200px) do?
- Mobile-first strategy: write base CSS for mobile (no
@media), then add complexity withmin-widthqueries. Mobile downloads minimal CSS; desktop gets everything. - Viewport meta tag:
<meta name="viewport" content="width=device-width, initial-scale=1.0">— required for media queries to work on mobile. Never adduser-scalable=no. - Progressive enhancement layers: 1 (base) → 2 (640px) → 3 (768px) → 4 (1024px) → 5 (1280px). Each layer adds complexity, never overrides down.
- Responsive navigation:
display: noneon mobile →display: flexat 768px+. CSS-only hamburger: hidden checkbox + label +:checked ~ .nav-links. - Auto-fill cards:
repeat(auto-fill, minmax(280px, 1fr))creates a responsive grid with zero media queries. - Fluid typography:
clamp()for smooth scaling. Setmax-width: 65chfor readable paragraphs. - Responsive images:
img { max-width: 100%; height: auto }as global reset. Useaspect-ratiofor consistent proportions. - Touch targets: minimum 44×44px on all interactive elements. Hover styles inside
@media (hover: hover) and (pointer: fine). - Fluid container:
width: min(100% - 2rem, 1200px); margin-inline: auto— responsive with one line.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.