Explain Codes LogoExplain Codes Logo

Adding a favicon to a static HTML page

html
responsive-design
best-practices
web-development
Alex KataevbyAlex Kataev·Jan 28, 2025
TLDR

Efficiently embed a favicon by dropping this tag into your HTML's <head>:

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

Confirm that favicon.ico is at the root (/) and it's either 16x16 or 32x32 pixels for universal support. If it's not appearing, empty your browser cache or give incognito mode a go.

Selecting the optimal image format

Historically, .ico files were the standard. But, guess what? Times have changed! PNG and SVG formats are now supported by modern browsers. Select PNG for more muscle in the quality department:

<!-- Even your grandma will notice the improved quality --> <link rel="icon" type="image/png" href="/favicon.png">

For the sleek combination of scalability and sharp detail at any size, SVG is your rockstar:

<!-- Scalable Vector Graphics - We're living in the future, folks! --> <link rel="icon" type="image/svg+xml" href="/favicon.svg">

Location considerations with host

If your favicon is throwing a party on an external server, use an absolute URL:

<!-- You must invite absolute URL to the party --> <link rel="icon" type="image/png" href="https://example.com/favicon.png">

For local heroes, keep your favicon in the same neighborhood as your HTML files. You know, to avoid missing the party.

Riding The Dynamic favicons Wave

Dynamic favicons change outfits based on the occasion, think new notifications. They're the Kardashians of favicons. Use JavaScript to modify the href dynamically:

<!-- Dynamic favicons: Being a chameleon was never so easy --> document.querySelector("link[rel='icon']").setAttribute('href', '/new-favicon.ico');

Broadening the Scope with Advanced Favicon Techniques

Serving Every Guest With Multiple Sizes

Customize your icons for different devices, because one size doesn't fit all:

<!-- Be the host with the most to offer --> <link rel="icon" sizes="16x16 32x32 48x48" href="/favicon.ico">

To cover all your bases, generate icons for every platform with faviconit.com or realfavicongenerator.net.

Being the Browser Whisperer

Different browsers, different mannerisms. This is the law of the browser jungle. Ensure your favicon gets along with everyone by testing in multiple browsers. .gif or .png can be your peace offerings.

Embedded favicons Saving The Day

Embedded favicons are like superheroes, they save the day by reducing HTTP requests:

<!-- Favicons to the rescue --> <link rel="icon" href="data:image/x-icon;base64,DATA_HERE">

Perform this hero act by converting your images to Base64 with online converters.

Favicons - The Animated Series

Animated GIFs for favicons? Absolutely!

<!-- Bring some sitcom into life --> <link rel="icon" type="image/gif" href="/favicon.gif">

Remember: It's fun, but not all browsers are party animals.