Meta Tags and the Viewport
What meta charset, viewport, description and robots do, what happens without charset, and why a page without the viewport tag looks zoomed out on phones.
Meta Tags and the Viewport
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `<meta charset="utf-8">` tells the browser how to decode the bytes. In a test, a UTF-8 page with no charset anywhere was decoded as windows-1252 and `café` showed as `café`, in Chromium, Firefox and WebKit.
- `<meta name="viewport" content="width=device-width, initial-scale=1">` is what makes a page lay out at the phone's width. In a mobile-emulated test without it, the layout width was 980px; with it, 390px.
- `<title>` and `<meta name="description">` are what search results and browser tabs show. The description is a hint search engines may replace.
- `<meta name="robots" content="noindex">` asks search engines not to index a page, and `<link rel="canonical">` names the preferred URL among duplicates. Both are requests, not enforcement.
- Do not set `user-scalable=no` or `maximum-scale=1` in the viewport tag. They stop users zooming, which breaks accessibility.
Information about the page, not on it
Everything in <head> is information for the browser, search engines and other programs, not content for the reader. The <meta> element is the general container for it: a name and a value, or a special-purpose attribute like charset. Two of them can break a page if missing, and a few shape how it appears elsewhere.
charset: how to read the bytes
A file is a stream of bytes. Which bytes mean é depends on the encoding, and the browser has to guess unless told. Telling it is the job of <meta charset="utf-8">, or of a charset in the Content-Type header.
I tested the guess. A server sent a UTF-8 page containing café with Content-Type: text/html and no charset, and no meta tag. In headless Chromium, Firefox and WebKit, document.characterSet was windows-1252 and the body read café. With <meta charset="utf-8"> in the head, all three reported UTF-8 and showed café. The spec asks for the declaration to be within the first 1024 bytes of the file, so put it first in the <head>. I did not test what happens when it is later.
Use UTF-8, always. Anatomy of an HTML document puts the tag in context.
viewport: the layout width on phones
Early phones met sites built for 980px-wide desktops. To cope, mobile browsers lay a page out at a virtual width of about 980px and shrink it to fit the screen. Sites designed for phones need a way to say “do not pretend; use the real width”. That is the viewport meta tag:
<meta name="viewport" content="width=device-width, initial-scale=1">
I tested it with Chromium’s mobile emulation at 390px wide. A page without the tag had document.documentElement.clientWidth of 980. The same page with the tag had 390. Media queries respond to that width, so without the tag a max-width: 600px rule never fires and the layout is a tiny desktop page. This is why responsive design starts here. I tested only Chromium emulation, not a real phone.
Leave out user-scalable=no and maximum-scale=1. They disable zoom, which people with low vision depend on. The accessible CSS lesson covers this.
title and description
<title> is the text in the tab, the bookmark and, usually, the search result’s headline. Every page needs a unique, specific one. <meta name="description" content="..."> is the summary search engines may show under the title. It is a suggestion: engines often write their own snippet from the page. A good description is a sentence or two that would make the right person click. The older keywords meta tag is ignored by search engines and can be omitted. The SEO basics lesson goes further.
robots, canonical and the others
<meta name="robots" content="noindex"> asks search engines not to index the page; nofollow asks them not to follow its links. They are requests that well-behaved crawlers honour; they are not access control. A page you need private needs authentication.
<link rel="canonical" href="...">, technically a link, not a meta tag, names the preferred URL when the same content appears at several (with tracking parameters, for example). The link elements lesson covers links in general.
A few more you may meet: <meta name="color-scheme" content="light dark"> tells the browser the page supports both, so it can theme form controls and the default canvas; <meta name="theme-color"> tints the browser UI on some mobile browsers; and <meta http-equiv="refresh"> redirects or reloads after a delay and is best avoided, since it breaks the back button and is hard to use accessibly. I did not test these. Social-sharing tags get their own lesson.
What the video covers
The embed is the whole of Learndev’s “Learn HTML Meta-Tags in 4 Minutes!” (June 2024). Its chapter list covers what meta tags are, the title tag, the description, keywords and author tags, refresh, the viewport tag, the robots tag and the canonical URL. That is close to this lesson’s list, in about four minutes. It is short, so it skips explanation such as why the viewport tag exists or what charset does; the lesson fills those gaps, and the charset and viewport behavior above comes from my own tests. I could not read a transcript, and the video’s keywords section may suggest a tag that search engines ignore. The runner-up is Kevin Powell’s focused three-minute viewport explanation.
Where this fits
This opens the head group, after anatomy of an HTML document. Next, link elements, stylesheets and icons covers the other main head element. Social tags come in social metadata and structured data.
Check yourself
A page contains `café` in UTF-8 but shows `café`. What is missing?
A declared encoding. Add `<meta charset="utf-8">` early in the `<head>`, or send `charset=utf-8` in the `Content-Type` header. In a test, a page with neither was decoded as windows-1252 in all three browsers.
Your responsive CSS has a media query at 600px, but on a phone the page looks like a shrunken desktop site. Why?
There is no viewport meta tag. Phones then lay the page out at a virtual width, 980px in a test, and scale it down to fit. Adding `width=device-width` makes the layout width the device's own, 390px in the test, so media queries see the real width.
What is the difference between `noindex` and `canonical`?
`noindex` asks search engines to leave this page out of results. `canonical` says this page is a copy and names the URL to treat as the main one, so duplicates share their ranking. Neither is enforced; they are signals.
Why avoid `maximum-scale=1` in the viewport tag?
It stops pinch-zoom, which people with low vision rely on. The page should work when zoomed, so leave scaling to the user.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.