How to Use a href in HTML [+ Examples]

Jamie Juviler
Jamie Juviler

Published:

What would the web be without links?

two people using a desktop computer to write code with the html a href attribute

Well, not much of a web at all — the World Wide Web is essentially a system of web pages.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.

Download Now: 50 Code Templates [Free Snippets]

For example, consider this link to the homepage of HubSpot. The HTML for that hyperlink looks like this:

<a href="https://www.hubspot.com/" rel="noopener" target="_blank">homepage of HubSpot</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.

Absolute URL

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).

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.

Relative URL

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.

URI Fragment

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.

Additional Protocols

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 example@gmail.com (though we can’t guarantee you’ll hear back).

<p>For example, click this link to send an email to <a href="mailto:example@gmail.com">example@gmail.com</a> (though we can’t guarantee you’ll hear back).</p>

A Script

Lastly, you can create a link to trigger JavaScript code.

<a href="javascript:alert('Hi there!');">Click here for a nice greeting</a>.</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.New Call-to-action

Topics: HTML

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

Dozens of free coding templates you can start using right now

CMS Hub is flexible for marketers, powerful for developers, and gives customers a personalized, secure experience

START FREE OR GET A DEMO