Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

React
  • One Request, Two Answers: the Same Post as an HTML Page and as JSON
  • A Blog Design from a Prompt: Downloading Figma Make Code and Reading App.tsx and BlogCard.tsx
  • How a Web Page Works: HTML Tags, Elements and Attributes
  • Semantic HTML: header, nav, main, aside and footer as Page Landmarks
  • HTML Forms: Labels, Names, Input Types and Where the Data Goes (GET vs POST)
  • CSS Basics: Selectors, Which Rule Wins, and the Box Model
  • CSS Flexbox vs Grid: A Nav Bar, a Card Row and a Dashboard
  • Responsive CSS: Viewport, Mobile-First Media Queries, clamp() and Dark Mode with Tokens
  • JavaScript for People Who Already Code: Syntax, Modules and Five Surprises
  • JavaScript Arrays the Functional Way: map, filter, reduce, find, sort and Object.entries
  • Async JavaScript: Promises, async/await, fetch and Promise.all
  • JavaScript in the Page: the DOM, querySelector and Events
  • npm, package.json and Vite: What npm install Fetches and How Vite Serves and Builds Your Page
  • TypeScript Essentials: Types Checked, Then Erased (Interfaces, Unions, Generics)
  • Why React, JSX and Components: The Page as a Function of Data
  • React Props and Composition: Data Down, Calls Up, Children in the Slot
  • Routing With React Router: One Page, Many Addresses
  • React Component Tests With Vitest and Testing Library: Test What the User Sees
  • End-to-End Tests for a React App With Playwright: A Full Dress Rehearsal
  • Why AI-Built Websites All Look the Same, and Four Levels That Fix It
  • Visual Hierarchy, Spacing and the 8-Point Grid: Tell the Eye What to Read First
  • Colour, Contrast and Dark Palettes: 60-30-10, Semantic Tokens and Ratios You Can Compute
  • Typography That Sells: Type Scale, Line Length and Free Font Pairings
  • The UI Element Catalogue: Buttons to Toasts, Their Jobs, States and shadcn/ui Names
  • Design Tokens: Primitive and Semantic Tokens, from Figma to CSS to Tailwind
  • Tailwind CSS 4 in React: Utility Classes, Responsive Prefixes, Hover and Dark Mode
  • shadcn/ui in React: Components You Own, Edit and Commit
  • Design Tokens to a Tailwind Theme: Light and Dark Mode with @theme inline
  • Motion in React: Entrance, Hover, Exit and Layout Animations
  • A React Dashboard: Stat Cards, a Recharts Line Chart and a Sortable Table
  • Accessibility in Practice: Dialogs, Menus and the Keyboard in React
  • Reference-Driven Design: Extract a DESIGN.md From a Site You Admire and Build From It
  • Components, Variants and Variables: One Design System in Figma and in CSS
  • Figma MCP Server: Build a React Component From a Frame, Then Write It Back
  • Claude Design: From a Brief to a Clickable Prototype, Then Hand It Off to Claude Code
  • Critiquing AI-Generated UI: Six Checks for Every Screen
  • The AI Design Landscape: Prompt to UI, Prompt to App, and Design to Code
  • Your Brand Rules Folder: One Folder Every AI-Built Page Follows
  • The Best-Practice UI Prompt: Plain Sentences That Beat the Average AI Page
  • UI Ideas From Figma Make, Google Stitch and ChatGPT: One Prompt, Three Designs
  • Google Stitch From a Sketch or Screenshot: Show the Layout, Say What to Keep
  • Stitch to Figma to Code: Turn a Stitch Screen Into Code That Uses Your Tokens
  • Figma Make: Prompt to Prototype With a Four-Part Prompt
  • Upgrade a Dated UI From One Screenshot (or a Reference Image)
  • States and Feedback: Make Every Click Get an Answer
  • A Design-Loop Skill: Claude Screenshots, Scores and Fixes Its Own UI
  • Copy Without the AI Tells: A Copy Pass on a Landing Page
  • React State, Events, Lists and Conditional Rendering: Follow One Click
  • React useEffect: When It Runs, What Cleanup Is For, and When You Don't Need One
  • Lifting State Up, Refs and React DevTools: Put State Where It Is Needed
  • Forms and Actions in React 19: Controlled Inputs, Form Actions, useActionState and useFormStatus
  • React Context and Custom Hooks: Share One Value, Reuse the Logic
  • Server State With TanStack Query: One Key, One Request, One Cache
  • Mutations and Optimistic Updates in TanStack Query: Change Data, Then Tell the Cache
  • Generate TypeScript Types, Query Hooks and Zod From an OpenAPI Spec With Orval
  • A Mock Backend in the Browser With Mock Service Worker, Generated From the Spec
  • A React Client for a FastAPI API: The CORS Wall, CORSMiddleware and the Vite Proxy
  • Forms With React Hook Form and Zod: The API's Rules in the Browser, the Server's Errors Under the Field
  • JWT Auth in a React Client With a FastAPI Backend: Log In, Carry the Token, Let the Server Decide
  1. Blog
  2. /
  3. React
  4. /
  5. Fundamentals

Responsive CSS: Viewport, Mobile-First Media Queries, clamp() and Dark Mode with Tokens

One page that fits a phone and a wide screen, and a dark mode with one small button. The viewport line, mobile-first min-width queries, clamp() for fluid headings, container queries, and colour tokens switched by data-theme.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Fundamentals

What you will learn

  • Add the viewport meta line and explain what breaks without it
  • Write mobile-first CSS with min-width media queries
  • Size headings with clamp() so text grows with the window
  • Build light and dark themes from CSS custom properties and a data-theme switch

Before you start

  • CSS selectors and the box model
  • Flexbox and Grid

Pour the same water into a tall glass and then into a wide bowl. You never change the water; it just takes the shape of what holds it. Your page is the water: the same HTML and the same content. The screen is the glass: a phone, a tablet or a laptop. Responsive design means one page that fits every screen, and CSS decides how.

One line that tells the phone its real width

Before any CSS, every page needs this line in its head:

  <!-- viewport: lay the page out at the phone's real width -->
  <meta name="viewport" content="width=device-width, initial-scale=1">

width=device-width means "lay the page out at the phone's own width", and initial-scale=1 means no zoom. With it, the task board is 375 pixels wide on the phone: one column, menu on top.

Delete that line and the phone pretends to be 980 pixels wide. It draws the wide desktop layout and shrinks everything until it's unreadable. Your media queries never see the phone at all.

Mobile first, then widen

/* mobile first: the base rules are the phone layout */
body {
  margin: 0;
  font-family: system-ui, sans-serif;
  background: #F1F5F9;
}
.page {
  display: grid;
  gap: 16px;
  padding: 16px;
}
.menu {
  display: flex;
  gap: 16px;
  padding: 12px 16px;
  background: #1E3A8A;
  border-radius: 8px;
}
.menu a { color: white; }
.tasks { display: grid; gap: 12px; }
.task { background: white; border-radius: 8px; padding: 2px 16px; }
/* 700px and wider: the menu becomes a sidebar */
@media (min-width: 700px) {
  .page { grid-template-columns: 200px 1fr; }
  .menu { flex-direction: column; }
}
/* 1000px and wider: two columns of tasks */
@media (min-width: 1000px) {
  .tasks { grid-template-columns: 1fr 1fr; }
}

The base rules at the top have no media query at all. That's the phone layout, one column with the menu on top, and every screen starts there. From 700 pixels and wider, the first @media (min-width: 700px) turns the menu into a 200px sidebar. From 1000 pixels, the second query puts the tasks into two columns. The 700 rules still apply at 1000; the new query only adds to them.

Three widths of the same page. At 420 pixels: menu on top, one column of tasks. At 800 pixels: sidebar plus one column. At 1100 pixels: sidebar plus two columns of tasks. 420px: base rules 800px: + min-width 700 1100px: + min-width 1000 menu menu menu
Start with the small screen, then add rules as the glass gets wider. That is mobile first.

Text that grows with the window

Layout isn't the only thing that can adapt. Here's a small order page:

/* clamp(smallest, preferred, largest) */
h1 {
  margin: 0 0 8px;
  line-height: 1.15;
  /* grows with the window (vw), never below 1.5rem, never above 3rem */
  font-size: clamp(1.5rem, 1rem + 2.5vw, 3rem);
}
p {
  margin: 0;
  line-height: 1.5;
  font-size: clamp(1rem, 0.9rem + 0.4vw, 1.25rem);
}

The pattern is clamp(smallest, preferred, largest). The browser uses the middle value but never goes past the two limits. For the heading, the preferred size is 1rem + 2.5vw, where vw is one percent of the window width, so the heading grows as the window grows. It's never below 1.5rem and never above 3rem. Measured in the browser, the heading is 26.5 pixels at 420 wide and 43.5 pixels at 1100 wide.

Compare the file's break: font-size: 6vw on its own is tiny on a phone and huge on a big screen. clamp() is what keeps fluid text sensible.

Container queries: ask the box, not the screen

Sometimes the same card sits in a narrow sidebar on one page and a wide column on another. A media query asks the screen; a container query asks the box the card sits in:

/* a container: its children may ask how wide it is */
.slot { container-type: inline-size; }
.card {
  display: grid;
  gap: 12px;
  padding: 12px;
  background: white;
  border-radius: 8px;
}
.photo { display: grid; place-items: center; height: 72px;
         background: #BFDBFE; border-radius: 8px; font-weight: 700; }
.card h2 { margin: 0; font-size: 18px; }
/* the card asks its container, not the screen */
@container (min-width: 400px) {
  .card { grid-template-columns: 96px 1fr; align-items: center; }
}

With the same student card placed twice, the copy in the 260px slot stays stacked, and the copy in the wide slot puts the photo beside the text. Delete the container-type line and there's no container to ask, so both cards stay stacked. This is handy for cards that live in many places; for most pages, media queries are all you need.

Colours from a paint chart

A painter doesn't write a colour code on every wall. Each wall says "use the wall colour", and the paint chart says what that colour is. Change the swatch once, from chalk white to night blue, and every wall changes. In CSS these swatches are custom properties such as --bg, often called design tokens.

/* design tokens: every colour gets a name, once */
:root {
  --bg: #F8FAFC;
  --surface: #FFFFFF;
  --text: #0F172A;
  --muted: #64748B;
  --accent: #2563EB;
  color-scheme: light;
}
/* dark mode: the same names, new values */
[data-theme="dark"] {
  --bg: #0F172A;
  --surface: #1E293B;
  --text: #E2E8F0;
  --muted: #94A3B8;
  --accent: #60A5FA;
  color-scheme: dark;
}
/* the components only ever use the names */
body { background: var(--bg); color: var(--text); }
.card { background: var(--surface); }
.card p { color: var(--muted); }
.status { color: var(--accent); }
button { background: var(--accent); color: var(--surface); }
/* BREAK IT: in .card write color: #0F172A instead of a token - in dark mode the title is dark on dark */

On :root, every colour gets a name, once. The [data-theme="dark"] block uses the same names with new values, and that's the whole dark mode. Everything below only ever uses var(--name). A separate base.css holds the layout and not one colour.

Components use var(--bg), var(--surface) and var(--text). The html element's data-theme attribute picks which set of values those names get: light or dark. components body: var(--bg).card: var(--surface).status: var(--accent) :root (light) --bg: #F8FAFC [data-theme="dark"] --bg: #0F172A theme.js flips data-theme
The components never change. Only the value behind each name does.

A small button for dark mode

Something has to flip data-theme. That's a tiny script, theme.js, loaded at the end of the page:

// 1. start in the theme the computer is set to
const root = document.documentElement;
const systemDark = matchMedia('(prefers-color-scheme: dark)').matches;
root.dataset.theme = systemDark ? 'dark' : 'light';

// 2. each click on the button flips light and dark
const button = document.querySelector('#theme-toggle');
button.addEventListener('click', () => {
  root.dataset.theme = root.dataset.theme === 'dark' ? 'light' : 'dark';
});

First it starts in the theme the computer is set to: prefers-color-scheme: dark is the system setting, so the page opens dark on a dark computer. Then each click on the Switch theme button flips data-theme between dark and light, and the tokens do the rest.

The file's break shows why the tokens matter: write color: #0F172A straight into .card instead of a token, and in dark mode the title is dark text on a dark card. One hard-coded colour is enough to break a theme.

Quick recap

ToolWhat it does
<meta name="viewport" ...>lays the page out at the phone's real width
@media (min-width: 700px)adds rules from that width up (mobile first)
clamp(1.5rem, 1rem + 2.5vw, 3rem)text that grows with the window, within limits
container-type + @containera component that adapts to its box
--bg + var(--bg)name each colour once (design tokens)
[data-theme="dark"] + a small scriptswitch every token at once

Try it yourself

Take the task board, check it at 420 and 1100 pixels in DevTools' device toolbar, give the headings a clamp() size, move every colour into tokens, and add a dark mode with the small button. The script uses the DOM, which is explained properly in JavaScript in the page.

Tried the exercise? Share your answer or ask a question.

The StudyEasy AI Community is free to join, with the lesson files, notebooks and code.

Join free on Skool

Keep going in React · 8 of 59

Previous
CSS Flexbox vs Grid: A Nav Bar, a Card Row and a Dashboard
Next post
JavaScript for People Who Already Code: Syntax, Modules and Five Surprises

Related course on Udemy

The Python Architect Masterclass: Full Stack to AI Engineer

Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)

Related posts

One Request, Two Answers: the Same Post as an HTML Page and as JSON

Ask a Django blog for the same post twice: once as a 12,731-byte HTML page built by a template, once as 2,976 bytes of JSON built by a serializer. Compare the headers, see who builds the page, and why the server still owns every write.

A Blog Design from a Prompt: Downloading Figma Make Code and Reading App.tsx and BlogCard.tsx

Make a blog design from a prompt in Figma Make, download src.zip, unzip it with Python's zipfile module, and read the two files that matter: App.tsx with its selectedPost state and BlogCard.tsx with its props.

How a Web Page Works: HTML Tags, Elements and Attributes

A web page is a text file full of labelled pieces. Learn tags, attributes and nesting with three tiny pages, and see what breaks when an attribute or a closing tag is wrong.

Semantic HTML: header, nav, main, aside and footer as Page Landmarks

The same page written as div soup and with semantic tags looks identical, but only one gives screen readers and search engines landmarks and an outline. Plus tables, div, span and the one-main rule.

On this page

  • One line that tells the phone its real width
  • Mobile first, then widen
  • Text that grows with the window
  • Container queries: ask the box, not the screen
  • Colours from a paint chart
  • A small button for dark mode
  • Quick recap
  • Try it yourself
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

The Python Architect Masterclass: Full Stack to AI Engineer

Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.