Home Module 07 Mobile-First Design

1. Introduction

Mobile-First is both a design philosophy and a CSS coding strategy. The idea is simple:

"Start designing for the smallest screen. Then progressively add features and complexity for larger screens."

The alternative — Desktop-First — starts with a full desktop design and then strips things away for mobile. Desktop-First leads to:

  • More CSS overrides (and therefore more bugs)
  • Slower mobile performance (downloading desktop assets then hiding them)
  • A worse mobile experience because it was an afterthought

Since 60%+ of web traffic is mobile, mobile should be the primary concern.

2. Theory

Mobile-First CSS: min-width Queries

In mobile-first CSS, the default styles (no media query) are for the smallest screen. You then use min-width queries to add styles for larger screens.

/* Mobile-First Example */

/* Base (no query): mobile styles */
.container { padding: 16px; }
.grid { grid-template-columns: 1fr; }
.nav-links { display: none; }

/* Tablet (768px+): enhance */
@media (min-width: 768px) {
  .container { padding: 24px; }
  .grid { grid-template-columns: repeat(2, 1fr); }
  .nav-links { display: flex; }
}

/* Desktop (1024px+): enhance further */
@media (min-width: 1024px) {
  .container { padding: 32px; }
  .grid { grid-template-columns: repeat(3, 1fr); }
}

Desktop-First (What to Avoid)

/* Desktop-First — NOT recommended */

/* Base: desktop styles */
.grid { grid-template-columns: repeat(3, 1fr); }
.nav-links { display: flex; }

/* Override for tablet */
@media (max-width: 1023px) {
  .grid { grid-template-columns: repeat(2, 1fr); }
}

/* Override for mobile */
@media (max-width: 767px) {
  .grid { grid-template-columns: 1fr; }
  .nav-links { display: none; }
}
/* You're writing 3x as much CSS to undo things */

Progressive Enhancement

Mobile-first is part of the broader Progressive Enhancement philosophy:

  1. Base layer: Essential content and functionality for all devices
  2. Enhanced layer: Better layout and styling for capable devices
  3. Optional layer: Advanced features for the most capable devices

Mobile UX Principles

  • Touch targets ≥ 44×44px — Apple's guideline; fingers need room to tap
  • Single-column layouts — reading in a column is natural on narrow screens
  • Larger base font size — 16px minimum (prevents browser zoom)
  • No hover-dependent features — touch devices don't have hover
  • Fast loading — mobile connections are slower; optimize images
  • Avoid text that requires horizontal scrolling

The Hamburger Menu Pattern

/* Mobile: hamburger visible, links hidden */
.nav-toggle { display: block; }
.nav-links  { display: none; }

/* Open state (added by JavaScript) */
.nav-links.is-open { display: flex; flex-direction: column; }

/* Tablet+: show links, hide hamburger */
@media (min-width: 768px) {
  .nav-toggle { display: none; }
  .nav-links  { display: flex; flex-direction: row; }
}

Fluid vs Adaptive vs Responsive

ApproachHow it works
FluidUses only percentages — always scales smoothly between any sizes
AdaptiveFixed designs that switch between a few specific sizes at breakpoints
ResponsiveCombines fluid layouts with breakpoints — the modern approach

3. Real World Example

Twitter (now X) is a great mobile-first example:

  • On mobile: single column, bottom tab bar, large touch targets
  • On tablet: sidebar appears on the left, main feed in centre
  • On desktop: three columns (sidebar, feed, trends/widgets)

The mobile experience is just as rich as desktop — it is not stripped down. The desktop version is an enhanced version of the mobile layout, not a completely different design.

4. Code Example — Mobile-First Page

/* ===================================
   MOBILE-FIRST COMPLETE EXAMPLE
   =================================== */

/* --- RESET --- */
*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; padding: 0; }
img { max-width: 100%; height: auto; display: block; }
input, button, textarea, select { font: inherit; }

/* --- VARIABLES --- */
:root {
  --header-h: 56px;
  --sidebar-w: 240px;
  --max-w: 1200px;
}

/* --- BASE TYPOGRAPHY (mobile) --- */
body {
  font-family: system-ui, sans-serif;
  font-size: 1rem;
  line-height: 1.6;
  color: #1e293b;
}

/* --- HEADER (mobile) --- */
.header {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--header-h);
  background: #1e293b;
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 100;
}

.header__logo { font-size: 1.125rem; font-weight: 700; }
.header__hamburger { background: none; border: none; color: white; font-size: 1.5rem; cursor: pointer; }
.header__nav { display: none; } /* hidden on mobile */

/* --- MOBILE NAV DROPDOWN --- */
.header__nav.is-open {
  display: flex;
  flex-direction: column;
  position: fixed;
  top: var(--header-h);
  left: 0; right: 0;
  background: #0f172a;
  padding: 16px;
  gap: 4px;
  z-index: 99;
}

/* --- MAIN CONTENT (mobile) --- */
.page-body {
  padding-top: var(--header-h);
  min-height: 100vh;
}

.container {
  width: 100%;
  padding: 16px;
}

/* --- HERO (mobile) --- */
.hero {
  padding: 48px 0;
  text-align: center;
}

.hero h1 { font-size: 1.75rem; margin-bottom: 16px; }
.hero p  { font-size: 1rem; margin-bottom: 24px; }

/* --- CARD GRID (mobile: 1 column) --- */
.card-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
  margin-top: 24px;
}

/* --- TABLET (768px+) --- */
@media (min-width: 768px) {
  :root { --header-h: 64px; }

  .container { padding: 24px; max-width: var(--max-w); margin: 0 auto; }
  .header__hamburger { display: none; }
  .header__nav {
    display: flex !important; /* always visible on tablet+ */
    flex-direction: row;
    align-items: center;
    position: static;
    background: none;
    padding: 0;
    gap: 4px;
  }

  .hero { padding: 80px 0; }
  .hero h1 { font-size: 2.5rem; }
  .hero p  { font-size: 1.125rem; }

  .card-grid { grid-template-columns: repeat(2, 1fr); gap: 24px; }
}

/* --- DESKTOP (1024px+) --- */
@media (min-width: 1024px) {
  .card-grid { grid-template-columns: repeat(3, 1fr); }
  .hero h1 { font-size: 3.5rem; }
}

/* --- WIDE (1280px+) --- */
@media (min-width: 1280px) {
  .card-grid { grid-template-columns: repeat(4, 1fr); gap: 32px; }
}

5. Code Breakdown

grid-template-columns: 1fr as default
Mobile base: single column. Then each breakpoint adds more columns: 2 at tablet, 3 at laptop, 4 at wide. Each query enhances rather than overrides.
display: none on hamburger at tablet+
The hamburger button is shown by default (mobile) and hidden at tablet+. The nav is hidden by default and shown at tablet+. Mobile-first: start simple, enhance for larger. This is less CSS than the desktop-first equivalent.
!important on .header__nav at tablet+
The JavaScript might add display: none inline when the hamburger menu is closed. The !important overrides that inline style on tablet+, ensuring the nav is always visible regardless of JS state. A legitimate use of !important.
CSS variable for --header-h
Defining the header height as a variable means it can be changed at any breakpoint (56px on mobile, 64px on tablet+) and everywhere that uses it (padding-top: var(--header-h)) updates automatically.

6. Common Mistakes

  • Starting with desktop-first and switching to mobile-first mid-project. Very painful. Decide upfront and commit.
  • Touch targets that are too small. Text links and icon-only buttons on mobile must be at least 44×44px. Add padding: 12px to small links.
  • Using hover-only features on mobile. Dropdown menus that only open on hover don't work on touch screens. Use click/tap toggles instead.
  • Not testing at 320px. This is the smallest common phone width. Many designers forget about it. Test at 320px to catch overflow issues early.
  • Horizontal overflow. A single element wider than the viewport causes the entire page to scroll horizontally. Common culprits: min-width on containers, images without max-width: 100%.

7. Best Practices

  • Always code mobile-first — base styles for mobile, enhance with min-width queries.
  • Make all interactive elements at least 44px tall on mobile.
  • Test at 320px as your minimum supported width.
  • Avoid hover-only interactions — always provide a tap/click alternative.
  • Lazy-load images on mobile — use loading="lazy" on all below-fold images.
  • Add font-size: 16px minimum on form inputs — iOS zooms in on inputs with font-size below 16px.
  • Use overflow-x: hidden on body as a safety net against horizontal overflow.

8. Practice Exercise

  1. Take a desktop design you have already built. Rewrite the CSS mobile-first (no styles, then add structure as viewport grows).
  2. Build a navigation bar: hamburger + hidden links on mobile, full horizontal links on tablet+. Use only CSS (no JavaScript for the show/hide).
  3. Test every page you have built at 320px in DevTools. Fix any horizontal overflow issues.
  4. Ensure all buttons and links have at least min-height: 44px on mobile.

9. Assignment

Build a complete mobile-first landing page for a fictional app:

  • Code entirely mobile-first — no desktop-first overrides
  • Mobile (320px–767px): single column, hamburger nav, stacked hero
  • Tablet (768px+): horizontal nav, 2-column features
  • Desktop (1024px+): 3-column features, larger hero text
  • All buttons minimum 44px height on mobile
  • No horizontal overflow at any width
  • Images use max-width: 100% and loading="lazy"

10. Interview Questions

Q1: What is mobile-first design?

Answer: Mobile-first is a design and development strategy where you start with the mobile experience as the default and progressively enhance it for larger screens. In CSS, this means writing base styles for mobile and using min-width media queries to add styles for tablets and desktops. It results in cleaner CSS, better performance, and a better mobile UX.

Q2: What is the difference between mobile-first and desktop-first CSS?

Answer: Mobile-first uses min-width queries: default styles are for mobile, queries enhance for larger. Desktop-first uses max-width queries: default styles are for desktop, queries strip features for smaller screens. Mobile-first is preferred because it requires less CSS overriding and aligns with the fact that most web traffic is mobile.

Q3: Why should touch targets be at least 44×44px?

Answer: This is Apple's and Google's guideline based on the average size of a fingertip. If a button or link is smaller, users will frequently miss it or hit adjacent elements. Ensuring minimum touch target size significantly improves usability on mobile and is part of WCAG accessibility guidelines.

Q4: What is progressive enhancement?

Answer: Progressive enhancement is a development strategy where you first build a baseline experience that works for all users and devices, then add enhancements (better layout, animations, advanced features) for devices that support them. Mobile-first CSS is one application of progressive enhancement.

Q5: How do you debug horizontal overflow on mobile?

Answer: Add * { outline: 1px solid red; } in DevTools to see all element boundaries. Or add * { max-width: 100vw; overflow: hidden; } temporarily. Common causes: an element with a fixed width wider than the viewport, an image without max-width: 100%, a min-width value on a container, or text with very long words.

11. Additional Resources

  • Luke Wroblewski — Mobile First (the original book — search "luke wroblewski mobile first")
  • web.dev — Mobile-first design (search "web.dev mobile first")
  • Google — Touch target size guidelines (search "google touch target size")
  • MDN — Responsive design (search "MDN responsive design")
  • BrowserStack — Cross-device testing (browserstack.com)