What would the web be without links? Well, not much of a web at all. Mozilla defines the World Wide Web as:
“An interconnected system of public web pages accessible through the Internet.”
And how are those pages connected? Hyperlinks, or links from one document to another, usually appearing as hypertext. In fact, the “H” in HTTP/HTTPS stands “HyperText”. Simply put, there’s no web without links.
We all know hyperlinks when we see them, whether we’re perusing an online store or going down a Wikipedia rabbit hole. In essence, browsing the web is just clicking link after link.
So, in this guide, we’ll be peeking under the hood to see what these links look like in HTML. To write links into your web pages, you’ll need the <a> element, paired with the href attribute — let’s learn how to use both.
What is the HTML a href attribute?
In HTML, the inline a (anchor) element denotes a hyperlink from one web address to another. All functional a elements must contain the href (hypertext reference) attribute inside the opening a tag. The href attribute indicates the destination of the hyperlink. Without the href attribute, the a element won’t work.
You’ve already met one instance of href in the above introduction when I linked to the definition of the World Wide Web. The HTML for that hyperlink looks like this:
<a href="https://developer.mozilla.org/en-US/docs/Glossary/World_Wide_Web" rel="noopener" target="_blank" >Mozilla defines the World Wide Web</a>
There are several things to note here, all important for the link to work:
- The link text is wrapped in an opening <a> tag and a closing </a> tag. All text between these tags is selectable and receives link styling.
- The href attribute, and its accompanying attributes, are placed inside the opening <a> tag.
- The attribute value of href (inside the quotation marks) is a URL that tells the browser where to go when the link is selected.
- Note the additional attributes target=“_blank” and rel=“noopener” — these tell the browser to open the web page in a new tab. Unlike href, these attributes are not necessary.
To be more precise, the link above uses an absolute URL to specify a destination on a different website. However, href can take on other values too, as we’ll see next.
a href Values
You’ll most often see the href attribute used to link to another web page on the same domain or a different domain. href can also be used for other things, like linking to a different part of the same page, or serving a different web protocol than HTTPS. Let’s review each of these possible values with examples.
As discussed above, an absolute URL points to a file on another website. In other words, it’s a URL that contains a different domain name from the current page’s URL (e.g. hubspot.com to mozilla.org).
Here’s another example: Go to Google search.
This hyperlink also contains an absolute URL:
<p>Here’s another example: <a href="https://www.google.com/">Go to Google search</a>.</p>
It doesn’t matter where this link is used — it always points to the same place, the Google homepage. That’s why it’s called an “absolute” URL.
Bonus tip: Notice how the link text describes what the link does. This makes your site more accessible to those using assistive technologies.
A relative URL references a file on the same website. For example, here’s a link to our Ultimate Guide to HTML.
And here’s what the code for that looks like:
<p>A relative URL references a file on the same website. For example, here’s a link to our <a href="/website/html">Ultimate Guide to HTML</a>.</p>
See how the href value looks different? It doesn’t include the protocol HTTPS or the domain name. When this part of the URL is left out, the browser assumes that the specified file is located on the same domain as the current page (in this case, hubspot.com).
We call these URLs “relative” because the destination of the link is specified relative to the current web page. If we pasted the HTML above on a page on a different website, it wouldn’t work.
Why use relative URLs in lieu of absolute URLs? The answer is that relative URLs come in handy if the file path of the linked file changes, such as in the case of a website restructuring or a domain change. If this happens, the relative URL will still work, since it only contains the name of the linked resource and not the entire file path.
Use a URI fragment to link to a different part of the same web page. URI fragments begin with the hash (#) symbol, followed by the id of the desired page element.
For example, this link directs you to the H3 above this one.
<p>For example, <a href="#relative-url">this link directs you to the H3 above this one</a>.</p>
You can also use the value #top to send visitors back to the top of the page.
<p>You can also use the value <strong>#top</strong> to <a href="#top">send visitors back to the top of the page</a>.</p>
URI fragments are useful for chapter links, or any instance in which users can jump to an earlier/later section of a page.
The absolute URLs and relative URLs we’ve seen so far use the HTTP/HTTPS protocol, which serves pages on the World Wide Web. However, we can use other protocols with href too, like mailto: and file:.
For example, click this link to send an email to firstname.lastname@example.org (though we can’t guarantee you’ll hear back).
<p>For example, click this link to send an email to <a href="mailto:email@example.com">firstname.lastname@example.org</a> (though we can’t guarantee you’ll hear back).</p>
href: Creating Links on the Web
If you’re anything like me, you spend all day clicking various links to get around the web. And if you’re just learning HTML, the <a> element and its accompanying href attribute are central to understanding how web pages are tied together. By learning these concepts and how to write them, you’re a step closer to building websites on your own.