HTML <a> tagS2C Home   « HTML <a> tag

Definition

The <a> tag and its' closing </a> tag are used to define an anchor in two ways:


  1. To another part of the current document using the href attribute in conjunction with either the name attribute or id attribute. For our examples we will be using id attribute, which has other benefits which we will discuss in the CSS tutorials.
    • This type of anchor is commonly known as a link
  2. To another document using the href attribute.
    • This type of anchor is commonly known as a hyperlink

Example


Link:
<p><a href="#beefpie">Beef Pie Link</a></p>
When clicked will move the webpage to the link below:
<p><h3 id="beefpie">Link To Same Page</h3>
Hyperlink:
<p><a href="http://htmldoctor.info/htmlbasics/lotsofpies.html">Hyperlink</a></p>
When clicked will display the webpage:
http://htmldoctor.info/htmlbasics/lotsofpies.html

Attributes

The following attributes can be used with the <a> tag.


<a> Specific Attributes
Attribute Value Description Example
downloadresource [value]Specifies that the link is to be used for downloading a resource, which can optionally have a value.

<a href="../images/apple.jpg"
   download>Chicken Pie</a> 
<a href="../images/apple.jpg"
   download="resource.fileExt">Apple</a>
hrefurlSpecifies the URL to link to.

<a href="../images/apple.jpg">Apple</a>
hreflanglanguage_codeSpecifies the language of target URL to link to.
Should be only be used when the href attribute is present.

<a href="../images/apple.jpg"
   hreflang="en">Apple</a> 
relSpecifies that the link is to be used for downloading a resource, which can optionally have a value.
Should be only be used when the href attribute is present.

<a href="../images/apple.jpg"
   rel="nofollow">Apple</a> 
alternateAlternative representation of current document.
authorLink to author of this document.
bookmarkGives a permalink for the nearest ancestor.
helpLink to a help document for current context.
licenseReferenced document provides the copyright license terms for current document.
nextIndicates that the document is part of a sequence, and that the link to the referenced document is next.
nofollowIndicates that the link is not endorsed by the original author or publisher of the page.
noreferrerIndicates that no referrer information is to be leaked when following the link.
prefetchSpecifies target document should be cached.
prevIndicates that the document is part of a sequence, and that the link to the referenced document is previous.
searchSpecifies that referenced document provides an interface specifically for searching the document and its related resources.
tagSpecifies a tag that applies to the current document.
targetSpecifies the relationship between the current document and linked document.
Multiple values can be used using a space as a delimiter.

<a href="../images/apple.jpg"
   target="_blank">Apple</a> 
_blankOpens linked document in new window or tab.
_parentOpens linked document in parent frame.
_self (default)Opens linked document in same frame as it was clicked in.
_topOpens linked document in window body
framenameOpens the linked document in a named framename.
typeMIME-typeSpecifies MIME type of the linked resource.
Should only be used when the href attribute is present.

<a href="../images/apple.jpg"
   type="text/html">Apple</a> 
<a> Global Attributes
Attribute Description Example
Common
classSpecifies a classname for the element allowing you to apply the style of the predefined class to the content.

<a href="../images/apple.jpg" class="name">Link Text</a> 
idSpecifies a unique id for the element allowing you to apply the style of the predefined id to the content.

<a href="../images/apple.jpg" id="idname">Link Text</a>
styleSpecifies an inline style for the element allowing you to apply the style to the contentt.

<a href="../images/apple.jpg" style="color:red;">Link Text</a> 
titleSpecifies an inline style for the element allowing you to apply the style to the content.

<a href="../images/apple.jpg" title="Content info.">Link Text</a> 
Keyboard
accesskeySpecifies a keyboard shortcut to associate with the element.

<a href="../images/apple.jpg" accesskey="a">Link Text</a> 
tabindexSpecifies a tab order for the element.

<a href="../images/apple.jpg" tabindex="1">Link Text</a>
Language
dirSpecifies the directional flow of the content.

<!-- The text will flow from left to right -->
<a href="../images/apple.jpg" dir="ltr">Link Text</a>
<!-- The text will flow from right to left -->
<a href="../images/apple.jpg" dir="rtl">Link Text</a>
langSpecifies a language code for the content of the element.

<a href="../images/apple.jpg" lang="en">Link Text</a>
spellcheckSpecifies an inline style for the element allowing you to apply the style to the contentt.

<!-- Valid values true and false. -->
<!-- Default inherited / browser specific. -->
<a href="../images/apple.jpg" spellcheck="true">Link Text</a> 
translateSpecifies an inline style for the element allowing you to apply the style to the content.

<!-- Valid values yes and no. -->
<!-- Default yes. -->
<a href="../images/apple.jpg" translate="no">Link Text</a>
Miscellaneous
contenteditableSpecifies whether the content of the element is editable.

<!-- Valid values true and false. -->
<!-- Default inherited. -->
<a href="../images/apple.jpg" contenteditable="false">Link Text</a> 
draggableSpecifies whether the element is draggable.

<!-- Valid values true and false. -->
<!-- Default browser specific. -->
<a href="../images/apple.jpg" draggable="true">Link Text</a> 
hiddenSpecifies whether the element is not yet, or no longer, relevant.

<!-- Valid values an empty string or hidden -->
<a href="../images/apple.jpg" hidden>Link Text</a> 
<a href="../images/apple.jpg" hidden="hidden">Link Text</a> 
<a> Event Attributes
Attribute Description Example
Document Element
oncopyThe script to be run when the user copies the content of an element.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" oncopy="go()">Link Text</a> 
oncutThe script to be run when the user cuts the content of an element.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" oncut="go()">Link Text</a>
onpasteThe script to be run when the user pastes some content into an element.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onpaste="go()">Link Text</a>
Drag and Drop
ondragThe script to be run when an element is dragged.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" ondrag="go()">Link Text</a>
ondragendThe script to be run when an element has stopped being dragged.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" ondragend="go()">Link Text</a>
ondragenterThe script to be run when an element has been dragged to a valid drop target.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" ondragenter="go()">Link Text</a>
ondragleaveThe script to be run when an element leaves a valid drop target.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" ondragleave="go()">Link Text</a>
ondragoverThe script to be run when an element is being dragged over a valid drop target.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" ondragover="go()">Link Text</a>
ondragstartThe script to be run at the start of a drag operation.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" ondragstart="go()">Link Text</a>
ondropThe script to be run when a dragged element is being dropped.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" ondrop="go()">Link Text</a>
Form
onblurThe script to be run when the element loses focus.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onblur="go()">Link Text</a>
onchangeThe script to be run when object changed and attempt to leave field.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onchange="go()">Link Text</a>
oncontextmenuThe script to be run when a context menu is triggered.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" oncontextmenu="go()">Link Text</a>
onfocusThe script to be run when the element gets focus.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onfocus="go()">Link Text</a>
oninputThe script to be run when an element gets user input.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" oninput="go()">Link Text</a>
oninvalidThe script to be run when an element is invalid.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" oninvalid="go()">Link Text</a>
onresetThe script to be run when a dragged element is being dropped.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onreset="go()">Link Text</a>
onselectThe script to be run when some or all of the contents of an object are selected.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onselect="go()">Link Text</a>
onsubmitThe script to be run when a form is submitted.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onsubmit="go()">Link Text</a>
Keyboard
onkeydownThe script to be run when an element is in focus and keyboard key is pressed down.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onkeydown="go()">Link Text</a>
onkeypressThe script to be run when an element is in focus and keyboard key is pressed down and released.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onkeypress="go()">Link Text</a>
onkeyupThe script to be run when an element is in focus and keyboard key is released.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onkeyup="go()">Link Text</a>
Media
onabortThe script code to be run on abort.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onabort="go()">Link Text</a>
oncanplayThe script to be run when a file has buffered enough so it is ready to start playing.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" oncanplay="go()">Link Text</a>
oncanplaythroughThe script to be run when a file can be played all the way to the end without further need of buffering.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" oncanplaythrough="go()">Link Text</a>
oncuechangeThe script to be run when the cue changes when using the track element.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" oncuechange="go()">Link Text</a>
ondurationchangeThe script to be run when the length of the media is changed.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" ondurationchange="go()">Link Text</a>
onemptiedThe script to be run when a media resource element suddenly becomes empty, usually due to an error.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onemptied="go()">Link Text</a>
onendedThe script to be run when the media has reach the end.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onended="go()">Link Text</a>
onloadeddataThe script to be run when media data is loaded and playback can start.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onloadeddata="go()">Link Text</a>
onloadedmetadataThe script to be run when metadata has been loaded.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onloadedmetadata="go()">Link Text</a>
onloadstartThe script to be run when the media resource has started loading.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onloadstart="go()">Link Text</a>
onpauseThe script to be run when the media resource has been paused.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onpause="go()">Link Text</a>
onplayThe script to be run when the media resource starts playback.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onplay="go()">Link Text</a>
onplayingThe script to be run when playback has already begun.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onplaying="go()">Link Text</a>
onprogressThe script to be run when the browser is fetching the media data.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onprogress="go()">Link Text</a>
onratechangeThe script to be run when the playback rate changes.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onratechange="go()">Link Text</a>
onseekedThe script to be run when the seeking attribute is set to false indicating that seeking has finished.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onseeked="go()">Link Text</a>
onseekingThe script to be run when the seeking attribute is set to true indicating that seeking is currently active.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onseeking="go()">Link Text</a>
onstalledThe script to be run when the browser is unable to continue fetching media data.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onstalled="go()">Link Text</a>
onsuspendThe script to be run when media data has stopped before being completely loaded.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onsuspend="go()">Link Text</a>
ontimeupdateThe script to be run when the media resources current playback position has changed.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" ontimeupdate="go()">Link Text</a>
onvolumechangeThe script to be run when the volume has changed or been muted.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onvolumechange="go()">Link Text</a>
onwaitingThe script to be run when the media resource has paused but is expected to resume.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onwaiting="go()">Link Text</a>
Mouse
onclickThe script to be run when when a mouse is clicked on an element.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onclick="go()">Link Text</a>
ondblclickThe script to be run when a mouse is double clicked on an element.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" ondblclick="go()">Link Text</a>
onmousedownThe script to be run when he mouse button is pressed down while the cursor is over an element.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onmousedown="go()">Link Text</a>
onmousemoveThe script to be run when the mouse button is moved.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onmousemove="go()">Link Text</a>
onmouseoutThe script to be run when the mouse cursor moves off an element.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onmouseout="go()">Link Text</a>
onmouseoverThe script to be run when the mouse cursor moves over an element.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onmouseover="go()">Link Text</a>
onmouseupThe script to be run when the mouse button is released while the cursor is over the element.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onmouseup="go()">Link Text</a>
onwheelThe script to be run when the mouse wheel rolls up or down over an element.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onwheel="go()">Link Text</a>
Scroll
onscrollThe script code to be run when the scrollbar of an element is being scrolled.

 <!-- Executes go() function -->
<a href="../images/apple.jpg" onscroll="go()">Link Text</a> 
Scroll
Window - NONE

Link To Same Page

We linked to a different part of the page!

Beef Pie

Go back



Relevant HTML Tutorial

HTML Basics - Links