Complete HTML Favicon Code Examples

TL;DR

To add a favicon, upload a 32×32 (or 16×16) PNG and an ICO file to your site root and add <link rel="icon" href="/favicon.ico"> inside your <head>. For best results on every platform, also add a 16×16 and 32×32 PNG, an Apple touch icon and a web manifest — the snippet below does all of it.

Complete HTML Favicon Code Examples

Favicons, or favorite icons, are small images that represent your website in browser tabs, bookmarks, and other places. They play a crucial role in branding and user experience. In this section, we will explore complete HTML favicon code examples that web developers, designers, and website owners can easily implement to enhance their websites.

Understanding Favicons

Before diving into the code, let's clarify what a favicon is. It is typically a 16x16 or 32x32 pixel image file that appears next to the page title in the browser tab. While most modern browsers support various image formats for favicons, the most commonly used formats are .ico, .png, and .svg. Now, let’s look at how to properly include these favicons in your HTML.

Basic Favicon Code Example

To include a simple favicon in your website, you can use the following HTML code snippet. This example assumes you have a favicon image file named favicon.ico stored in your root directory:


It’s important to ensure that the href attribute points to the correct file location. If your favicon is stored in a different folder, simply adjust the path accordingly.

Using PNG Format for Favicons

For those who prefer a higher-quality image and transparency, a PNG format is an excellent choice. Below is an example of how to use a PNG favicon:


This code snippet should also be placed within the <head> section of your HTML document. Ensure that the path reflects the actual location of your favicon file.

Adding Multiple Favicon Sizes

To cater to various devices and resolutions, it’s a good practice to include multiple favicon sizes. Here is how you can do it:




By specifying the sizes attribute, you help browsers choose the most appropriate favicon size for the user's device.

Using SVG Format for Favicons

SVG favicons offer scalability and crispness across different screen sizes. Here’s how to include an SVG favicon:


SVG favicons are particularly beneficial for responsive designs, as they can be resized without losing quality.

Apple Touch Icons

For users on Apple devices, adding a touch icon enhances the experience when users save your website to their home screen. Include the following code to specify your touch icon:


Conclusion

Adding a favicon to your website is a simple yet effective way to improve branding and user engagement. By utilizing the code examples provided above, web developers and designers can easily implement favicons in various formats and sizes. Remember, a well-crafted favicon not only enhances your website's aesthetic but also helps in creating a memorable user experience.