Skip to content
Laviyx Learn

Try

HTML course menu

HTML Links: The a Element, href, target and Writing Good Link Text

Learn how to create links in HTML with the a element and href attribute, open links in a new tab with target="_blank", link images, write descriptive link text, and use the download and title attributes.

Beginner4 min readUpdated
On this page

Links are what make the web a web: they connect pages to each other. The "HyperText" in HTML means text with links. In HTML, a link is an <a> (anchor) element.

HTML
<p>Read the <a href="https://developer.mozilla.org/en-US/docs/Web/HTML">MDN HTML guide</a> for reference.</p>
Result
  • <a> creates the link.
  • href (hypertext reference) is the destination URL.
  • The content between the tags is the clickable link text.

Browsers show unvisited links blue and underlined by default, and visited links purple. You can change this with CSS.

href valueGoes to
https://example.com/Another website (an absolute URL)
about.htmlAnother page on the same site (a relative URL)
#contactA section of the same page
mailto:hello@example.comOpens an email app
tel:+919876543210Starts a phone call on phones

Absolute and relative URLs have their own lesson: Absolute and Relative URLs. Section, email and phone links are in Page Anchors, Email and Phone Links.

Opening in a new tab#

target="_blank" opens the link in a new tab or window:

HTML
<a href="https://www.w3.org/" target="_blank">W3C website</a>

Use it sparingly. People can already choose to open a link in a new tab, and unexpected new tabs are confusing, especially for screen reader users. A good use is a link that would interrupt a task, such as a help page opened from the middle of a form. When you do use it, consider saying so in the link text, for example "Help (opens in a new tab)".

Modern browsers treat target="_blank" links as if they had rel="noopener", which stops the new page from controlling the original one. You may still see rel="noopener noreferrer" written out in existing code; it is harmless.

Linking an image#

Any inline content can be a link, including images. The image's alt text then becomes the link's name, so describe where the link goes:

HTML
<a href="https://en.wikipedia.org/wiki/Mango">
  <img src="/lesson-assets/mango.svg" alt="Read about mangoes on Wikipedia" width="160" height="160">
</a>
Result

Link text should make sense on its own. Screen reader users often list all the links on a page, and "click here" repeated ten times tells them nothing.

HTML
<!-- Vague -->
<p>To see our prices, <a href="/pricing">click here</a>.</p>

<!-- Clear -->
<p>See our <a href="/pricing">pricing plans</a>.</p>
  • Describe the destination: "pricing plans", "download the brochure (PDF, 2 MB)".
  • Avoid "click here", "read more" and "link".
  • Do not use the full URL as text unless people need to see or copy it.

Other useful attributes#

AttributePurpose
titleExtra advisory text, often shown as a tooltip. Do not rely on it for important information.
downloadAsks the browser to download the file instead of opening it. Works for files on the same site.
hreflangThe language of the linked page, such as hi.
HTML
<a href="/files/timetable.pdf" download>Download the timetable (PDF)</a>

Use a link (<a href>) when the user goes somewhere: another page or another part of the page. Use a button (<button>) when the user does something on the current page, such as submit a form, open a menu or add an item to a cart. Using the right element gives keyboard and screen reader users the behaviour they expect. Buttons are covered in Buttons.

Common mistakes#

Practice#

Key takeaway#

Finished this lesson?