Sub topic 1
Setting up the viewport
Every Tailwind breakpoint depends on one number: the width the browser reports for the page. On a desktop that number is the window width, so it is rarely a problem. On a phone, the browser can report a width that has little to do with the physical screen unless you tell it otherwise. This chapter covers the one line of HTML that fixes this, what it does, and how to check that it works.
Prerequisites
This chapter assumes you have the following:
- Basic HTML and CSS, including flexbox and grid.
- Familiarity with Tailwind utility classes such as
flex,p-4andtext-lg. - A working idea of what a CSS media query is, meaning a rule that applies only when a condition such as the screen width is true.
You do not need to know the breakpoint prefixes yet. The next chapters introduce them. Here you only need to see why they depend on the viewport.
What the viewport is
The viewport is the rectangular area of the page that the browser uses to lay out content and evaluate media queries. Tailwind's responsive prefixes such as md: compile to media queries on the viewport width. If the browser reports the wrong width, every breakpoint fires at the wrong time.
Mobile browsers distinguish two viewports:
- The layout viewport is the width the browser uses to lay out the page and to evaluate media queries.
- The visual viewport is the part of the page currently visible on screen. It changes when the user pinch-zooms.
The layout viewport is the one that matters for Tailwind. Its width is measured in CSS pixels, not physical device pixels. A modern phone may have a screen 1170 physical pixels wide, but it reports about 390 CSS pixels because the display packs several physical pixels into each CSS pixel. This is what lets the same 16px font look about the same size on screens of very different density.
The problem the meta tag solves
When the first iPhone shipped, most websites were built for desktop screens. To avoid breaking them, mobile browsers began pretending that the screen was wide, around 980 CSS pixels, and then scaling the whole page down to fit. Mobile browsers still do this for any page that does not say it is mobile-aware.
Without a viewport declaration, a Tailwind page on a phone has these symptoms:
- The layout viewport is about 980px wide, so
md:(768px) and evenlg:(1024px, close) styles can apply on a phone. - The page looks like a tiny desktop site, and users have to pinch-zoom to read it.
- Text that you sized carefully appears very small.
The meta tag below tells the browser that the page is responsive and that the layout viewport should match the device width:
<meta name="viewport" content="width=device-width, initial-scale=1">width=device-widthsets the layout viewport width to the device's width in CSS pixels, for example 390 on a typical phone.initial-scale=1sets the initial zoom to 100%, so one CSS pixel maps to one CSS pixel of the layout viewport with no scaling.
With this tag in place, a 390px phone reports a 390px viewport. Unprefixed utilities apply, and sm: (640px) does not.
Where the tag goes
Place the tag inside <head>, before any content. It must be in the HTML the server sends, or in the HTML that a framework produces during rendering. A tag added later by JavaScript can work in some browsers, but the browser may already have laid out the page, which causes a visible jump.
Here is a complete page you can save as index.html and open on a phone or in a browser's device mode. It loads Tailwind v4 from the official browser build, which is meant for experiments and not for 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="rounded bg-red-200 p-4 text-center sm:bg-yellow-200 md:bg-green-200 lg:bg-blue-200">
Red below 640px, yellow from 640px, green from 768px, blue from 1024px.
</p>
</body>
</html>The paragraph changes color as the viewport widens. Each prefix overrides the one before it. This is the mobile-first behavior covered in the next chapter. For now, the point is that the colors change at the widths listed only if the viewport tag is present. Remove the <meta name="viewport"> line, reload on a real phone, and the paragraph will usually show a wider color than you expect.
In build tools and frameworks
Most project starters already include the tag. Check before adding a second one.
| Setup | Where the tag lives |
|---|---|
| Plain HTML or Vite starter | `index.html`, inside `<head>` |
| Next.js (App Router) | Added by default. Customize with `export const viewport` in `layout.tsx` |
| Laravel Blade | The main layout file, such as `resources/views/layouts/app.blade.php` |
| Astro, SvelteKit, Nuxt | The root layout or `app.html` template |
If you need to change the defaults in Next.js, the App Router uses an exported object instead of a hand-written tag:
// app/layout.js
export const viewport = {
width: 'device-width',
initialScale: 1,
};
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}Duplicate viewport tags are not an error, but they make debugging harder. The browser uses the combined values, and later properties can override earlier ones. Keep one tag.
The other viewport properties
The content attribute is a comma-separated list of properties. You will use width and initial-scale in nearly every project. The others solve narrower problems.
| Property | Example value | What it does |
|---|---|---|
| `width` | `device-width` or `600` | Sets the layout viewport width in CSS pixels |
| `initial-scale` | `1` | Sets the starting zoom level |
| `minimum-scale`, `maximum-scale` | `1`, `5` | Limits how far the user can zoom |
| `user-scalable` | `no` | Disables pinch zoom in some browsers |
| `viewport-fit` | `cover` | Extends the page under display cutouts such as notches |
| `interactive-widget` | `resizes-content` | Controls how the on-screen keyboard changes the viewport |
Two of these deserve a warning.
viewport-fit=cover changes how much of the screen your page occupies. If you use it, content can slide under a notch or a home indicator. You then need safe-area padding, for example with Tailwind's arbitrary value pt-[env(safe-area-inset-top)]. Most sites do not need it.
What the tag does not do
The tag makes the viewport width honest. It does not make your layout responsive. Several related problems still need their own solutions.
- It does not stop horizontal scrolling. A fixed-width element, such as
w-[800px], still overflows a 390px viewport. The viewport reports 390, but your content is wider. - It does not change the breakpoints.
sm:still means 640px and up, whatever the device. - It does not give you a way to test your page. You still need to resize the window or use device emulation, described next.
- It does not cover the viewport height. On mobile, the height changes as the address bar shows and hides, so
h-screen(100vh) can be taller than the visible area. Tailwind providesh-dvh(100dvh, the dynamic viewport height) for full-screen sections.
Checking that the viewport works
Verify the setup once at the start of every project. A few minutes here saves confusion later.
- Open the page in Chrome or Edge, and open DevTools.
- Toggle device mode (the phone and tablet icon, or
Ctrl+Shift+M/Cmd+Shift+M). - Choose a phone preset or set the width to 390.
- Reload the page. Emulation reads the viewport tag only at load time, so a reload after changing the tag is needed.
- Check that the page fills the width and that text is readable without zooming.
You can also read the width directly. Open the console and run this:
console.log(window.innerWidth, window.devicePixelRatio);On a correctly configured phone, innerWidth is about 360 to 430 and devicePixelRatio is 2 or 3. If innerWidth is about 980, the browser is using the default virtual viewport, and the tag is missing or malformed.
Common typing mistakes make the tag silently fail:
name="viewpoint"instead ofviewport.- A semicolon instead of a comma between properties.
width=device-widthwritten aswidth=device_width.- The tag placed in
<body>, or inside a comment.
Emulation in desktop DevTools is useful but not the same as a real device. It does not reproduce every behavior of mobile browsers, such as address bar resizing. Before release, check at least one real phone.
Key points
- Tailwind breakpoints are media queries on the layout viewport width.
- Mobile browsers default to a virtual width of about 980 CSS pixels unless the page opts out.
<meta name="viewport" content="width=device-width, initial-scale=1">in<head>is the opt-out, and almost every project needs it.- Do not disable zoom.
- Confirm the setup with device mode and
window.innerWidthbefore working on layouts.
Exercise
You open a Tailwind page on a phone and the layout looks like a shrunken desktop site. The elements with md: classes appear to be active, even though the phone is about 390 CSS pixels wide. In the console, window.innerWidth returns 980. What is the most likely cause, and what single line fixes it?
Show answer
The page has no viewport meta tag (or it is malformed), so the mobile browser uses its default virtual layout width of about 980px. That is wider than the 768px md: breakpoint, so md: styles apply. Add <meta name="viewport" content="width=device-width, initial-scale=1"> inside <head>, then reload. innerWidth should then report about 390.
Exercise
A teammate adds maximum-scale=1, user-scalable=no to the viewport tag so that iOS stops zooming when users tap a form field. Why is this a poor fix, and what is a better one?
Show answer
It blocks pinch zoom for everyone, which harms users with low vision and fails WCAG 1.4.4. Some browsers ignore it anyway. The better fix is to give the input a font size of at least 16px, for example with text-base, because iOS Safari zooms only when the focused field's text is smaller than that.
Exercise
A page has the correct viewport tag, and a card with the class w-[800px] is inside the body. On a 390px phone, the page scrolls sideways. Does adding or changing the viewport tag help? Why or why not?
Show answer
No. The viewport is already 390px wide, which is what the tag is supposed to do. The card is a fixed 800px wide, so it overflows the viewport. The fix is in the layout, for example w-full max-w-[800px], not in the meta tag.