Skip to main content

How to use anchor links

Introduction

Anchor links are hyperlinks that allow you to link to specific sections on a URL.  We're using them on this page, in the CTA links at the top of this page.  In AEM, you can apply anchor links to any hyperlink, button, or CTA. 

Let's look at how to author an anchor link.

Authoring

An anchor link is configured through two elements:

  • Label: To set up an anchor link you must first add a label to the title of the section you want to link to.
  • Link: Setting up anchor links require some customization to insert the label to your URL.

Adding Labels to Titles

Add a Unique Label

If you are using multiple anchor links on a page, it's important that you take care to give each section a unique label.  Anchor tags will not work if there is more than one label with the same name on a page. 

Adding the Label to a Title

The best practice is to add the label to the Title component of the section you want to link to. This will help you quickly find labels when editing at a later time. 

Once you've planned out your labels for the page, adding it to the title is easy. Just insert your label, in all lowercase, into the ID field of the Title component, as shown here in the image below.

Once your Titles have their ID Labels in place, it's time to build your links.  As mentioned, you can apply the anchor link to any CTA, hyperlink, or button by authoring as usual. 

If adding a hyperlink to the page that you are editing, then when you add your URL, use the AEM browser to pick the page that you are currently editing.  Once you select the page that path will look something like this:

Before you SAVE the hyperlink, you need to adjust this URL by doing the following:

  1. add ".html" to the URL path that you selected
  2. Then, add a # symbol followed by the label you want to link to. If I want to link to a section with the ID Label of "examples" I would add "#examples" to the end of my URL, after the ".html" that I added previously. 

Here's how your new URL should look after editing:

It's that easy!  Save your link, publish your page, and now your hyperlinks should link to the precise section of the page where you want to direct your audience. 

Examples

We've set up anchor links at the top of this page where you can try out the anchor links. 

Please note, anchor links can work within a page, buy you can also use them across pages by adding a label to the title ID of one page, but adding the actual anchor link to a button on a different page. Give it a try!