Frontend Master

JavaScript Curriculum

Responsive Design
+65 XP

Responsive Design

medium
~35 min·65 XP

The coffee shop website needs to work on phones too. Mobile customers finding the shop on Google see a broken desktop layout. Let's make it genuinely responsive — mobile first.

Responsive Design

A responsive website adapts its layout, typography, and spacing to any screen size. It's not about making a desktop site "work" on mobile — it's about designing for the smallest screen first, then progressively enhancing for larger ones.

The Viewport Meta Tag — Non-Negotiable

Before any CSS, put this in every <head>:

html
<meta name="viewport" content="width=device-width, initial-scale=1.0">

Without it, mobile browsers render your page at ~980px and zoom out, making everything tiny. This one tag tells the browser: "use the actual device width."

Media Queries

Media queries apply CSS only when certain conditions are true — most commonly, when the viewport is wider than a threshold:

media-queries.css
Query
/* base styles — mobile first */

No media query needed. Write base styles for mobile first.

Layout at Mobile
header (stacked)
main (full width)
footer
💡Mobile-first means min-width, not max-width
Mobile-first means writing base styles for mobile (no query), then adding min-width queries to enhance for larger screens. Desktop-first uses max-width queries to scale down. Mobile-first produces smaller, cleaner CSS — less to override, better performance on slow mobile connections.

Building Mobile-First — Step by Step

Watch how a layout evolves from mobile base to full desktop:

mobile-first.css

Start with mobile. Single column, stacked nav, sidebar hidden. No media query needed — these are the defaults.

Viewport: 375px
375px viewport
Menu
About
Contact
card 1
card 2
card 3
Cumulative CSS (all steps so far)
.nav {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.cards {
  grid-template-columns: 1fr;
}
.sidebar { display: none; }
ℹ️Only override what changes
The power of mobile-first: at each breakpoint, you only write the CSS that changes. All mobile styles apply by default at every size — you're only adding, never fighting against a complex base.

Fluid Units — Scale Without Media Queries

Some things should scale automatically with the viewport or user preferences — no media queries needed:

fluid-units.css
✓ Use this1rem = root font-size (16px)

Relative to root font-size (usually 16px). Scales with user browser font preferences. Best for font-size and spacing.

Computed value20px at default root
font-size: 1.25rem; /* 20px at default, scales with user prefs */
⚠️Never use px for font-size
Users can set their browser base font size for accessibility reasons (large text, low vision). If you use px for font-size, their settings are ignored. Always use rem for font-size — it respects user preferences and scales correctly.

Your Challenge

Rebuild the coffee shop page mobile-first with two media query breakpoints and fluid units throughout.

Challenge

Start from a single-column mobile layout. Add a @media (min-width: 768px) query to switch to a 2-column card grid. Add a @media (min-width: 1024px) query to unlock the sidebar. Use rem for font sizes throughout and clamp() for the hero heading.

responsivemedia-queriesmobile-firstmin-widthbreakpointsviewportrememvwvhclampfluid-typographymin-contentmax-content
Responsive Design | Nexus Learn