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.
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.
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.
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
| Tool | What 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 + @container | a component that adapts to its box |
--bg + var(--bg) | name each colour once (design tokens) |
[data-theme="dark"] + a small script | switch 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.
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.
