href
Specifies the destination address. It can be an absolute or relative URL, or the name of an anchor. An
absolute URL is the complete URL of a website like http://example.com/. A relative URL points to
another directory and/or document inside the same website, e.g. /about-us/ points to the directory
“about-us” inside the root directory (/). When pointing to another directory without explicitly specifying
the document, web servers typically return the document “index.html” inside that directory.
hreflang
Specifies the language of the resource linked by the href attribute (which must be present with this
one). Use language values from BCP 47 for HTML5 and RFC 1766 for HTML 4.
rel
Specifies the relationship between the current document and the linked document. For HTML5, the
values must be defined in the specification or registered in the Microformats wiki.
target
Specifies where to open the link, e.g. in a new tab or window. Possible values are _blank, _self, _parent, _top, and framename (deprecated). Forcing such behaviour is not recommended since it violates the control of the user over a website.
title
Specifies extra information about a link. The information is most often shown as a tooltip text when
the cursor moves over the link. This attribute is not restricted to links, it can be used on almost all
HTML tags.
download
Specifies that the target will be downloaded when a user clicks on the hyperlink. The value of the
attribute will be the name of the downloaded file. There are no restrictions on allowed values, and the
browser will automatically detect the correct file extension and add it to the file (.img, .pdf, etc.). If the
value is omitted, the original filename is used.
Anchor tags are commonly used to link separate webpages, but they can also be used to link between different
places in a single document, often within table of contents or even launch external applications. This topic explains the implementation and application of HTML anchor tags in various roles.
Link to another site
This is the basic use of the <a> (anchor element) element:
Code: Select all
<a href="http://example.com/">Link to example.com</a>
To denote that this link leads to an external website, you can use the external link type:
Code: Select all
<a href="http://example.com/" rel="external">example site</a>
Code: Select all
<a href="ftp://example.com/">This could be a link to a FTP site</a>
using the File Transfer Protocol (FTP) rather than the Hypertext Transfer Protocol (HTTP).
Link to an anchor
Anchors can be used to jump to specific tags on an HTML page. The <a> tag can point to any element that has an id attribute. To learn more about IDs, visit the documentation about Classes and IDs. Anchors are mostly used to jump to a subsection of a page and are used in conjunction with header tags.
Suppose you've created a page (page1.html) on many topics:
Code: Select all
<h2>First topic</h2>
<p>Content about the first topic</p>
<h2>Second topic</h2>
<p>Content about the second topic</p>
If you gave an id attribute to your topics, you could then link to them
Code: Select all
<h2 id="Topic1">First topic</h2>
<p>Content about the first topic</p>
<h2 id="Topic2">Second topic</h2>
<p>Content about the second topic</p>
Code: Select all
<h1>Table of Contents</h1>
<a href='#Topic1'>Click to jump to the First Topic</a>
<a href='#Topic2'>Click to jump to the Second Topic</a>
Link to a page on the same site
You can use a relative path to link to pages on the same website.
Code: Select all
<a href="/example">Text Here</a>
If this link was on http://example.com, the following two links would bring the user to the same location
Code: Select all
<a href="/page">Text Here</a>
Code: Select all
<a href="http://example.com/page">Text Here</a>
Link that dials a number
If the value of the href-attribute begins with tel:, your device will dial the number when you click it. This works on mobile devices or on computers/tablets running software – like Skype or FaceTime – that can make phone calls.
Code: Select all
<a href="tel:11234567890">Call us</a>
Code: Select all
<a href="example.com" target="_blank">Text Here</a>
new tab or window (per user preference).
SECURITY VULNERABILITY WARNING!
Using target="_blank" gives the opening site partial access to the window.opener object via JavaScript,
which allows that page to then access and change the window.opener.location of your page and
potentially redirect users to malware or phishing sites.
Whenever using this for pages you do not control, add rel="noopener" to your link to prevent the
window.opener object from being sent with the request.
Currently, Firefox does not support noopener, so you will need to use rel="noopener noreferrer" for
maximum effect.
Link that runs JavaScript
Simply use the javascript: protocol to run the text as JavaScript instead of opening it as a normal link:
Code: Select all
<a href="javascript:myFunction();">Run Code</a>
Code: Select all
<a href="#" onclick="myFunction(); return false;">Run Code</a>
Also noteworthy, you can include an exclamation mark ! after the hashtag in order to prevent the page from
scrolling to the top. This works because any invalid slug will cause the link to not scroll anywhere on the page,
because it couldn't locate the element it references (an element with id="!"). You could also just use any invalid
slug (such as #scrollsNowhere) to achieve the same effect. In this case, return false; is not required:
Code: Select all
<a href="#!" onclick="myFunction();">Run Code</a>
Basic usage
If the value of the href-attribute begins with mailto: it will try to open an email client on click:
Code: Select all
<a href="mailto:example@example.com">Send email</a>
Cc and Bcc
You can also add addresses for cc- or bcc-recipients using the following syntax:
Code: Select all
<a href="mailto:example@example.com?cc=john@example.com&bcc=jane@example.com">Send email</a>
You can populate the subject and body for the new email as well:
Code: Select all
<a href="mailto:example@example.com?subject=Example+subject&body=Message+text">Send email</a>