Adding a favicon to your HTML is a great way to make your website more recognizable and professional. A favicon, short for favorite icon, is the small icon associated with a website and displayed in the browser's address bar or next to the page's name in a user's list of bookmarks. Here's a step-by-step guide on how to add a favicon in HTML, as taught by W3Schools.

The first step is to create your favicon. You can use any graphics editor like Adobe Illustrator or online tools like Favicon-Generator.org to create a 16x16 pixel icon. Once you have your favicon, you're ready to add it to your HTML.

Using the <link> Element
The recommended way to add a favicon to your HTML is by using the <link> element. This method ensures that your favicon is displayed correctly across all browsers and devices.

Place the <link> tag in the <head> section of your HTML. Here's the basic syntax:
Basic Syntax

The basic syntax for the <link> tag to add a favicon is as follows:
The <link> tag has two attributes: rel and href. The rel attribute defines the relationship between the current document and the linked resource. In this case, it sets the relationship to icon. The href attribute specifies the location of the favicon. The location can be an absolute or relative URL.
For example, if your favicon is named 'favicon.ico' and is located in the root directory of your website, the path would be:

If your favicon is located in a subdirectory, such as 'img/' on your website, the path would be:
However, to ensure compatibility with all browsers and devices, it's recommended to provide multiple sizes of your favicon. This can be done by adding multiple <link> tags with different sizes.
Providing Multiple Sizes

Providing favicons in multiple sizes ensures that your icon will be displayed correctly across a wide range of devices and browsers. Here's how you can do it:
Different Sizes




















Here are the commonly used sizes for favicons: * 16x16 pixels - Standard size for most browsers and desktops. * 32x32 pixels - Used on Windows 10 and macOS. * 48x48 pixels - Large size for touch devices and high density displays. * 96x96 pixels - Extra large size for high density displays. * 196x196 pixels -Icon for Apple's iPad and iPhone with Retina display.
To provide these different sizes, simply add multiple <link> tags, each with a different size specified in the 'sizes' attribute. Here's an example:
In this example, each <link> tag is pointing to the same 'favicon.ico' file, but the 'sizes' attribute specifies the size of the icon. The 'type' attribute specifies the MIME type of the favicon, which is 'image/x-icon' for most favicon files.
Remember, it's not necessary to provide every size. Use these sizes as a guide, and provide the sizes that are most relevant to your website's users. Additionally, you can use tools like RealFaviconGenerator to generate multiple sizes of your favicon automatically.
Adding a favicon to your HTML is a small detail that can make a big difference in how your website is perceived. By following these steps, you'll ensure that your website's favicon is displayed correctly and consistently across all browsers and devices. Happy coding!