Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add favicon in HTML

Understanding Favicon

Let's start by understanding what a favicon is and why it's important. A favicon, short for "favorite icon", is a small square image or logo that appears next to a web page's title in the browser tab. It's like a little business card for your site. If you have multiple tabs open, the favicon makes it easy to recognize and navigate to your site. Think of it like the emblem on the spine of a book, which makes it easy to find the book you want on a packed bookshelf.

How Favicon Works

Before we dive into how to add a favicon to an HTML page, let's get a basic understanding of how it works. When you visit a website, your web browser checks for a favicon. It does this by trying to access the favicon file in the root directory of the website. If it finds this file, it displays the favicon in the browser tab. If it doesn't find it, it just displays a default icon.

Adding a Favicon to Your HTML Page

To add a favicon to your HTML page, you first need to have a favicon image. This is typically a .png or .ico file, and it's usually 16x16 pixels or 32x32 pixels in size.

Here's an example of what the code might look like:

<link rel="icon" href="favicon.ico" type="image/x-icon">

In this line of code, the rel attribute specifies the relationship between the current document and the linked document. In this case, the linked document is an icon. The href attribute points to the location of the favicon file.

Creating Your Favicon

Creating a favicon can be as simple or as complex as you'd like. You can create your own using any graphic design software, or you can use a favicon generator online. The key is to make sure the image represents your brand and is clear and recognizable at a small size.

Once you've created your favicon, save it in the root directory of your website. This is generally the same place where your index.html file is located.

Implementing the Favicon in Your HTML

To implement the favicon in your HTML, you'll need to add a link element to the head of your HTML document. The link should point to the location of your favicon file. Here's an example:

<!DOCTYPE html>
<html>
<head>
    <title>My Website</title>
    <link rel="icon" href="favicon.ico" type="image/x-icon">
</head>
<body>
    <!-- Your content goes here -->
</body>
</html>

In this example, the favicon.ico file is located in the same directory as the HTML file. If your favicon file was in a different location, you would need to adjust the href attribute accordingly. For instance, if your favicon was in an images folder, your href might look like this: href="images/favicon.ico".

Testing Your Favicon

After you've added the favicon link to your HTML, it's time to test to make sure it's working. Save and close your HTML file, then open it in your web browser. You should see your favicon appear in the browser tab.

If you don't see your favicon, double-check your code and the location of your favicon file. If everything seems correct, try clearing your browser's cache. The browser may still be displaying an old favicon from its cache.

Final Thoughts

Favicons are a small but important part of your website. They help brand your site and make it easy for users to find your site among their open tabs. While this guide has shown you how to add a basic favicon, there's a lot more you can do with favicons, including creating different icons for different devices and resolutions. But for those just starting out in programming, adding a basic favicon is a great first step.