What is the best practice for creating a favicon on a web site?

HtmlFavicon

Html Problem Overview


Question

  • What is the best practice for creating a favicon on a web site?
  • and is an .ico file with both 16x16 and 32x32 images better than a .png file with 16x16 only?
  • Could the right method preferred today not be working in reasonably old browsers?

Method 1

Placing a file named favicon.ico in the main directory is one way. The browser always requests that file. You can see that in the apache log files.

Method 2

HTML tag in the <head> section:

<link rel="shortcut icon" href="/images/favicon.png (or ico?)" type="image/x-icon" />

Html Solutions


Solution 1 - Html

There are several ways to create a favicon. The best way for you depends on various factors:

  • The time you can spend on this task. For many people, this is "as quick as possible".
  • The efforts you are willing to make. Like, drawing a 16x16 icon by hand for better results.
  • Specific constraints, like supporting a specific browser with odd specs.

First method: Use a favicon generator

If you want to get the job done well and quickly, you can use a favicon generator. This one creates the pictures and HTML code for all major desktop and mobiles browsers. Full disclosure: I'm the author of this site.

Advantages of such solution: it's quick and all compatibility considerations were already addressed for you.

Second method: Create a favicon.ico (desktop browsers only)

As you suggest, you can create a favicon.ico file which contains 16x16 and 32x32 pictures (note that Microsoft recommends 16x16, 32x32 and 48x48).

Then, declare it in your HTML code:

<link rel="shortcut icon" href="/path/to/icons/favicon.ico">

This method will work with all desktop browsers, old and new. But most mobile browsers will ignore the favicon.

About your suggestion of placing the favicon.ico file in the root and not declaring it: beware, although this technique works on most browsers, it is not 100% reliable. For example Windows Safari cannot find it (granted: this browser is somehow deprecated on Windows, but you get the point). This technique is useful when combined with PNG icons (for modern browsers).

Third method: Create a favicon.ico, a PNG icon and an Apple Touch icon (all browsers)

In your question, you do not mention the mobile browsers. Most of them will ignore the favicon.ico file. Although your site may be dedicated to desktop browsers, chances are that you don't want to ignore mobile browsers altogether.

You can achieve a good compatibility with:

  • favicon.ico, see above.
  • A 192x192 PNG icon for Android Chrome
  • A 180x180 Apple Touch icon (for iPhone 6 Plus; other device will scale it down as needed).

Declare them with

<link rel="shortcut icon" href="/path/to/icons/favicon.ico">
<link rel="icon" type="image/png" href="/path/to/icons/favicon-192x192.png" sizes="192x192">
<link rel="apple-touch-icon" sizes="180x180" href="/path/to/icons/apple-touch-icon-180x180.png">

This is not the full story, but it's good enough in most cases.

Solution 2 - Html

  1. you can work with this website for generate favin.ico
  2. I recommend use .ico format because the png don't work with method 1 and ico could have more detail!
  3. both method work with all browser but when it's automatically work what you want type a code for it? so i think method 1 is better.

Solution 3 - Html

I used https://iconifier.net I uploaded my image, downloaded images zip file, added images to my server, followed the directions on the site including adding the links to my index.html and it worked. My favicon now shows on my iPhone in Safari when 'Add to home screen'

Solution 4 - Html

As SVG favicon is gaining support in major browsers, another option would be to switch to SVG:

<link rel="icon" sizes="any" type="image/svg+xml" href="favicon.svg">
<!-- Fallback favicon in case a browser does not support the SVG version -->
<link rel="alternate icon" type="image/x-icon" href="favicon.ico">

Like other formats, you can also encode your SVG to Base64 (for example, using this online tool). The benefit is that, it can look like the original SVG and you avoid one HTTP request:

<link rel="icon" sizes="any" type="image/svg+xml" href="data:image/svg+xml;utf8,%3Csvg viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill='%23f5e400' d='m6 21 2-7-6-5h7l3-7 3 7h7l-6 5 2 7-6-4z'/%3E%3C/svg%3E%0A">

This CSS-Tricks article explains some of the benefits of switching to SVG, most notably:

  • Future-proofing (guarantee that our favicon looks crisp on future devices)
  • Easier to modify (for example, changing a color)
  • Dark mode support
  • Could be animated (using SMIL) (AFAIK, only Firefox supports this)

This post provides a good answer whether/where it is safe to use an SVG image for favicon.

And here are some famous sites that are using SVG favicons (inspect a page with your browser):

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionbytecode77View Question on Stackoverflow
Solution 1 - Htmlphilippe_bView Answer on Stackoverflow
Solution 2 - Htmla828hView Answer on Stackoverflow
Solution 3 - HtmlHbleggView Answer on Stackoverflow
Solution 4 - HtmlMahozadView Answer on Stackoverflow