Sub topic 1
Setting up the viewport
Prerequisites
This chapter assumes you know the following:
- Basic HTML and CSS, including flexbox and grid.
- Tailwind utility classes such as
flex,p-4andtext-lg. - What a CSS media query is. Tailwind's breakpoint prefixes compile to media queries on viewport width.
Why the viewport matters
Tailwind's responsive prefixes such as md: and lg: compile to media queries that test the viewport width. A media query is only useful if the browser reports the real width of the device.
Mobile browsers do not do this by default. Early phone browsers had to display pages built for desktops, so they pretend the screen is wide, usually around 980 CSS pixels. They then shrink the whole page to fit. This pretend width is the layout viewport.
Without a hint from your page, a 390px-wide phone matches sm:, md: and lg: rules as if it were a desktop. Your mobile layout never appears, and the page looks like a tiny desktop site.
The meta tag
Put this tag inside the <head> of every page:
<meta name="viewport" content="width=device-width, initial-scale=1">It has two parts:
width=device-widthsets the layout width to the device's width in CSS pixels.initial-scale=1sets the starting zoom to 100%, so one CSS pixel is shown at its natural size.
Tailwind does not add this tag for you. Its base styles (Preflight) reset browser defaults, but they cannot touch the <head>. You must include it yourself, or confirm that your framework or starter template does.
A complete page to test
This page uses the Tailwind v4 browser build from a CDN, so you can save it as a file and open it without a build step. The CDN build is meant for learning and prototypes, not production.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Viewport test</title>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body class="p-6">
<p class="bg-red-200 p-4 md:bg-green-200">
Red below 768px, green from 768px up.
</p>
</body>
</html>On a phone or a narrow browser window, the paragraph is red. Widen the window past 768px and it turns green. That color change is the md: breakpoint working.
Now delete the meta tag and load the page on a real phone. The paragraph will probably turn green, because the phone reports a width of around 980px. This is the failure the tag prevents.
Options to avoid
You may see tutorials add maximum-scale=1 or user-scalable=no to the content value. Do not copy them. They block pinch-to-zoom, which people with low vision depend on, and they can fail accessibility checks.
| Value | Effect | Recommendation |
|---|---|---|
| `width=device-width` | Uses the real device width | Always include |
| `initial-scale=1` | Starts at 100% zoom | Always include |
| `maximum-scale=1` | Limits zoom in | Avoid |
| `user-scalable=no` | Disables pinch zoom | Avoid |
Checking it works
Use these checks after you add the tag:
- Open the page in your browser's developer tools and switch on device emulation (the phone and tablet icon in Chrome and Firefox).
- Pick a phone preset such as a 390px-wide device. Confirm that only unprefixed styles apply.
- Drag the width above 768px, or choose a tablet preset, and confirm that
md:styles take over. - If the layout looks like a shrunken desktop page, view the page source and look for the meta tag. Frameworks that generate the
<head>can omit or override it.
With the viewport set correctly, the next chapter's mobile-first model works as described. Unprefixed utilities target every screen, and prefixed utilities add changes as the width grows.
Exercise
A page uses class="grid-cols-1 md:grid-cols-3" and has no viewport meta tag. On a 390px phone it shows three columns, but in a resized desktop window it shows one column at the same width. Why does this happen, and what is the fix?
Show answer
Without the tag, the phone uses a layout width of about 980px, which is above the 768px md breakpoint, so md:grid-cols-3 applies. A resized desktop window reports its true width, so it shows one column. Add <meta name="viewport" content="width=device-width, initial-scale=1"> inside <head> so the phone reports 390px.