Skip to main content

HTML Tags

HTML <a> Tag

Written by Updated HTML <a> Tag exercises

The a tag makes links: to another page, a file, an email address, or a section of the same page. The text inside is what people click, and the href is where it goes. Everything else is optional.

The many kinds of links are covered with examples in HTML Links. This is the tag reference.

Syntax

html

<a href="URL">Link Text</a>
AttributeWhat it does
hrefThe destination: a web address, a file, #id for a section, mailto: or tel:.
targetWhere it opens. _blank opens a new tab.
downloadDownloads the file instead of opening it. Works only for files on the same site.
relThe relationship to the target, such as nofollow, noopener or noreferrer.
hreflangThe language of the linked page.
titleExtra information shown on hover. Not a replacement for clear link text.

Example

html

<!DOCTYPE html>
<html>
<head>
  <title>Anchor Tag Example</title>
  <style>
    a {
      color: #29AB87;
      text-decoration: none;
      font-weight: bold;
    }
    a:hover {
      color: #1c7a63;
      text-decoration: underline;
    }
  </style>
</head>
<body>
  <h1>HTML &lt;a&gt; Tag Example</h1>

  <p>
    Visit our official website:
    <a href="https://html5andcss3.org" target="_blank" title="Visit HTML5 and CSS3 Official Site">
      HTML5 and CSS3
    </a>
  </p>

  <p>
    Download a sample PDF file:
    <a href="https://ik.imagekit.io/html5andcss3/lesson-samples/sample.pdf" download="sample.pdf">Download PDF</a>
  </p>

  <p>
    Send us an email:
    <a href="mailto:info@html5andcss3.org">info@html5andcss3.org</a>
  </p>

  <p>
    Jump to a section on this page:
    <a href="#about">Go to About Section</a>
  </p>

  <h2 id="about">About Section</h2>
  <p>This section explains the working of the anchor tag in HTML.</p>
</body>
</html>

The example shows four kinds of link: one to a website that opens in a new tab, one to a PDF, one email link, and one that jumps down to the "About Section" on the same page.

The PDF is on another domain, so most browsers ignore download and simply open it. For a real download, the file must be on your own site, or the server must send it as an attachment.

About target="_blank"

Opening links in a new tab breaks the back button and can disorient people, so use it only when it really helps, such as a help link in the middle of a form, and say so in the link text. Modern browsers automatically treat target="_blank" as rel="noopener", so the old security warning about it no longer applies, but adding rel="noopener" does no harm.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Good to know

  • Write link text that makes sense on its own: "Download the price list" rather than "click here".
  • An <a> without href is not a link: it cannot be focused with the keyboard. For actions on the page, use a <button>.
  • <a> can wrap blocks, such as a whole card, in HTML5.

HTML <a> Tag exercises

Link to a page

Add a link that says Docs and goes to /docs

Result

Check your answer to see it here.