HTML Favicon
29 March 2025 | Category: HTML
What Is an HTML Favicon?
A favicon (short for “favorite icon”) is a small icon displayed in the browser tab next to the website’s title. Favicons enhance the branding of a website and make it easily recognizable among multiple open tabs or bookmarks.
Adding a Favicon to Your Website
The <link>
tag inside the <head>
section of your HTML document is used to specify the favicon.
Basic Syntax:
<link rel="icon" href="favicon.ico" type="image/x-icon" />
Steps to Add a Favicon
- Prepare the Favicon Image:
- Create a square image (recommended size: 16x16px, 32x32px, or 48x48px).
- Save the image in ICO, PNG, SVG, or GIF format.
- Place the Favicon in Your Project:
- Place the favicon file in the root directory of your website (e.g.,
favicon.ico
).
- Place the favicon file in the root directory of your website (e.g.,
- Add the Favicon to the HTML:
<head> <link rel="icon" href="favicon.ico" type="image/x-icon" /> </head>
Examples of Favicon Integration
Example 1: Using a PNG File
<link rel="icon" href="favicon.png" type="image/png" />
Example 2: Using an SVG File
<link rel="icon" href="favicon.svg" type="image/svg+xml" />
Example 3: Alternate Sizes for High-Resolution Screens
You can provide multiple favicon sizes for better compatibility:
<link rel="icon" href="favicon-16x16.png" sizes="16x16" type="image/png" />
<link rel="icon" href="favicon-32x32.png" sizes="32x32" type="image/png" />
Fallback for Older Browsers
For older browsers that default to favicon.ico
:
- Name the favicon file
favicon.ico
. - Place it in the root directory. Browsers will automatically detect it without needing the
<link>
tag.
Favicon Best Practices
- Use Modern Formats:
- While
.ico
is widely supported, consider using modern formats like.png
or.svg
for better quality.
- While
- Test Your Favicon:
- Ensure it appears correctly in different browsers and devices.
- Compress the Favicon:
- Optimize the favicon to reduce file size and improve page load times.
- Brand Alignment:
- Use a simple design that aligns with your website or company branding.
Example of a Complete HTML Document with a Favicon
<!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 rel="icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<h1>Welcome to My Website</h1>
<p>This page includes a favicon!</p>
</body>
</html>