Lesson 23 of 29 · Grid & Responsive Design
Responsive Web Design: Media Queries and the Viewport
People visit your pages on phones, tablets, laptops and huge monitors. Responsive web design means building one page that adapts its layout to every screen, instead of a separate mobile site. The main tools are the viewport meta tag, flexible widths and CSS media queries.
The viewport meta tag
By default, phones pretend to be about 980px wide and zoom out, so desktop pages still fit on the screen, just tiny. This line in <head> tells the browser to use the real width of the device instead:
<meta name="viewport" content="width=device-width, initial-scale=1">
Every responsive page needs it. Without it, your styles for small screens never kick in on a real phone.
Flexible widths
A fixed width: 960px forces phone users to scroll sideways. Use max-width instead: the element is at most that wide, but it shrinks on smaller screens. Add margin: 0 auto to centre it, because auto side margins share the leftover space equally:
.container {
max-width: 960px;
margin: 0 auto; /* 0 top and bottom, auto left and right */
padding: 0 16px; /* breathing room on phones */
}
Relative units help too. % is relative to the parent's width, rem to the root font size (usually 16px, so 2rem is 32px), and vw is 1% of the viewport width. Font sizes in rem stay consistent across the page and respect the reader's browser font settings.
Media queries
A media query applies CSS only when a condition is true, for example when the screen is narrow:
.layout {
display: grid;
grid-template-columns: 2fr 1fr;
}
@media (max-width: 600px) {
.layout {
grid-template-columns: 1fr; /* one column on phones */
}
}
max-width: 600pxmatches screens 600px wide or narrower.min-width: 768pxmatches screens 768px wide or wider.
The width at which the layout changes is called a breakpoint. Pick breakpoints where your design starts to look cramped, not for specific devices.
Mobile first
Many developers write the phone layout first, as the default, and add min-width queries for bigger screens:
.layout { display: grid; gap: 24px; } /* phones: one column */
@media (min-width: 768px) {
.layout { grid-template-columns: 2fr 1fr; } /* tablets and up */
}
Both directions work. Mobile-first CSS often stays simpler: small screens get the simple layout, and big screens get the additions.
Common mistakes
- Forgetting the viewport meta tag. The page looks right in a narrow desktop window but stays zoomed out on a real phone.
- Putting the media query before the normal rule. A media query doesn't make a rule stronger. If the normal rule comes later in the file, it wins. Write media queries after the rules they change.
- Using
widthinstead ofmax-width. A fixed width can't shrink, so the page scrolls sideways on phones.
Assignment
Make the realm news page responsive:
- Add the viewport meta tag to
<head>, withcontent="width=device-width, initial-scale=1". - Give
.containeramax-widthof800pxand centre it withmargin: 0 auto. - Make
.layouta grid with two columns,2fr 1fr, and a24pxgap, so the article and the sidebar sit side by side. - Set the
h1font size to2.5rem. - Add a media query for screens 600px wide or narrower,
@media (max-width: 600px), that switches.layoutto one column (1fr) and sets theh1to1.75rem.
The tests check your page at several widths: 1024px, 700px, 600px and 500px.
Premium lesson
Unlock the full Learn HTML & CSS course
- All 29 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.
Lesson last updated on .