Responsive design and media queries
Make one site fit every screen with the viewport tag, flexible units, media queries, breakpoints and testing tools.
In this lesson you will
- Explain responsive design
- Write media queries
- Choose breakpoints sensibly
- Test with responsive mode
About 16 minutes
One site, every screen
A visitor opens your site on a 6-inch phone. Another on a 27-inch monitor. Another on a tablet held sideways. A fourth has zoomed the text to 200 percent. You will never know which, and you cannot build four separate sites. Responsive design is the practice of building one site that reshapes itself to suit whatever screen it finds. It is no longer optional: well over half of web visits come from phones, and search engines judge your site by its mobile version first.
Step 1: the viewport meta tag
<meta name="viewport" content="width=device-width, initial-scale=1">
Without this line, phones pretend to be about 980 pixels wide and shrink your page to fit, so everything looks tiny. With it, the phone reports its real width, which is the only way your responsive CSS can work at all. You added it in the HTML course. If your phone layout looks like a miniature desktop, this tag is missing.
Step 2: flexible foundations
Most of responsive design needs no special code, only flexible habits you have already learned:
- Use
%,fr,remandmax-widthinstead of fixed pixel widths. - Let images shrink with
img { max-width: 100%; height: auto; }. - Use
flex-wrapandauto-fitgrids so rows break by themselves. - Use
clamp()for headings that scale smoothly. - Do not fix heights on boxes with text.
A page built that way already survives most screens. Media queries finish the job where a layout must change, not just stretch.
Step 3: media queries
A media query applies CSS only when a condition is true, such as the screen being at least a certain width.
.cards { display: grid; grid-template-columns: 1fr; gap: 16px; }
@media (min-width: 700px) {
.cards { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 1000px) {
.cards { grid-template-columns: repeat(3, 1fr); }
}
Read it in plain words: “by default, one column. When the screen is at least 700 pixels wide, two columns. At 1000 pixels, three.” Inside the braces you write normal rules, and they apply only while the condition holds.
The boxes are stacked in one column. Add a media query so that when the preview is at least 400px wide the .row becomes a flex row. Try resizing the browser to see it switch.
<style>
.row div { background: #EFEAFD; padding: 18px; margin-bottom: 8px; border-radius: 8px; font-family: Arial; }
/* add a media query below */
</style>
<div class="row"><div>One</div><div>Two</div><div>Three</div></div>
@media (min-width: 400px) { .row { display: flex; gap: 8px; } .row div { flex: 1; margin: 0; } }
<style>
.row div { background: #EFEAFD; padding: 18px; margin-bottom: 8px; border-radius: 8px; font-family: Arial; }
@media (min-width: 400px) { .row { display: flex; gap: 8px; } .row div { flex: 1; margin-bottom: 0; } }
</style>
<div class="row"><div>One</div><div>Two</div><div>Three</div></div>
The preview frame in this editor is narrower than a full browser window, so it behaves like a small screen. Use your browser’s responsive mode, described below, to test full pages.
min-width and max-width
@media (min-width: 700px)means “700 pixels and wider”. Used in a mobile-first approach, the next lesson.@media (max-width: 699px)means “699 pixels and narrower”. Used in a desktop-first approach.- Combine them:
@media (min-width: 600px) and (max-width: 900px)targets tablets only.
Choosing breakpoints
A breakpoint is the width at which your layout changes. Beginners copy the sizes of popular devices, but devices change every year. A better method: start with a small window and slowly widen it. The moment the design looks stretched or awkward, that is your breakpoint. In practice, a few common values work for most sites.
/* small phones: the default, no query */
@media (min-width: 600px) { /* large phones, small tablets */ }
@media (min-width: 900px) { /* tablets and small laptops */ }
@media (min-width: 1200px) { /* desktops */ }
Other things you can test
@media (orientation: landscape) { ... }
@media (prefers-color-scheme: dark) { body { background: #14112E; color: #F4F1FC; } }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
@media (hover: hover) { .card:hover { transform: translateY(-4px); } }
@media print { nav, footer { display: none; } }
These are some of the most useful. prefers-color-scheme adapts to dark mode. prefers-reduced-motion respects people who find animation uncomfortable. hover: hover limits hover effects to devices that have a real pointer. And print styles tidy a page for paper.
Testing responsive pages
- Open your page in Chrome or Edge and press F12.
- Click the device toolbar icon, or press Ctrl+Shift+M, to open responsive mode.
- Drag the width handle slowly from narrow to wide and watch for awkward moments.
- Pick a few device presets and rotate them.
- Finish on a real phone. Emulators are close but not perfect, especially for touch and keyboards.
Touch-friendly details
- Make tap targets at least about 44 by 44 pixels, with space between them.
- Keep body text at 16 pixels or more. Smaller text makes phones zoom when you tap a form field.
- Do not depend on hover for anything important.
- Avoid horizontal scrolling. If a table or code block is wide, let that one element scroll inside its own container with
overflow-x: auto.
Writing media queries in a random order. Because later rules win when specificity is equal, write them from smallest to largest for min-width, so each larger query refines the previous one.
Add a dark mode: when the visitor prefers a dark colour scheme, make the background dark and the text light.
<style>
body { background: #FAF8FF; color: #14112E; font-family: Arial; padding: 20px; }
/* dark mode here */
</style>
<h2>Hello</h2>
<p>This page can adapt to dark mode.</p>
@media (prefers-color-scheme: dark) { body { background: #14112E; color: #F4F1FC; } }
<style>
body { background: #FAF8FF; color: #14112E; font-family: Arial; padding: 20px; }
@media (prefers-color-scheme: dark) { body { background: #14112E; color: #F4F1FC; } }
</style>
<h2>Hello</h2>
<p>This page can adapt to dark mode.</p>
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
What does the viewport meta tag fix?
- Colours
- Phones showing a shrunken desktop layout
- Fonts
- Image size
It makes the page use the device’s real width.
What does @media (min-width: 700px) mean?
- Screens up to 700px
- Screens 700px and wider
- Exactly 700px
- Printers
min-width applies from that width upwards.
What is the best way to find a breakpoint?
- Copy a popular phone size
- Widen a small window until the design looks awkward
- Pick a random number
- Use only 1000px
Let the content decide.
Which query adapts to dark mode?
@media (dark)@media (prefers-color-scheme: dark)@media (night)@media (theme: dark)
prefers-color-scheme reads the visitor’s setting.
Which text size avoids the zoom-on-tap on phones for inputs?
- 10px
- 12px
- 16px or more
- 8px
Inputs under 16px make some phones zoom.
Summary
- Always include the viewport meta tag.
- Flexible units and wrapping layouts do most of the work.
- Media queries change layouts at breakpoints.
- Test in responsive mode and on a real phone.
Questions
How many breakpoints do I need?
Often two or three. Let your design show you where it breaks.
Do media queries slow the page?
No. The browser evaluates them quickly.
What about container queries?
They let a component respond to its container's width instead of the screen's. They are covered in the advanced lessons.
