Lesson 43 of 51 · Module 7, lesson 3 of 11 · 3 min

Favicons

Give your site a small icon on the browser tab.

What you'll learn

  • What a favicon is
  • How to add one with <link rel="icon">
  • Which file types and sizes to use

Why this matters

A favicon is the small icon on the browser tab, next to the title. When someone has many tabs open, it is how they find yours. It also appears in bookmarks and history. It takes one line of HTML.

Two browser tabs. The first, "Home Kitchen", shows a small blue icon with a red bowl. The second, "My notes", has no favicon, so the browser shows a plain default icon. Below, the same icon large: at 16 pixels, only simple shapes and strong colours stay clear.

Get the sample icon

  1. Open favicon.png.
  2. Right-click the image and choose Save image as….
  3. Save it in your my-website folder, next to index.html, as favicon.png.

Add the line

In the <head> of every page:

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Home — Home Kitchen</title>
  <link rel="icon" href="favicon.png" type="image/png">
</head>
<body>
  <h1>Home Kitchen</h1>
  <p>Recipes I love.</p>
</body>
</html>

What you'll see: open the page from your folder. The tab shows the small icon before the title. (The Try code playground has no tab, so try this one in your own browser.)

  • rel="icon" says what the file is for.
  • href is the path to the icon — relative, as usual.
  • type says the file format.

A page inside a subfolder, such as recipes/soup.html, needs href="../favicon.png".

Which file?

FormatNotes
.pngWorks in every current browser. 32 × 32 pixels or larger.
.svgSharp at every size. type="image/svg+xml".
.icoThe oldest format. Browsers look for favicon.ico at the top of a website even without a <link>.

Design tips: keep it simple — one shape, one or two strong colours, no small text. At 16 pixels, details disappear.

The icon does not change?

Browsers keep favicons in memory for a long time. After you replace the file, do a hard refresh: Ctrl + Shift + R (Windows) or Command + Shift + R (macOS). If it still shows the old icon, open the page in a private window.

Try it yourself

  1. Add the favicon to index.html, about.html and your other pages.
  2. Open two of your pages in tabs. Both show the icon.
  3. Make your own icon: draw a simple 64 × 64 image in any drawing program, save it as PNG, and use it instead.

Common mistakes

  • A leading slash, as in href="/favicon.png". It fails when you open the page from your computer. Fix: href="favicon.png".
  • Forgetting ../ on pages in subfolders. Fix: check the path from that page.
  • Expecting the new icon at once. The old one is remembered. Fix: hard refresh.
  • A detailed photo as an icon. Unrecognisable at 16 pixels. Fix: a simple shape.

Check yourself

  1. Where does the favicon line go?
  2. What is the path to favicon.png from recipes/soup.html?
  3. You changed the icon, but the old one still shows. What do you do?

Answers

  1. In the <head>.
  2. ../favicon.png.
  3. A hard refresh (Ctrl + Shift + R or Command + Shift + R), or open a private window.

Next: from small icons to moving pictures — video, with captions so that everyone can follow it.

Chat with us on WhatsApp