More HTML answers

by Danny
Published 3 min read

A modern HTML head needs only a handful of tags: charset, viewport, title, description, canonical, icons and theme-color. Here is a clean template and what you can safely drop.

The short answer

#

Most pages need fewer tags than old boilerplates suggest. Start with a charset, a viewport, a unique title and description, a canonical URL, your icons and an optional theme colour. Then add stylesheets and scripts.


_16
<!doctype html>
_16
<html lang="en-GB">
_16
<head>
_16
<meta charset="utf-8">
_16
<meta name="viewport" content="width=device-width, initial-scale=1">
_16
<title>Pricing | Acme</title>
_16
<meta name="description" content="Simple monthly plans for teams of any size.">
_16
<link rel="canonical" href="https://acme.example/pricing">
_16
<link rel="icon" href="/favicon.ico" sizes="32x32">
_16
<link rel="icon" href="/icon.svg" type="image/svg+xml">
_16
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
_16
<link rel="manifest" href="/manifest.webmanifest">
_16
<meta name="theme-color" content="#0f172a">
_16
<link rel="stylesheet" href="/styles.css">
_16
<script src="/app.js" type="module"></script>
_16
</head>

Why it happens

#

The head has collected decades of tags for browsers that no longer exist. Many templates still ship tags for old Internet Explorer modes, Windows tiles and search engines that stopped reading them years ago. Each extra tag is a few bytes, but more importantly it is noise that makes the important tags harder to spot and maintain.

The tags that still matter fall into three groups: ones the browser needs to parse and lay out the page, ones search engines and social sites read, and ones that control how your site looks when it is installed or saved.

How to fix it

#

Charset first

meta charset="utf-8" must appear within the first 1024 bytes of the document, so put it first in the head. Without it, browsers may guess the encoding and show garbled characters. UTF-8 is the only value you should use.

Viewport

width=device-width, initial-scale=1 tells mobile browsers to use the real device width rather than a zoomed-out desktop layout. Do not add maximum-scale=1 or user-scalable=no, which stop people zooming in.

Title and description

The title is shown in the tab, in bookmarks and as the headline in search results. Make it unique per page and put the specific part first, such as "Pricing | Acme". The meta description is not a ranking factor, but search engines often use it as the snippet under your link, so write one sentence that describes the page.

Canonical

link rel="canonical" tells search engines which URL is the main version of the page when the same content is reachable through several URLs, for example with tracking parameters or trailing slashes. Use an absolute URL.

Icons and manifest

A small set covers almost everything: a 32 pixel favicon.ico, an SVG icon for modern browsers, a 180 pixel apple-touch-icon for iOS home screens, and a web app manifest that lists larger PNG icons for Android and installed apps.

Theme colour

meta name="theme-color" lets some browsers tint their interface to match your site, mostly on mobile. Others ignore it. You can give separate values for light and dark mode with the media attribute.


_10
<meta name="theme-color" content="#ffffff" media="(prefers-color-scheme: light)">
_10
<meta name="theme-color" content="#0f172a" media="(prefers-color-scheme: dark)">

Social previews

If the page is likely to be shared, add Open Graph tags such as og:title and og:image. They are optional for the page to work but control how links look in chat apps and social feeds.

What you can drop


_10
<!-- None of these are needed any more -->
_10
<meta http-equiv="X-UA-Compatible" content="IE=edge">
_10
<meta name="keywords" content="...">
_10
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
_10
<link rel="shortcut icon" href="/favicon.ico">
_10
<meta name="msapplication-TileColor" content="...">
_10
<script type="text/javascript" src="/app.js"></script>
_10
<link rel="stylesheet" type="text/css" href="/styles.css">

  • X-UA-Compatible only affected Internet Explorer, which is retired.
  • Major search engines ignore meta keywords.
  • The long Content-Type form is replaced by the short meta charset.
  • shortcut in rel="shortcut icon" was never needed; rel="icon" is enough.
  • msapplication tiles were for pinned sites on old Windows versions.
  • type="text/javascript" and type="text/css" are the defaults.

Common pitfalls

#
  • Missing lang on the html element. It is not in the head, but screen readers use it to pick a voice and pronunciation. Set it every time.
  • Copying the same title and description to every page. Search results then look identical and users cannot tell pages apart.
  • Relative canonical URLs. They usually work, but absolute URLs avoid mistakes on staging domains and proxies.
  • Blocking scripts in the head. A classic script tag without defer or type="module" stops parsing until it downloads and runs.
  • Disabling zoom in the viewport tag. It is an accessibility failure and most mobile browsers now ignore it anyway.
#

Was this resource helpful?