Table of contents
1.
Introduction
2.
What is HTML Favicon?
3.
How to Implement a Favicon in HTML
3.1.
Step 1: Prepare Your Favicon Image
3.2.
Step 2: Place the Favicon File in Your Project
3.3.
Step 3: Link the Favicon in the HTML Document
4.
List of Favicon Sizes
5.
Add Favicon
6.
Favicon File Format Support
7.
Example of Using Different Formats for Favicons
8.
Frequently Asked Questions
8.1.
What size should my favicon be for a website?
8.2.
Can I use a PNG file as a favicon?
8.3.
How do I add a favicon to my website?
9.
Conclusion
Last Updated: Dec 30, 2024
Easy

Favicon in HTML

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

favicon (short for "favorite icon") is a small image or logo that represents a website. It appears in the browser's tab, bookmarks, and other places, making it easier for users to identify a website at a glance. 

Favicon in HTML

This article will guide you on how to implement a favicon in HTML, discuss the different favicon sizes, and explore the file formats that support favicons.

What is HTML Favicon?

HTML Favicon Favicons are typically square images, commonly 16x16 pixels in size. They are saved in ICO, PNG, or GIF format. To add a favicon to your website, you need to include a <link> tag in the <head> section of your HTML file. For example:

<head> <link rel="icon" type="image/png" href="/favicon.png"> </head>


The rel attribute specifies the relationship between the current document & the linked resource. For favicons, we use rel="icon". The type attribute indicates the MIME type of the image file. Common values are "image/x-icon" for ICO files, "image/png" for PNG, & "image/gif" for GIF.

The href attribute points to the location of your favicon file. It's recommended to place the favicon in the root directory of your website for easy reference.

How to Implement a Favicon in HTML

Adding a favicon to a website is a simple process that involves using the <link> tag in the HTML document. The favicon is usually linked within the <head> section of the HTML code. Below is a step-by-step guide to implementing a favicon.

Step 1: Prepare Your Favicon Image

The first step is to choose or create a small image that will represent your website. A typical favicon size is 16x16 pixels, but you can use larger sizes for better resolution on different devices.

Step 2: Place the Favicon File in Your Project

After selecting your favicon image (commonly named favicon.ico), place it in the root directory of your website or a specific folder like /images or /assets.

Step 3: Link the Favicon in the HTML Document

Now, in the <head> section of your HTML document, you need to add a <link> tag that points to the location of the favicon file. Here's an example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Website</title>
    
    <!-- Link to the Favicon -->
    <link rel="icon" href="favicon.ico" type="image/x-icon">
</head>
<body>
    <h1>Welcome to My Website!</h1>
    <p>This is a simple website with a favicon.</p>
</body>
</html>


Output

Output

In this code, the href attribute specifies the location of the favicon file, and the type attribute defines the format of the image (in this case, image/x-icon).

When you open this HTML file in a browser, the favicon will appear on the tab next to the page title.

List of Favicon Sizes

While a 16x16 pixel favicon is the most common size, modern browsers support multiple favicon sizes to accommodate different devices & resolutions. Let’s see some recommended favicon sizes, like:

  • 16x16 pixels: Standard favicon size for desktop browsers.
     
  • 32x32 pixels: Used by some browsers for higher-resolution displays.
     
  • 48x48 pixels: Used by some mobile browsers & device home screens.
     
  • 96x96 pixels: Used by some mobile browsers & device home screens for high-resolution displays.
     
  • 144x144 pixels: Used by Windows 8 & 10 for pinned site tiles.
     
  • 180x180 pixels: Used by iOS devices for home screen icons.
     
  • 192x192 pixels: Used by Android devices for home screen icons.
     
  • 512x512 pixels: Used by iOS & Android devices for splash screens & high-resolution icons.
     

To provide multiple favicon sizes, you can include multiple <link> tags in your HTML <head> section, each pointing to a different image file. For example:

<head>
  <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="96x96" href="/favicon-96x96.png">
  <link rel="icon" type="image/png" sizes="192x192" href="/favicon-192x192.png">
</head>


Note: With multiple favicon sizes, you can ensure that your website's favicon looks sharp & visually appealing across different devices & screen resolutions.

It’s important to provide favicons in multiple sizes to ensure your website looks good on various devices and screen resolutions.

Here is an example of how you can link multiple sizes of favicons in your HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Website</title>
    <!-- Multiple Favicon Sizes -->
    <link rel="icon" href="favicon-16x16.png" sizes="16x16" type="image/png">
    <link rel="icon" href="favicon-32x32.png" sizes="32x32" type="image/png">
    <link rel="icon" href="favicon-48x48.png" sizes="48x48" type="image/png">
    <link rel="icon" href="favicon-96x96.png" sizes="96x96" type="image/png">
    <link rel="icon" href="favicon-128x128.png" sizes="128x128" type="image/png">
    <link rel="icon" href="favicon-180x180.png" sizes="180x180" type="image/png">
</head>
<body>
    <h1>Welcome to My Website!</h1>
    <p>Different favicon sizes are used to make the website look great on all devices.</p>
</body>
</html>


Output

Output

In this example, we use multiple <link> tags with different sizes attributes to specify various sizes of favicons.

Add Favicon

Now that you know how to create a favicon and the recommended sizes, let's take a look at the steps to add a favicon to your website:

1. Design your favicon: Create a square image that represents your website or brand. You can use graphic design tools like Adobe Photoshop, Sketch, or online favicon generators.
 

2. Save the favicon: Save your favicon in the desired file format (ICO, PNG, or GIF) and size (16x16, 32x32, etc.). Give it a descriptive name like "favicon.ico" or "favicon-16x16.png".
 

3. Upload the favicon: Upload your favicon file(s) to the root directory of your website or to a specific folder dedicated to icons.
 

4. Add the HTML code: In the <head> section of your HTML file, add the <link> tag to reference your favicon. For example:

<head>
  <link rel="icon" type="image/png" href="/path/to/favicon.png">
</head>


Replace 

"/path/to/favicon.png" with the actual path to your favicon file.


5. Test your favicon: Save your HTML file and open it in a web browser. You should see your favicon displayed next to the website title in the browser tab. If it doesn't appear immediately, try clearing your browser cache and refreshing the page.


6. Optionally, add multiple favicon sizes: To support different devices and resolutions, you can add multiple <link> tags for different favicon sizes.

Favicon File Format Support

Favicons can be created in different file formats. The most commonly supported formats are:

  1. ICO (Icon File): The most widely used format for favicons. It can contain multiple sizes in a single file. This makes it easier to use one file for different screen sizes and resolutions.
     
  2. PNG (Portable Network Graphics): PNG is widely supported and is often used because it supports transparency and higher quality images.
     
  3. GIF (Graphics Interchange Format): While not commonly used for favicons, GIF can still be supported by most browsers.
     
  4. SVG (Scalable Vector Graphics): SVG is becoming more popular because it is resolution-independent and can scale to any size without losing quality. It is great for modern websites, but not all browsers support it fully for favicons.
     
  5. JPEG (Joint Photographic Experts Group): Though JPEG files are not commonly used for favicons due to their lack of transparency, they can be used in some cases.

Example of Using Different Formats for Favicons

Here’s how you can implement different favicon formats:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Website</title>
    <!-- Link to ICO Favicon -->
    <link rel="icon" href="favicon.ico" type="image/x-icon">
    
    <!-- Link to PNG Favicon -->
    <link rel="icon" href="favicon.png" type="image/png">

    <!-- Link to SVG Favicon -->
    <link rel="icon" href="favicon.svg" type="image/svg+xml">
</head>
<body>
    <h1>Welcome to My Website!</h1>
    <p>We've included different formats of favicons for compatibility across browsers.</p>
</body>
</html>


Output

Output

In this example, we link to different formats of favicons. The browser will use the first supported format it can find.

Frequently Asked Questions

What size should my favicon be for a website?

The most common size for a favicon is 16x16 pixels, but it's recommended to use multiple sizes, such as 32x32 pixels48x48 pixels, and 180x180 pixels, to ensure the favicon looks good on different devices and screen resolutions.

Can I use a PNG file as a favicon?

Yes, you can use a PNG file for your favicon. PNG is widely supported, and it allows for better quality and transparency compared to the ICO format.

How do I add a favicon to my website?

To add a favicon to your website, use the <link> tag in the <head> section of your HTML document, like this: <link rel="icon" href="favicon.ico" type="image/x-icon">

Conclusion

In this article, we covered the basics of implementing a favicon in HTML, including the common sizes and supported file formats. By adding a favicon, you make your website more recognizable and user-friendly. The examples provided demonstrate how to use various favicon sizes and formats to ensure compatibility across different devices and browsers. 

You can also check out our other blogs on Code360.

Live masterclass