Sub topic 2
Responsive foundations: the viewport tag and media queries
This chapter assumes you know basic HTML structure, flexbox, and how to write a CSS rule. Every navigation pattern later in this tutorial depends on two things covered here: the viewport meta tag and media queries.
Why the viewport tag matters
A phone browser does not assume your page was built for a small screen. If a page has no viewport declaration, mobile browsers pretend the screen is about 980 CSS pixels wide. They render the page at that width and then shrink it to fit. The result is tiny text and a desktop navigation bar squeezed into a thumbnail.
The viewport meta tag turns that behavior off:
<meta name="viewport" content="width=device-width, initial-scale=1">width=device-widthsets the layout width to the real width of the screen in CSS pixels. A typical phone is between 360 and 430 CSS pixels wide.initial-scale=1sets the first zoom level to 100%.
Put the tag inside <head>. Without it, none of the media queries below behave the way you expect on a phone, because the browser reports a width of about 980 pixels.
CSS pixels, not device pixels
A phone screen may have 1170 physical pixels across, but the browser reports about 390 CSS pixels. The ratio is the device pixel ratio. Media queries and px values use CSS pixels, so you can ignore the physical resolution when you choose breakpoints.
Media queries
A media query applies CSS only when a condition is true. The most useful condition for navigation is the viewport width:
@media (min-width: 48em) {
/* rules for viewports 768px wide and up */
}The 48em value is 48 times the browser's default font size of 16px, which equals 768px. Using em means the breakpoint also moves when a user raises their default text size. That is the behavior you want, because larger text needs more room for the same links.
Mobile first
There are two ways to write the queries:
| Approach | Base styles are for | Queries use | Typical result |
|---|---|---|---|
| Mobile first | Small screens | `min-width` | Small screens load the simplest CSS |
| Desktop first | Large screens | `max-width` | Small screens must undo desktop rules |
Mobile first is the better default for navigation. You write the compact layout with no query, and you add the wide layout when there is room for it. Browsers that fail to match a query still show a usable menu.
A worked example
This example uses the nav from the hamburger chapter that follows. The markup has a toggle button and a list of links:
<meta name="viewport" content="width=device-width, initial-scale=1">
<header>
<a href="/">Site name</a>
<button id="menu-toggle" type="button" aria-expanded="false" aria-controls="site-menu">Menu</button>
<nav aria-label="Main">
<ul id="site-menu" hidden>
<li><a href="/products">Products</a></li>
<li><a href="/pricing">Pricing</a></li>
<li><a href="/docs">Docs</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
</header>The base CSS describes the phone layout. The media query describes the wide layout:
#site-menu {
list-style: none;
margin: 0;
padding: 0;
}
#site-menu a {
display: block;
padding: 0.75rem 1rem;
}
@media (min-width: 48em) {
#menu-toggle {
display: none;
}
#site-menu,
#site-menu[hidden] {
display: flex;
gap: 1rem;
}
}Read the media query in three steps:
- Below 48em, the list keeps the
hiddenattribute and stays closed until script opens it. The toggle button is visible. - At 48em and above, the toggle button disappears with
display: none. - At the same width, the
#site-menu[hidden]selector overrides the browser's built-indisplay: nonefor thehiddenattribute, so the list shows as a row even if the attribute is still present.
The same markup serves both layouts. You do not need a second copy of the menu for desktop.
Common errors
- Missing viewport tag. Symptom: the phone shows a shrunken desktop page, and your mobile media queries never apply. Fix: add the meta tag to
<head>. - Breakpoints in
pxonly. Symptom: users with larger default text see links wrap or overflow at widths that looked fine in testing. Fix: useemfor breakpoints. - Breakpoints chosen from device names. Phones and tablets change every year. Choose a breakpoint where your own content stops fitting. For a nav, widen the browser slowly and note where the links first fit on one line.
- Forgetting that
hiddenhas low priority. Thehiddenattribute is only a default style from the browser, so any rule that setsdisplayon the same element overrides it. This is why the example above targets[hidden]on purpose.
Exercise
Your navigation looks correct in a desktop browser narrowed to 400px, but on a real phone it appears as a tiny full-width desktop layout. What is the most likely cause, and what single line fixes it?
Show answer
The page has no viewport meta tag, so the phone assumes a layout width of about 980 CSS pixels and scales the page down. Add <meta name="viewport" content="width=device-width, initial-scale=1"> inside <head>.
Exercise
In the worked example, why does the wide-screen rule list both #site-menu and #site-menu[hidden]?
Show answer
The hidden attribute applies display: none through the browser's default styles. The selector #site-menu[hidden] is more specific than the browser default, so its display: flex wins. This keeps the list visible on wide screens even if script left the hidden attribute set after the menu was closed on a narrow screen.