Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 122 ยท Images and Media

Favicons and App Icons

Add a favicon with link rel="icon", use SVG and dark-mode icons, and set up apple-touch-icon and a web app manifest. A minimal, modern set of icon files.

Look at the tabs at the top of your browser. Each one shows a tiny picture next to the page title. That picture is the favicon (short for "favorite icon", from the days when it appeared in bookmarks). It also shows up in bookmarks, history, search results on some engines, and โ€” at a much larger size โ€” on a phone's home screen when someone saves your site.

A good icon makes your site easy to find among twenty open tabs. In this lesson you will learn how browsers find icons, which files you actually need today (fewer than most generators produce), how to make an SVG icon that adapts to dark mode, and the basics of the web app manifest.

How browsers find a favicon

Icons are declared in the page's <head> with a <link> element whose rel is icon:

HTML
<head>
  <meta charset="utf-8">
  <title>Northwind Books</title>
  <link rel="icon" href="/favicon.svg" type="image/svg+xml">
</head>

If a page declares no icon at all, most browsers quietly request /favicon.ico from the root of your site. That is why server logs on sites without one are full of "404 favicon.ico" errors. Even if you declare your icons with <link>, keeping a favicon.ico at the root is a cheap safety net for tools and old software that only look there.

A few attributes help the browser choose between several icons:

  • type โ€” the file's MIME type, such as image/svg+xml or image/png. Browsers skip types they do not support.
  • sizes โ€” the pixel dimensions, such as 32x32, or any for scalable formats like SVG.
  • href โ€” the URL of the file, like any other link.

You may see rel="shortcut icon". That is a leftover from early Internet Explorer. Browsers still accept it, but rel="icon" is all you need.

Designing for 16 pixels

In a browser tab, your icon is usually drawn at 16ร—16 CSS pixels (32ร—32 on high-density screens). That is tiny. A full logo with a company name becomes an unreadable smudge. Good favicons are:

  • Simple: one bold shape or a single letter.
  • High contrast: they must work on light and dark tab bars.
  • Square: the browser fits the icon into a square.

The editor below defines one icon as an SVG <symbol> and shows it at several sizes. Try adding fine details โ€” a thin line, small text โ€” and see what survives at 16 pixels.

Test your icon at real sizes

SVG favicons

For years favicons meant a stack of PNGs (16, 32, 48 pixelsโ€ฆ) packed into an .ico file. Today you can use a single SVG file instead. Because it is a vector, it is sharp at every size and usually only a few hundred bytes.

HTML
<link rel="icon" href="/favicon.svg" type="image/svg+xml">

Chromium-based browsers (Chrome, Edge, Opera) and Firefox all support SVG favicons. Older versions of Safari ignore them, which is one more reason to keep a fallback, as you will see in the recommended set below.

A dark-mode favicon

An SVG file can contain its own CSS, including the prefers-color-scheme media query. That means one icon file can switch colors when the visitor's system uses dark mode:

HTML
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
  <style>
    .tile { fill: #ffffff; stroke: #4f46e5; }
    @media (prefers-color-scheme: dark) {
      .tile { fill: #1e1b4b; stroke: #a5b4fc; }
    }
  </style>
  <rect class="tile" x="1.5" y="1.5" width="29" height="29" rx="7" stroke-width="3"/>
</svg>

A dark icon on a dark tab bar disappears; this technique fixes that. The same file works as an ordinary image, so you can preview it on the page:

An adaptive SVG icon

The icon reacts to the system or browser theme, not to the color of the surface around it. That matches how browsers draw their tab bars, which also follow the system theme.

Icons for home screens: apple-touch-icon

When someone adds your site to the home screen on an iPhone or iPad, iOS looks for this link:

HTML
<link rel="apple-touch-icon" href="/apple-touch-icon.png">

Use a 180ร—180 PNG. iOS does not use SVG here, and it fills transparent areas with black, so give the icon a solid background and a little padding around the main shape. iOS rounds the corners for you; do not round them yourself.

Many Android browsers and other tools read this link too, as a fallback when nothing better is available.

The web app manifest

A web app manifest is a small JSON file that describes your site as an installable app: its name, its icons, its colors and how it should open. Link it from the <head>:

HTML
<link rel="manifest" href="/manifest.webmanifest">

A minimal manifest looks like this:

JSON
{
  "name": "Northwind Books",
  "short_name": "Northwind",
  "start_url": "/",
  "display": "standalone",
  "background_color": "#ffffff",
  "theme_color": "#4f46e5",
  "icons": [
    { "src": "/icon-192.png", "type": "image/png", "sizes": "192x192" },
    { "src": "/icon-512.png", "type": "image/png", "sizes": "512x512" },
    { "src": "/icon-maskable.png", "type": "image/png", "sizes": "512x512", "purpose": "maskable" }
  ]
}
  • name and short_name appear under the icon and in install prompts.
  • display: "standalone" opens the installed site in its own window, without the browser's address bar.
  • background_color is used for the splash screen while the app starts; theme_color can tint the window's title bar.
  • The 192 and 512 pixel icons cover home screens, app launchers and splash screens.

Maskable icons

Android shapes app icons into circles, squircles or rounded squares, depending on the device. An icon marked "purpose": "maskable" tells the system it may crop the image to any of those shapes. Keep everything important inside the central safe zone (a circle with a radius of 40% of the icon's width) and let the background color fill the edges.

Manifests can do much more โ€” shortcuts, screenshots, and the building blocks of progressive web apps โ€” but icons and names are the part every site benefits from.

A modern, minimal icon set

You do not need the twenty files some generators produce. This set covers current browsers and devices:

HTML
<link rel="icon" href="/favicon.ico" sizes="32x32">
<link rel="icon" href="/icon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<link rel="manifest" href="/manifest.webmanifest">
File Size Used by
favicon.ico 32ร—32 Older browsers, feed readers, tools that only check the root
icon.svg scalable Modern browser tabs; can adapt to dark mode
apple-touch-icon.png 180ร—180 iOS home screens and many other fallbacks
icon-192.png, icon-512.png 192, 512 Referenced from the manifest for Android and installed apps

The sizes="32x32" on the .ico link matters: it tells browsers that this file is small, so those that support SVG prefer the scalable icon.svg instead of the old bitmap.

Favicons are cached stubbornly. Browsers can keep showing an old icon for days, even after a hard refresh. When you change your icon, give the file a new name (or add a version such as /icon.svg?v=2) so browsers fetch the new one.

The color that browsers use around your page, set with <meta name="theme-color">, is covered in Meta Tags.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A page has no link rel="icon" at all. What do most browsers do?

    Without a declared icon, browsers fall back to requesting /favicon.ico from the root of the site.
  2. 2. How can one SVG favicon switch colors in dark mode?

    SVG files can contain CSS. A prefers-color-scheme media query inside the file changes the colors when the system is in dark mode.
  3. 3. What should an apple-touch-icon be?

    iOS expects a PNG (180ร—180 is the common size). It fills transparency with black and adds rounded corners itself, so use a solid background.
  4. 4. Where do the 192ร—192 and 512ร—512 icons for installed web apps usually get declared?

    The manifest's icons array lists the larger icons that Android and installed apps use, including maskable variants.

Summary

  • Declare icons with <link rel="icon"> in the <head>; without one, browsers request /favicon.ico.
  • Favicons appear at 16ร—16 CSS pixels, so keep the design bold, simple and square.
  • A single SVG favicon stays sharp at every size and can adapt to dark mode with prefers-color-scheme.
  • <link rel="apple-touch-icon"> points to a 180ร—180 PNG for iOS home screens.
  • A web app manifest names your site and lists 192 and 512 pixel icons, including maskable ones.
  • A minimal set is: favicon.ico, an SVG icon, an apple-touch-icon and a manifest.

Next up: adding moving pictures to your pages with Video.