Links

Links are one of the most basic, yet deeply fundamental and foundational building blocks of the web. Click a link, and you move to another page or are moved to another place within the same page.

A basic link

<a href="https://css-tricks.com">CSS-Tricks</a>

That’s a link to a “fully qualified” or “absolute” URL.

A relative link

You can link “relatively” as well:

<!-- Useful in navigation, but be careful in content that may travel elsewhere (e.g. RSS) --> <a href="/pages/about.html">About</a>

That can be useful, for example, in development where the domain name is likely to be different than the production site, but you still want to be able to click links. Relative URLs are most useful for things like navigation, but be careful of using them within content — like blog posts — where that content may be read off-site, like in an app or RSS feed.

A jump link

Links can also be “hash links” or “jump links” by starting with a # :

<a href="#section-2">Section Two</a> <!-- will jump to... --> <section id="section-2"></section>

Clicking that link will “jump” (scroll) to the first element in the DOM with an ID that matches, like the section element above.

💥 Little trick: Using a hash link (e.g. #0 ) in development can be useful so you can click the link without being sent back to the top of the page like a click on a # link does. But careful, links that don’t link anywhere should never make it to production.

💥 Little trick: Jump-links can sometimes benefit from smooth scrolling to help people understand that the page is moving from one place to another.

It’s a fairly common UI/UX thing to see a “Back to top” link on sites, particularly where important navigational controls are at the top but there is quite a bit of content to scroll (or otherwise navigate) through. To create a jump link, link to the ID of an element that is at the top of the page where it makes sense to send focus back to.

<a href="#top-of-page">Back to Top</a>

Jump links are sometimes also used to link to other anchor ( <a> ) elements that have no href attribute. Those are called “placeholder” links:

<a id="section-2"></a> <h3>Section 2</h3>

There are accessibility considerations of these, but overall they are acceptable.

Disabled links

A link without an href attribute is the only practical way to disable a link. Why disable a link? Perhaps it’s a link that only becomes active after logging in or signing up.

a:not[href] { /* style a "disabled" link */ }

When a link has no href , it has no role, no focusability, and no keyboard events. This is intentional. You could think of it like a <span> .

Do you need the link to open in a new window or tab?

You can use the target attribute for that, but it is strongly discouraged.

<a href="https://css-tricks.com" target="_blank" rel="noopener noreferrer"> CSS-Tricks </a>

The bit that makes it work is target="_blank" , but note the extra rel attribute and values there which make it safer and faster.

Making links open in new tabs is a major UX discussion. We have a whole article about when to use it here. Summarized:

Don’t use it:

Because you or your client prefer it personally.

Because you’re trying to beef up your time on site metric.

Because you’re distinguishing between internal and external links or content types.

Because it’s your way out of dealing with infinite scroll trickiness.

Do use it:

Because a user is doing something on the current page, like actively playing media or has unsaved work.

You have some obscure technical reason where you are forced to (even then you’re still probably the rule, not the exception).

Need the link to trigger a download?

The download attribute on a link will instruct the browser to download the linked file rather than opening it within the current page/tab. It’s a nice UX touch.

<a href="/files/file.pdf" download>Download PDF</a>

The rel attribute

This attribute is for the relationship of the link to the target.

The rel attribute is also commonly used on the <link> element (which is not used for creating hyperlinks, but for things like including CSS and preloading). We’re not including rel values for the <link> element here, just anchor links.

Here are some basic examples:

<a href="/page/3" rel="next">Next</a> <a href="/page/1" rel="prev">Previous</a> <a href="http://creativecommons.org/licenses/by/2.0/" rel="license">cc by 2.0</a> <a href="/topics/" rel="directory">All Topics</a>

rel="alternate" : Alternate version of the document.

: Alternate version of the document. rel="author" : Author of the document.

: Author of the document. rel="help" : A resource for help with the document.

: A resource for help with the document. rel="license" : License and legal information.

: License and legal information. rel="manifest" : Web App Manifest document.

: Web App Manifest document. rel="next" : Next document in the series.

: Next document in the series. rel="prev" : Previous document in the series.

: Previous document in the series. rel="search" : A document meant to perform a search in the current document.

There are also some rel attributes specifically to inform search engines:

rel="sponsored" : Mark links that are advertisements or paid placements (commonly called paid links) as sponsored.

: Mark links that are advertisements or paid placements (commonly called paid links) as sponsored. rel="ugc" : For not-particularly-trusted user-generated content, like comments and forum posts.

: For not-particularly-trusted user-generated content, like comments and forum posts. rel="nofollow" : Tell the search engine to ignore this and not associate this site with where this links to.

And also some rel attributes that are most security-focused:

rel="noopener" : Prevent a new tab from using the JavaScript window.opener feature, which could potentially access the page containing the link (your site) to perform malicious things, like stealing information or sharing infected code. Using this with target="_blank" is often a good idea.

: Prevent a new tab from using the JavaScript feature, which could potentially access the page containing the link (your site) to perform malicious things, like stealing information or sharing infected code. Using this with is often a good idea. rel="noreferrer" : Prevent other sites or tracking services (e.g. Google Analytics) from identifying your page as the source of clicked link.

You can use multiple space-separated values if you need to (e.g. rel="noopener noreferrer" )

And finally, some rel attributes come from the microformats standard or the indieweb like:

rel="directory" : Indicates that the destination of the hyperlink is a directory listing containing an entry for the current page.

: Indicates that the destination of the hyperlink is a directory listing containing an entry for the current page. rel="tag" : Indicates that the destination of that hyperlink is an author-designated “tag” (or keyword/subject) for the current page.

: Indicates that the destination of that hyperlink is an author-designated “tag” (or keyword/subject) for the current page. rel="payment" : Indicates that the destination of that hyperlink provides a way to show or give support for the current page.

: Indicates that the destination of that hyperlink provides a way to show or give support for the current page. rel="help" : States that the resource linked to is a help file or FAQ for the current document.

: States that the resource linked to is a help file or FAQ for the current document. rel="me" : Indicates that its destination represents the same person or entity as the current page.

ARIA roles

The default role of a link is link , so you do not need to do:

<a role="link" href="/">Link</a>

You’d only need that if you were faking a link, which would be a weird/rare thing to ever need to do, and you’d have to use some JavaScript in addition to this to make it actually follow the link.

<span class="link" tabindex="0" role="link" data-href="/"> Fake accessible link created using a span </span>

Just looking above you can see how much extra work faking a link is, and that is before you consider that is breaks right-clicking, doesn’t allow opening in a new tab, doesn’t work with Windows High Contrast Mode and other reader modes and assistive technology. Pretty bad!

A useful ARIA role to indicate the current page, like:

<a href="/" aria-current="page">Home</a> <a href="/contact">Contact</a> <a href="/about">About/a></a>

Should you use the title attribute?

Probably not. Save this for giving an iframe a short, descriptive title.

<a title="I don't need to be here" href="/"> List of Concerts </a>

title provides a hover-triggered UI popup showing the text you wrote. You can’t style it, and it’s not really that accessible.

Hover-triggered is the key phrase here. It’s unusable on any touch-only device. If a link needs more contextual information, provide that in actual content around the link, or use descriptive text the link itself (as opposed to something like “Click Here”).

Icon-only links

If a link only has an icon inside it, like:

<a href="/">😃</a> <a href="/"> <svg> ... </svg> </a>

That isn’t enough contextual information about the link, particularly for accessibility reasons, but potentially for anybody. Links with text are almost always more clear. If you absolutely can’t use text, you can use a pattern like:

<a href="/"> <!-- Hide the icon from assistive technology --> <svg aria-hidden="true" focusable="false"> ... </svg> <!-- Acts as a label that is hidden from view --> <span class="visually-hidden">Useful link text</span> </a>

visually-hidden is a class used to visually hide the label text with CSS:

.visually-hidden { border: 0; clip: rect(0 0 0 0); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; white-space: nowrap; width: 1px; }

Unlike aria-label , visually hidden text can be translated and will hold up better in specialized browsing modes.

Links around images

Images can be links if you wrap them in a link. There is no need to use the alt text to say the image is a link, as assistive technology will do that already.

<a href="/buy/puppies/now"> <img src="puppy.jpg" alt="A happy puppy."> </a>

Links around bigger chunks of content

You can link a whole area of content, like:

<a href="/article/"> <div class="card"> <h2>Card</h2> <img src="..." alt="..."> <p>Content</p> </div> </a>

But of course, there are UX implications. For example, it can be harder to select the text, and the entire element needs fairly complex styling to create clear focus and hover states. There are also accessibility implications, like the fact that the content of the entire card is read before it is announced as a link.

Here’s an example with two approaches. The first wraps the entire card in a link. This is valid, but remember the implications. The second has a link inside the title, and the link has a pseudo-element on it that covers the entire card. This also has implications (a bit awkward to select text, for example), but is perhaps more expected for assistive technology.

CodePen Embed Fallback

The second example also opens up the possibility of including multiple links. You can’t nest links, so things get a little tricky if you need to. It’s possible though, by making the individual links set above the card-covering link with z-index.

Here’s the default look of a link:

The default User-Agent styling of a link.

It’s likely you’ll be changing the style of your links, and also likely you’ll use CSS to do it. I could make all my links red in CSS by doing:

a { color: red; }

Sometimes selecting and styling all links on a page is a bit heavy-handed, as links in navigation might be treated entirely differently than links within text. You can always scope selectors to target links within particular areas like:

/* Navigation links */ nav a { } /* Links in an article */ article a { } /* Links contained in an element with a "text" class */ .text a { }

Or select the link directly to style.

.link { /* For styling <a class="link" href="/"> */ } a[aria-current="page"] { /* You'll need to apply this attribute yourself, but it's a great pattern to use for active navigation. */ }

Link states

Links are focusable elements. In other words, they can be selected using the Tab key on a keyboard. Links are perhaps the most common element where you’ll very consciously design the different states, including a :focus state.

:hover : For styling when a mouse pointer is over the link.

: For styling when a mouse pointer is over the link. :visited : For styling when the link has been followed, as best as the browser can remember. It has limited styling ability due to security.

: For styling when the link has been followed, as best as the browser can remember. It has limited styling ability due to security. :link : For styling when a link has not been visited.

: For styling when a link has not been visited. :active : For styling when the link is pressed (e.g. the mouse button is down or the element is being tapped on a touch screen).

: For styling when the link is pressed (e.g. the mouse button is down or the element is being tapped on a touch screen). :focus : Very important! Links should always have a focus style. If you choose to remove the default blue outline that most browsers apply, also use this selector to re-apply a visually obvious focus style.

These are chainable like any pseudo-class, so you could do something like this if it is useful for your design/UX.

/* Style focus and hover states in a single ruleset */ a:focus:hover { }

You can style a link to look button-like

Perhaps some of the confusion between links and buttons is stuff like this:

Very cool “button” style from Katherine Kato.

That certainly looks like a button! Everyone would call that a button. Even a design system would likely call that a button and perhaps have a class like .button { } . But! A thing you can click that says “Learn More” is very much a link, not a button. That’s completely fine, it’s just yet another reminder to use the semantically and functionally correct element.

Color contrast

Since we often style links with a distinct color, it’s important to use a color with sufficient color contrast for accessibility. There is a wide variety of visual impairments (see the tool WhoCanUse for simulating color combinations with different impairments) and high contrast helps nearly all of them.

Perhaps you set a blue color for links:

The blue link is #2196F3.

While that might look OK to you, it’s better to use tools for testing to ensure the color has a strong enough ratio according to researched guidelines. Here, I’ll look at Chrome DevTools and it will tell me this color is not compliant in that it doesn’t have enough contrast with the background color behind it.

Chrome DevTools is telling us this link color does not have enough contrast.

Color contrast is a big consideration with links, not just because they are often colored in a unique color that needs to be checked, but because they have all those different states (hover, focus, active, visited) which also might have different colors. Compound that with the fact that text can be selected and you’ve got a lot of places to consider contrast. Here’s an article about all that.

Styling “types” of links

We can get clever in CSS with attribute selectors and figure out what kind of resource a link is pointing to, assuming the href value has useful stuff in it.

/* Style all links that include .pdf at the end */ a[href$=".pdf"]::after { content: " (PDF)"; } /* Style links that point to Google */ a[href*="google.com"] { color: purple; }

Styling links for print

CSS has an “at-rule” for declaring styles that only take effect on printed media (e.g. printing out a web page). You can include them in any CSS like this:

@media print { /* For links in content, visually display the link */ article a::after { content: " (" attr(href) ")"; } }

Resetting styles

If you needed to take all the styling off a link (or really any other element for that matter), CSS provides a way to remove all the styles using the all property.

.special-area a { all: unset; all: revert; /* Start from scratch */ color: purple; }

You can also remove individual styles with keywords. (Again, this isn’t really unique to links, but is generically useful):

a { /* Grab color from nearest parent that sets it */ color: inherit; /* Wipe out style (turn black) */ color: initial; /* Change back to User Agent style (blue) */ color: revert; }

Say you wanted to stop the clicking of a link from doing what it normally does: go to that link or jump around the page. In JavaScript, you can use preventDefault to prevent jumping around.

const jumpLinks = document.querySelectorAll("a[href^='#']"); jumpLinks.forEach(link => { link.addEventListener('click', event => { event.preventDefault(); // Do something else instead, like handle the navigation behavior yourself }); });

This kind of thing is at the core of how “Single Page Apps” (SPAs) work. They intercept the clicks so browsers don’t take over and handle the navigation.

SPAs see where you are trying to go (within your own site), load the data they need, replace what they need to on the page, and update the URL. It’s an awful lot of work to replicate what the browser does for free, but you get the ability to do things like animate between pages.

Another JavaScript concern with links is that, when a link to another page is clicked, the page is left and another page loads. That can be problematic for something like a page that contains a form the user is filling out but hasn’t completed. If they click the link and leave the page, they lose their work! Your only opportunity to prevent the user from leaving is by using the beforeunload event.

window.addEventListener("beforeunload", function(event) { // Remind user to save their work or whatever. });

A link that has had its default behavior removed won’t announce the new destination. This means a person using assistive technology may not know where they wound up. You’ll have to do things like update the page’s title and move focus back up to the top of the document.

JavaScript frameworks

In a JavaScript framework, like React, you might sometimes see links created from something like a <Link /> component rather than a native <a> element. The custom component probably creates a native <a> element, but with extra functionality, like enabling the JavaScript router to work, and adding attributes like aria-current="page" as needed, which is a good thing!

Ultimately, a link is a link. A JavaScript framework might offer or encourage some level of abstraction, but you’re always free to use regular links.

We covered some accessibility in the sections above (it’s all related!), but here are some more things to think about.

You don’t need text like “Link” or “Go to” in the link text itself. Make the text meaningful (“documentation” instead of “click here”).

Links already have an ARIA role by default ( role="link" ) so there’s no need to explicitly set it.

) so there’s no need to explicitly set it. Try not to use the URL itself as the text ( <a href="google.com">google.com</a> )

) Links are generally blue and generally underlined and that’s generally good.

All images in content should have alt text anyway, but doubly so when the image is wrapped in a link with otherwise no text.

Unique accessible names

Some assistive technology can create lists of interactive elements on the page. Imagine a group of four article cards that all have a “Read More”, the list of interactive elements will be like:

Read More

Read More

Read More

Read More

Not very useful. You could make use of that .visually-hidden class we covered to make the links more like:

<a href="/article"> Read More <span class="visually-hidden"> of the article "Dancing with Rabbits". <span> </a>

Now each link is unique and clear. If the design can support it, do it without the visually hidden class to remove the ambiguity for everyone.