Skip to main content

Hover anchor links for Jekyll on GitHub Pages

3 min read

Summary: Add GitHub-style hover anchor links to your Jekyll headings with a few lines of CSS and JavaScript, so readers can deep-link to any section of your content. These days, the AnchorJS library does it for you.

Table of contents

If you’ve ever read a Markdown file on GitHub, you may have noticed that hovering over a heading produces a visible, clickable anchor link. This is incredibly useful to link someone to a particular heading or section, rather than the page as a whole, a practice known as “deep linking” of content.

When you send someone one of the resulting anchor URLs, which include the name of the linked heading, upon clicking the link, the user will automatically scroll to the desired part of the page, and thus be directed to the exact content you want them to see.

As long as you’re using Jekyll and authoring your content in Markdown, you can actually achieve this feature on GitHub Pages much more easily than you might expect:

Font Awesome and jQuery

First, you’ll want jQuery and Font Awesome included in your site template, if they aren’t already.

jQuery, a JavaScript library, helps you select all the headers programmatically, and Font Awesome, an icon library, provides the link icon that the user sees.

You can do this a handful of ways, but the easiest is to add the following in your template’s <head> section:

<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

The CSS

We need to tell the browser how to position the link icon, and to only display it when the visitor hovers over your heading. You’ll want to add the following to your site’s CSS file:

.header-link {
position: absolute;
left: -0.5em;
opacity: 0;
-webkit-transition: opacity 0.2s ease-in-out 0.1s;
-moz-transition: opacity 0.2s ease-in-out 0.1s;
-ms-transition: opacity 0.2s ease-in-out 0.1s;
}
h2:hover .header-link,
h3:hover .header-link,
h4:hover .header-link,
h5:hover .header-link,
h6:hover .header-link {
opacity: 1;
}

The JavaScript

Last, to tie everything together, you’ll also need a bit of JavaScript magic client-side, by adding the following to your site’s footer:

<script>
$(function() {
return $("h2, h3, h4, h5, h6").each(function(i, el) {
var $el, icon, id;
$el = $(el);
id = $el.attr('id');
icon = '<i class="fa fa-link"></i>';
if (id) {
return $el.prepend($("<a />").addClass("header-link").attr("href", "#" + id).html(icon));
}
});
});
</script>

And that’s it! You can see the result on this page if you hover over any heading. Click the link to update your URL bar, and you’ll have a shareable, deep-linked URL.

Happy deep linking!

Keep reading

Ben Balter

I'm Ben Balter, and I write here about developer platforms, product and engineering leadership, open source, and showing your work. I spent over a decade building developer platforms at GitHub. For five years I was a product manager for trust and safety, shipping 500+ features and the API endpoints behind them to 50M+ developers. I also led a bottom-up rewrite of GitHub Pages and wrote the open source library behind GitHub's license detection, and my open source projects have hundreds of millions of downloads. I wrote Open and Async, the playbook for remote and distributed teams, and shipped a Model Context Protocol (MCP) server alongside it so agents can query it directly. More about the author

Follow along: Bluesky LinkedIn

Originally published March 13, 2014 View revision history

This page is open source Help improve this article on GitHub

Keyboard shortcuts

Search

  • / or ⌘ K Open search
  • ↓ or j ↑ or k Move through results
  • Enter Open the selected result
  • Esc Close search

Go to

  • g then h Home
  • g then p All posts

This page

  • y Copy link to this page
  • ? Show this list