How to Add a Favicon to Your Site's HTML
Two things make a favicon work: an image file on your site, and a link tag in the head of your page that points to it. Wikipedia describes the standard method the same way. A link element with a rel attribute, placed in the head section, names the file's format, name and location1.
The steps
- Save your icon file where your site can serve it. The root folder, next to your home page, is the usual spot. A file called favicon.ico there matches a convention browsers follow2.
- Open the HTML for your page and find the
<head>section. - Add a link tag inside it (examples below).
- Upload the changed page and the icon file to your site.
- Reload the page. If an old icon appears, work through the troubleshooting checklist.
The basic tag
This is the simplest form. MDN gives it as the link for a site's favicon2.
<link rel="icon" href="/favicon.ico">
The rel value says what the file is for. The href value says where it lives. Google says the href can be a relative path such as /smile.ico or a full address, and the file doesn't have to sit on your own site3.
Adding type and sizes
MDN explains that the sizes attribute gives the icon size, and type gives the MIME type, which is the label for the file's format2. Both are hints that help a browser choose.
<link rel="icon" href="/icon-32.png" type="image/png" sizes="32x32">2
When you list several icons, MDN says the browser chooses the one that works best for its context, using rel and sizes as hints2. Which file types make sense is covered in the sizes and formats page.
Is the tag required?
Not strictly. MDN says a request for /favicon.ico at the site root happens without any tag. It adds that providing explicit links protects you against changes to these conventions2.
Wikipedia says that if a linked favicon is specified, modern browsers choose it instead of the one in the root of the website1.
An older spelling
You'll see rel="shortcut icon" in older pages. Google says it still supports it for historical reasons, as an earlier alternative to icon3. New pages can use plain icon.
Check these before you move on
- The page with the tag must be one search engines can crawl. Google says the relevant URLs must not be blocked from crawling by a robots.txt file or other means3.
- The icon file's own address must load when you type it into a browser.
- One tag is enough to start. You can add more later for phones, as covered in the Apple touch icon page.
If you use a website builder or a content management system, it may have a settings field for the icon instead. Follow your platform's own help pages for that.
Sources used
- Favicon, Wikipedia
- <link>: The External Resource Link element, MDN Web Docs
- Define a favicon to show in search results, Google Search Central