Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

Adding a Favicon to a Site

4.60/5 (4 votes)
14 Jan 2014CPOL2 min read 8.3K  
How to add a favicon to a site

Introduction

I forgot to make a favicon when I created the theme for this site, so let’s fix this. A favicon is the small icon that appears beside the title in the tab, and that is shown when the user bookmarks your site. According to the W3C, the favicon should be a .PNG, .GIF or .ICO image that’s 32×32 pixels or 16×16 pixels. So I created a .PNG image that is 32×32 pixels so it will look nice scaled down, and is a nice starting size if some browsers display it bigger. I also added the following tag to the head of each page:

XML
<link rel="icon" type="image/png" href="http://www.cindypotvin.com/favicon.png" />

Unfortunately, Internet Explorer only supports .ICO for favicons, even Internet Explorer 10, so I had to also add the following in the head:

XML
<link rel="shortcut icon" type="image/x-icon" href="http://www.cindypotvin.com/favicon.ico" />

Depending on the browser and the order of the declarations, the .PNG or the .ICO will be used, but since it’s the same image, it’s good enough for me and respects the standard. Everything now works in Internet Explorer, Chrome and Firefox on the desktop, but it will not work with mobile devices. When the web site is added to the home screen in iOS or Android, the favicon will not be shown. iOS looks for a link tag in the head of the page with the rel=apple-touch-icon, which adds the 3D effect in iOS 6 or earlier (iOS 7 icons stays flat), or rel=apple-touch-icon-precomposed that displays the image without embellishments. Android phones will also recognise apple-touch-icon and apple-touch-icon-precomposed for the home screen images, so this is enough for both platforms. If you want to support older phones, Android 2.1 will only recognize apple-touch-icon-precomposed. I wanted to keep the same flat look as my theme, so I used the –precomposed attribute and the same icon as earlier:

XML
<link rel="apple-touch-icon-precomposed" href="http://www.cindypotvin.com/favicon.png" />

Using the same icon will make it stretched on every device, since the default size is 60×60. You can add icons specific for each platform with apple-touch-icon if you need a bit more control:

XML
<link rel="apple-touch-icon-precomposed" 
href="http://www.cindypotvin.com/touch-icon-iphone.png" />
<link rel="apple-touch-icon-precomposed" sizes="76x76" 
href="http://www.cindypotvin.com/touch-icon-ipad.png" />
<link rel="apple-touch-icon-precomposed" sizes="120x120" 
href="http://www.cindypotvin.com/touch-icon-iphone-retina.png" />
<link rel="apple-touch-icon-precomposed" sizes="152x152" 
href="http://www.cindypotvin.com/touch-icon-ipad-retina.png" />

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)