Skip to main content

Header hover anchor links on GitHub Pages using Jekyll

4 min read
: A quick guide to adding GitHub-style hover anchor links to your Jekyll headings with CSS and JavaScript, so readers can deep-link to any section of your content.
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 includes 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 easier 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.

There are a handful of ways to do this, 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!

Originally published March 13, 2014 View revision history
Share

More to explore

Helpful 404s for Jekyll (and GitHub Pages)

3 min read

How to build 404 pages for Jekyll and GitHub Pages that automatically suggest similar URLs to those requested, using Levenshtein distance and your sitemap.

Jekyll: Where content is truly king

2 min read

Choosing Jekyll over a traditional CMS for government.github.com freed us to spend six months iterating on what mattered most — the content.

Welcome to the Post-CMS World

7 min read

Jekyll (and other static-sites) lead to simple, flexible, and reliable websites that allow for a renewed focus on what actually matters: the content.

Test your content

4 min read

Treat content as code and unlock CI for prose. Use HTML Proofer and Travis CI to automatically test every link, image, and change.

Intro to GitHub for non-technical roles

10 min read

GitHub isn't just for developers. A practical guide for non-technical roles to follow along, collaborate, and track work with confidence.

Ben Balter

I'm Ben Balter — I write here about engineering leadership, open source, and showing your work. By day I'm Director of Hubber Enablement at GitHub, where I help thousands of GitHubbers do their best remote work. Before this role: Chief of Staff for Security, enterprise PM, and GitHub's first Government Evangelist. Before GitHub: attorney, Presidential Innovation Fellow, and member of the White House's first agile development team. More about the author →

Follow along: Bluesky LinkedIn

This page is open source Help improve this article on GitHub