Unable to set favicon using Jekyll and github pages
Asked Answered
S

10

69

I am trying to set a favicon.ico for my github page, but it doesn't work. When I serve it locally I see the standard "empty" favicon and when I push it I see the facebook icon. Why is it so? I have the right favicon.ico in the root directory of my project and I added the line

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

to the relevant default.html. You can see the sources here: https://github.com/drorata/drorata.github.io

Schema answered 30/5, 2015 at 21:20 Comment(0)
T
114

I cloned your project from GitHub to take a look at it. After serving it using Jekyll, the favicon did not display, as you noted.

I did some quick testing by converting the favicon file to be a .png rather than a .ico file and changing the favicon declaration to the following, and that was able to get it to display the favicon.

<link rel="shortcut icon" type="image/png" href="/favicon.png">

I tried getting the favicon to work while keeping the .ico file format, and was unable to do so at first. However, I did some quick searching and came across this question, favicon not displayed by Firefox.

In that question the asker had a similar issue with the favicon not showing, and was eventually able to come up with a quick fix by adding a ? to the end of the link to the favicon file in the favicon declaration. I attempted this and it worked. Here is what the favicon declaration would be:

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

Either of those two methods seem to be able to fix your issue. Personally I'd recommend using the first method, whereby you convert the image to a .png file, as it seems a bit simpler and less hacky.

However, if you want to keep the file as a .ico file then you should read over the question that I linked to before you attempt the second method, as the accepted answer for the question differed from that solution. Also I'm not sure as to why the second method works, and it does seem a little bit hacky.

Torpid answered 30/5, 2015 at 23:22 Comment(1)
also <link rel="shortcut icon" type="image/png" href="/favicon.png?"> working for meArithmetic
E
18

I believe, currently, the correct way to do this is:

<link rel="shortcut icon" type="image/png" href="{{ "/favicon.png" | prepend: site.baseurl }}" >

Assuming you are using a png. The following also worked for me with a .ico instead of .png:

<link rel="shortcut icon" type="image/x-icon" href="{{ "/favicon.ico" | prepend: site.baseurl }}" >

I was working with Chrome on Linux. Neither Excalibur Zero's answer or Ribtoks answer worked for me.

Euraeurasia answered 18/12, 2015 at 3:46 Comment(0)
W
8

Quick solution

Leave the slash out to make the href address relative.

For example:

Change

<link rel="shortcut icon" type="image/png" href="/favicon.png">

to:

<link rel="shortcut icon" type="image/png" href="favicon.png">

In my github pages site this works perfectly.

Explanation

Use my site https://hustlion.github.io/spanish-cards/ as an example:

When you use <link rel="shortcut icon" type="image/png" href="/favicon.png">, the icon address will be https://hustlion.github.io/favicon.png, because the root of the site (as specified by the slash in /favicon.png) is https://hustlion.github.io/.

However, when you use <link rel="shortcut icon" type="image/png" href="favicon.png">, this is relative to the path https://hustlion.github.io/spanish-cards/, so the icon address will be resolved properly as https://hustlion.github.io/spanish-cards/favicon.png.

Notes

This path issue happens especially when you are using github pages for your specific repository.

Walcott answered 5/8, 2016 at 6:27 Comment(1)
I'm not sure why this was downvoted, but it's the best explanation of what's going on. Alternatively, you can add the dot before the slash like so: href="./favicon.png"Pelf
E
4

I use

<link rel="shortcut icon" type="image/x-icon" href="{{ site.baseurl }}/images/favicon.ico" >

And I have favicon in folder images.

Earsplitting answered 7/3, 2016 at 21:41 Comment(0)
W
4

according to documentation:

1) put favicon.ico file into assets/images folder of jekyll project as assets/images/favicon.ico

2) create a _includes/head_custom.html file if not yet exists

3) add needed overriding content:

<link rel="shortcut icon" type="image/x-icon" href="{{ site.baseurl }}/assets/images/favicon.ico">

Done.

Waterline answered 1/9, 2019 at 3:50 Comment(0)
S
3

Nothing above worked for me, but the steps in this vid (assuming the minima theme) did.

1) Add _includes directory to your project root

  • Terminal: find _includes/head.html by typing bundle show minima
  • Copy _includes/head.html from finder into your project root

2) Modify _includes/head.html to include favicon link

  • The following works for me: <link rel="shortcut icon" type="image/png" href="/favicon.png">
  • Important: the / in front of /favicon.png matters. Without the /, your website root will have your favicon but no other endpoints will.

3) Add the jekyll-seo-tag plugin to your _config.yml. It should look something like this:

# Build settings
markdown: kramdown
theme: minima
plugins:
  - jekyll-feed
  - jekyll-seo-tag
Szabadka answered 28/10, 2018 at 21:5 Comment(0)
A
2

I got it to working using:

<!-- Add favicon -->
<link rel="shortcut icon" 
      type="image/png" 
      href="{{ '/favicon.png' | relative_url }}" >

Notes on the snippet:

  1. PNG format for the favicon,
  2. a relative URL in the HTML head tag (in minimia this is head.html).
  3. Adding relative_url tells Liquid to prepend the url and baseurl to the given path.
Actually answered 30/7, 2018 at 17:14 Comment(1)
It works for me, as well with .ico as .png, but on Firefox only.Aquarius
J
1

Just in case someone will be looking for this. Both approaches didn't work for me. But when I appended the site.url, it worked

<link rel="shortcut icon" type="image/png" href="{{site.url}}/favicon.png">
Julio answered 6/9, 2015 at 16:26 Comment(1)
Perhaps this is because your site field included a base url? Generally you have a site and a base url. www.example-site.com/example-base/ <- your site.url probably looked like thisInterpretative
L
0

In my case, I had to add the favicon.ico file to the assets folder and reference it as follows:

<link rel="shortcut icon" type="image/x-icon" href="assets/favicon.ico">
Linger answered 10/5, 2019 at 0:20 Comment(0)
N
0

I couldn't get the images to display and I use markdown for my posts, appending what worked for me so that others may benefit.

![Description of the image]({{ site.baseurl }}/assets/images/image.png)

This works both locally and on github pages as well

Necklace answered 8/5, 2021 at 20:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.