The HTML <a>
element (or anchor element) creates a hyperlink to other web pages, files, locations within the same page, email addresses, or any other URL.
Content categories | Flow content, phrasing content, interactive content, palpable content. |
---|---|
Permitted content | Transparent, containing either flow content (excluding interactive content) or phrasing content. |
Tag omission | None, both the starting and ending tag are mandatory. |
Permitted parents | Any element that accepts phrasing content, or any element that accepts flow content, but always excluding <a> elements (according to the logical principle of symmetry, if <a> tag, as a parent, can not have interactive content, then the same <a> content can not have <a> tag as its parent). |
Permitted ARIA roles |
button , checkbox , menuitem , menuitemcheckbox , menuitemradio , option , radio , switch , tab , treeitem
|
DOM interface | HTMLAnchorElement |
This element's attributes include the global attributes.
download
HTML5
/
and \
are converted to underscores. Most file systems limit some punctuation in file names, and browsers will adjust the suggested name accordingly. blob:
URLs and data:
URLs to download content generated by JavaScript, such as pictures created in an image-editor Web app.Content-Disposition:
gives a different filename than this attribute, the HTTP header takes priority over this attribute.Content-Disposition:
is set to inline
, Firefox prioritizes Content-Disposition
, like the filename case, while Chrome prioritizes the download
attribute.href
#
), which specifies an internal target location (an ID of an HTML element) within the current document. URLs are not restricted to Web (HTTP)-based documents, but can use any protocol supported by the browser. For example, file:
, ftp:
, and mailto:
work in most browsers.Note: You can use href="#top"
or the empty fragment href="#"
to link to the top of the current page. This behavior is specified by HTML5.
hreflang
referrerpolicy
'no-referrer'
means the Referer:
header will not be sent.'no-referrer-when-downgrade'
means no Referer:
header will be sent when navigating to an origin without HTTPS. This is the default behavior.'origin'
means the referrer will be the origin of the page, not including information after the domain.'origin-when-cross-origin'
meaning that navigations to other origins will be limited to the scheme, the host and the port, while navigations on the same origin will include the referrer's path.'unsafe-url'
means the referrer will include the origin and path, but not the fragment, password, or username. This is unsafe because it can leak data from secure URLs to insecure ones.rel
target
<iframe>
. The following keywords have special meanings: _self
: Load the URL into the same browsing context as the current one. This is the default behavior._blank
: Load the URL into a new browsing context. This is usually a tab, but users can configure browsers to use new windows instead._parent
: Load the URL into the parent browsing context of the current one. If there is no parent, this behaves the same way as _self
._top
: Load the URL into the top-level browsing context (that is, the "highest" browsing context that is an ancestor of the current one, and has no parent). If there is no parent browsing context, this behaves the same way as _self
.Note: When using target
, consider adding rel="noopener noreferrer"
to avoid exploitation of the window.opener
API.
type
charset
Obsolete since HTML5
ISO-8859-1
. Usage note: This attribute is obsolete in HTML5 and should not be used by authors. To achieve its effect, use the HTTP Content-Type:
header on the linked URL.
coords
HTML 4 only, Obsolete since HTML5
shape
attribute, this attribute used a comma-separated list of numbers to define the coordinates of the link on the page.name
HTML 4 only, Obsolete since HTML5
id
and name
could be used simultaneously on a <a>
element as long as they have identical values. Usage note: This attribute is obsolete in HTML5, use the global attribute id
instead.
rev
HTML 4 only, Obsolete since HTML5
shape
HTML 4 only, Obsolete since HTML5
circle
, default
, polygon
, and rect
. The format of the coords attribute depends on the value of shape. For circle
, the value is x,y,r
where x
and y
are the pixel coordinates for the center of the circle and r
is the radius value in pixels. For rect
, the coords attribute should be x,y,w,h
. The x,y
values define the upper-left-hand corner of the rectangle, while w
and h
define the width and height respectively. A value of polygon
for shape requires x1,y1,x2,y2,...
values for coords. Each of the x,y
pairs defines a point in the polygon, with successive points being joined by straight lines and the last point joined to the first. The value default
for shape requires that the entire enclosed area, typically an image, be used. usemap
attribute for the <img>
element and the associated <map>
element to define hotspots instead of the shape
attribute.<!-- anchor linking to external file --> <a href="https://www.mozilla.com/"> External Link </a>
<!-- links to element on this page with id="attr-href" --> <a href="#attr-href"> Description of Same-Page Links </a>
Description of Same Page Links
This small example use an image to link to the MDN home page. The home page will open in a new browsing context, that is a new page or a new tab.
<a href="https://developer.mozilla.org/en-US/" target="_blank"> <img src="https://mdn.mozillademos.org/files/6851/mdn_logo.png" alt="MDN logo" /> </a>
It's common to create links that open in the user's email program to allow them to send a new message. This is done with a mailto:
link. Here's a simple example:
<a href="mailto:[email protected]">Send email to nowhere</a>
This results in a link like this: Send email to nowhere.
For additional details about the mailto
URL scheme, such as including the subject, body, or other predetermined content, see Email links or RFC 6068.
With phones viewing web documents and laptops connected to phones, offering phone links becomes increasingly helpful.
<a href="tel:+491570156">+49 157 0156</a>
For additional details about the tel
URL scheme, see RFC 2806 and RFC 2396.
download
attribute to save a <canvas>
as a PNGIf you want to let users download an HTML Canvas as an image, you can create a link with a download
attribute and the canvas data as a file URL:
var link = document.createElement('a'); link.innerHTML = 'download image'; link.addEventListener('click', function(ev) { link.href = canvas.toDataURL(); link.download = "mypainting.png"; }, false); document.body.appendChild(link);
You can see this in action at jsfiddle.net/codepo8/V6ufG/2/
HTML 3.2 defines only the name
, href
, rel
, rev
, and title
attributes.
Anchor tags are often abused with the onclick
event to create psuedo-buttons by setting href to "#"
or "javascript:void(0)"
to prevent the page from refreshing. These values cause unexpected behavior when copying/dragging links, opening links in a new tabs/windows, bookmarking, and when JavaScript is still downloading/errors out/is disabled. This also conveys incorrect semantics to assistive technologies, e.g. screen readers. In these cases, it is recommended to use a <button>
instead. In general you should only use an anchor for navigation using a proper URL.
Whether clicking on an <a>
causes it to become focused varies by browser and OS.
Desktop Browsers | Windows 8.1 | OS X 10.9 |
---|---|---|
Firefox 30.0 | Yes | Yes |
Chrome ≥39 (Chromium bug 388666) | Yes | Yes |
Safari 7.0.5 | N/A | Only when it has a tabindex
|
Internet Explorer 11 | Yes | N/A |
Presto (Opera 12) | Yes | Yes |
Mobile Browsers | iOS 7.1.2 | Android 4.4.4 |
---|---|---|
Safari Mobile | Only when it has a tabindex
| N/A |
Chrome 35 | ??? | Only when it has a tabindex
|
Specification | Status | Comment |
---|---|---|
Referrer Policy The definition of 'referrer attribute' in that specification. | Working Draft | Added the referrer attribute. |
WHATWG HTML Living Standard The definition of '<a>' in that specification. | Living Standard | |
HTML5 The definition of '<a>' in that specification. | Recommendation | |
HTML 4.01 Specification The definition of '<a>' in that specification. | Recommendation |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | 1.0 (1.7 or earlier) [1] | (Yes) | (Yes) | (Yes) |
href="#top" | (Yes) | (Yes) | 10.0 (10.0) | (Yes) | (Yes) | (Yes) |
download | 14 | (Yes) | 20.0 (20.0) | Edge 13 [4] | 15 | No support |
ping | (Yes) | ? | No support [2] | No support | (Yes) | No support |
referrerpolicy | 46.0 [3] | ? | 50 (50) | No support | No support | No support |
Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | 1.0 (1.0) [1] | (Yes) | (Yes) | (Yes) | (Yes) |
href="#top" | (Yes) | ? | (Yes) | 10.0 (10.0) | (Yes) | (Yes) | (Yes) | ? |
download | (Yes) | ? | (Yes) | 20.0 (20.0) | No support | ? | No support | ? |
ping | ? | ? | ? | No support [2] | ? | ? | ? | ? |
referrerpolicy | No support | 46.0 [3] | ? | 50.0 (50.0) | No support | No support | No support | 46.0 [3] |
[1] Starting from Gecko 41 (Firefox 41.0 / Thunderbird 41.0 / SeaMonkey 2.38), <a>
without href
attribute is no longer classified as interactive content. Clicking it inside <label>
will activate labelled content (bug 1167816).
[3] Implemented behind a flag.
[4] Attempting to download data URIs causes Edge to crash until v14.14357.
<abbr>
, <em>
, <strong>
, <small>
, <cite>
, <q>
, <dfn>
, <time>
, <code>
, <var>
, <samp>
, <kbd>
, <sub>
, <sup>
, <b>
, <i>
, <mark>
, <ruby>
, <rp>
, <rt>
, <bdo>
, <span>
, <br>
, <wbr>
.
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a