What does any blog author with two posts need? "More content!" I hear you shout, to which I say - not today!
I have decided to add - drum roll, please - some anchor links to the headings!
Okay, I lied, I did indeed decide to make some content around it, since the last time I posted a blog post... Well, let's just say it's been a long time to avoid embarrassment.
So what are anchor links, anyway? If you're unfamiliar, anchor links are basically links to specific sections of a page.
Try hovering over one of the headings in this page, and clicking on the icon next to it - it will append a
How this works is not magic - the ID of the heading should match the heading "slug"; In this example, the "Adding the Anchor"
Try hovering over one of the headings in this page, and clicking on the icon next to it - it will append a
# with the heading
(e.g. #adding-the-anchor) and will scroll the page so that it focuses on the heading. If you navigate to this URL, it will also focus on the heading.How this works is not magic - the ID of the heading should match the heading "slug"; In this example, the "Adding the Anchor"
<h2> heading has an ID of adding-the-anchor.
Try inspecting the HTML and seeing for yourself!Note: If you link to a page's heading (for example, in technical documentation), bear in mind that the heading can change (which may "break" your link and lead folks to the top of the page rather than a specific section) but web developers are aware of this functionality and may not change the ID to avoid that.
Text Fragments are also useful for this kind of stuff (but are much more prone to breaking).
So, let's begin with the simplest heading and see how you would go about doing something like this:
<h2>Adding the Anchor</h2>
The actual code I use to render headings is omitted for simplicity (and accounts for various level of heading tags).
Let's break this down into three parts:
- Add the anchor to the heading
- Display it only on hover
- Add the actual anchor link functionality



