Tóm Tắt
HTML <a> Tag
The HTML <a>
tag is used for creating an a
element (also known as an “anchor” element).
The a
element represents a hyperlink. This is usually a link to another document.
You can use the <a>
tag to link text or images. You can also link a large block of content (even containing multiple elements) if required – it’s not just restricted to hyperlinking single elements. However, there must be no “interactive content” descendant.
Syntax
The <a>
tag is written as <a href=""></a>
with the linked URL between the double quotes of the href
attribute and the anchor text (i.e. the text that the user sees) between the start and end tags.
Like this:
Examples
Basic tag usage
Open the link in a new window (or tab)
Here we use target="_blank"
to open the link in a new window.
Reload the new window
Here we open multiple links in a new window, but instead of a new window being opened with each link, a new window is opened with the first link, then the following links load their contents into that window.
We do this simply by giving the target
attribute a name that doesn’t exist (i.e. we make up our own name for the window/tab).
Linked Image
Here we wrap the <a>
around an image to create a linked image.
No-Follow
Here we use rel="nofollow"
to create a “nofollow” link. This can be used to tell search engines that you don’t endorse the content at the other end of the link. The nofollow
attribute value is typically used on paid links and advertising.
Many people refer to this as the “nofollow tag” but it’s not actually a tag. It’s not even an attribute (the attribute is rel
). The nofollow
bit is simply a value of the rel
attribute.
Attributes
Attributes can be added to an HTML element to provide more information about how the element should appear or behave.
The <a>
element accepts the following attributes.
AttributeDescription
href
Specifies the URL of a page that the link goes to.
target
Specifies the default browsing context to load the URL into. Only to be used when the href
attribute is present.
Possible values:
_blank
_self
_top
_parent
- Any string with at least one character that does not start with a
U+005F LOW LINE
character. In other words, cannot start with an underscore character (names starting with an underscore are reserved for special keywords).
download
Indicates that the link is to be used for downloading a resource (such as a file). The author can specify a default file name by providing a value. This attribute is optional.
Value:
[Default file name.] (optional)
rel
Describes the relationship between the current document and the destination URI. Only to be used when the href
attribute is present. Multiple values can be provided, separated by a space.
Possible values:
ValueDescription
alternate
Gives alternate representations of the current document.
author
Gives a link to the current document’s author.
bookmark
Provides the permalink for the nearest ancestor section.
external
Indicates that the referenced document is not part of the same site as the current document.
help
Provides a link to context-sensitive help.
license
Indicates that the main content of the current document is covered by the copyright license described by the referenced document.
next
Indicates that the current document is a part of a series, and that the next document in the series is the referenced document.
nofollow
Indicates that the current document’s original author or publisher does not endorse the referenced document. This attribute is often used to declare paid links to search engines such as Google, who, request that webmasters declare all paid links (eg, advertising) in this manner.
noopener
Creates a top-level browsing context that is not an auxiliary browsing context if the hyperlink would create either of those to begin with (i.e., has an appropriate target
attribute value).
noreferrer
Requires that the user agent not send an HTTP Referer
(sic) header if the user follows the hyperlink.
opener
Creates an auxiliary browsing context if the hyperlink would otherwise create a top-level browsing context that is not an auxiliary browsing context (i.e., has "_blank"
as target
attribute value).
prev
Indicates that the current document is a part of a series, and that the previous document in the series is the referenced document.
search
Gives a link to a resource that can be used to search through the current document and its related pages.
tag
Gives a tag (identified by the given address) that applies to the current document.
rev
Reverse link relationship of the destination resource to this document (or subsection/topic).
hreflang
Language code of the destination URL. Only to be used when the href
attribute is present.
type
Specifies the MIME type of the linked resource. Only to be used when the href
attribute is present.
referrerpolicy
Referrer policy for fetches initiated by the element.
Global Attributes
The following attributes are standard across all HTML elements. Therefore, you can use these attributes with the <a>
tag , as well as with all other HTML tags.
accesskey
autocapitalize
class
contenteditable
data-*
dir
draggable
hidden
id
inputmode
is
itemid
itemprop
itemref
itemscope
itemtype
lang
part
slot
spellcheck
style
tabindex
title
translate
For a full explanation of these attributes, see HTML 5 global attributes.
Event Handlers
Event handler content attributes enable you to invoke a script from within your HTML. The script is invoked when a certain “event” occurs. Each event handler content attribute deals with a different event.
onabort
onauxclick
onblur
oncancel
oncanplay
oncanplaythrough
onchange
onclick
onclose
oncontextmenu
oncopy
oncuechange
oncut
ondblclick
ondrag
ondragend
ondragenter
ondragexit
ondragleave
ondragover
ondragstart
ondrop
ondurationchange
onemptied
onended
onerror
onfocus
onformdata
oninput
oninvalid
onkeydown
onkeypress
onkeyup
onlanguagechange
onload
onloadeddata
onloadedmetadata
onloadstart
onmousedown
onmouseenter
onmouseleave
onmousemove
onmouseout
onmouseover
onmouseup
onpaste
onpause
onplay
onplaying
onprogress
onratechange
onreset
onresize
onscroll
onsecuritypolicyviolation
onseeked
onseeking
onselect
onslotchange
onstalled
onsubmit
onsuspend
ontimeupdate
ontoggle
onvolumechange
onwaiting
onwheel
Most event handler content attributes can be used on all HTML elements, but some event handlers have specific rules around when they can be used and which elements they are applicable to.
For more detail, see HTML event handler content attributes.