Definition and Usage
The <a> tag (also known as the anchor tag) is used to create links.
If a URL is specified using the href attribute, it represents a hyperlink (shortened to "link") displayed as the tag's content.
Clicking the tag's content (such as text or an image) takes the user to the linked URL.
Basic Structure
The basic structure of the <a> tag is as follows:
<a href="URL">Content</a>
href="URL": Specifies the URL for the hyperlink (shortened to "link").Content: Content that users can click, such as text or an image.
Example
<p>Quick Links</p>
<ul>
<li>
<a href="https://coding.courses/">codingCourses</a>
</li>
<li>
<a href="https://www.google.com/"><img src="googlelogo" alt="google"></a>
</li>
</ul>
If the content of the <a> tag includes text, an underline is displayed by default.
See the CSS text-decoration-line Property.
Usage Notes
To properly understand and use the <a> tag, you need to be familiar with the following terms:
URL
URL stands for Uniform Resource Locator. A URL is used to identify and locate various resources on the web. URLs represent different types of resources, such as web pages, images, videos, and files. In other words, a URL serves as a unique address that provides information about a resource and its location.
- A typical URL structure begins with a protocol (scheme), such as
http://orhttps://. - The
mailto:scheme uses a URL format that allows users to send an email directly to a specific email address (in the formatmailto:email-address, such asmailto:help@example.com). - The
tel:scheme uses a URL format that represents a phone number and allows users to make a phone call (in the formattel:phone-number, such astel:+12025550147).
In a URL, the scheme is the part that contains information about the type and location of a web resource.
In simple terms, it appears at the beginning of a URL and specifies which protocol to use, which server to connect to, and which resource to request.
Hyperlink
A hyperlink is a link that allows users to directly access resources on the internet, such as other web pages, files, or specific locations.
Hyperlinks are generally represented as clickable text or images. When clicked, they take users to the linked destination or download a file. For example, clicking text like "Click here" navigates directly to the specified page or resource.
A hyperlink is also commonly shortened to "link."
How the <a> Tag Works with URLs and Hyperlinks
Email App Linking
If you specify a mailto: scheme in the URL assigned to the href attribute, using the URL format that allows users to compose a message directly to a specific email address (mailto:email-address, such as mailto:help@example.com), an email app on the user's device that can be used to compose a message directly to that email address opens automatically in a new message window. This feature is supported by default in most web browsers and operating systems. (An email app must be installed on the user's device.)
A link that connects to an email app using mailto: is commonly called a "mailto link." Although it falls under the concept of a hyperlink, it differs from a typical hyperlink in its functionality, presentation, and purpose.
<a href="mailto:help@example.com">Send an Email to the Administrator</a>
When the user clicks the "Send an Email to the Administrator" link, an email app installed on the user's device opens automatically, displaying a new message window. The recipient's email address is automatically set to help@example.com.
Email app linking is very useful. Users can compose an email with a single click without having to enter the email address themselves.
Phone App Linking
If you specify a tel: scheme in the URL assigned to the href attribute, using the URL format that allows users to make a phone call to a specific phone number (tel:phone-number, such as tel:+12025550147), a phone app on the user's device automatically connects and opens a new message window. This feature is supported by default in most web browsers and operating systems. (A phone app must be installed on the user's device.)
A link that connects to a phone app using tel: is commonly called a "tel link." Although it falls under the concept of a hyperlink, it differs from a typical hyperlink in its functionality, presentation, and purpose.
<a href="tel:+12025550147">Talk to an Advisor</a>
When the user clicks the "Talk to an Advisor" link, a phone app installed on the user's device launches automatically and opens a new message window. The recipient's phone number is automatically set to +12025550147. Some web browsers may require an additional confirmation step when the user clicks a "tel link."
Phone app linking is a useful feature that makes communication between a website and its users easier and helps improve customer satisfaction.
The tel:phone-number format is as follows:
href="tel:+country-code-phone-number"
The + symbol indicates an international dialing prefix. For calls that do not require international dialing, specify only the phone number, without the + symbol or country code.
href="tel:phone-number"
Phone numbers, including those in international format, can contain hyphens (-), spaces, and other separators.
<!-- Mobile phone -->
<a href="tel:2025550147">Talk to an Advisor</a>
<a href="tel:202-555-0147">Talk to an Advisor</a>
<a href="tel:202 555 0147">Talk to an Advisor</a>
<!-- Landline -->
<a href="tel:2025550128">Talk to an Advisor</a>
<a href="tel:202-555-0128">Talk to an Advisor</a>
<a href="tel:202 555 0128">Talk to an Advisor</a>
Moving Within the Same Page
If the URL specified in the href attribute contains a fragment (also known as a "hash link"), the browser navigates to the fragment's target.
First, let's look at what a fragment in a URL (or URI) is. A URL generally consists of a domain name and a path, among other components. Here is an example of a URL that includes a # character at the end of a typical URL.
The part highlighted in red, #example, is the fragment. It is also informally referred to as a "hash URL."
In a URL, the fragment is the part after the # character. In an HTML document, the browser uses this part to find an element whose id attribute matches the fragment. It then scrolls the page to bring that element into view, typically positioning it near the top of the viewport.
<nav>
<ul>
<li><a href="#section-1">Go to Section 1</a></li>
<li><a href="#section-2">Go to Section 2</a></li>
<li><a href="#section-3">Go to Section 3</a></li>
</ul>
</nav>
<section id="section-1">
<h2>Section 1</h2>
</section>
<section id="section-2">
<h2>Section 2</h2>
</section>
<section id="section-3">
<h2>Section 3</h2>
</section>
Good to Know!
You can create a link that navigates to the top of the current page by using href="#top" or an empty fragment (href="#"). See the HTML specification.
Using Related Attributes
Let's look at the attributes related to the <a> tag.
href Attribute
The href attribute specifies the URL that the hyperlink points to.
The href attribute is not required. If it is omitted, the <a> tag does not represent a hyperlink.
This URL can use any URL scheme supported by the browser.
- A typical URL structure begins with a protocol (scheme), such as
http://orhttps://. - The
mailto:scheme uses a URL format that allows users to send an email directly to a specific email address (in the formatmailto:email-address, such asmailto:help@example.com). - The
tel:scheme uses a URL format that represents a phone number and allows users to make a phone call (in the formattel:phone-number, such astel:+12025550147).
target Attribute
The target attribute specifies which window or tab the linked page opens in when the user clicks the link.
Values
_self |
The default value when the target attribute is not specified.
The URL is displayed in the current window or frame. |
|---|---|
_blank |
The URL is displayed in a new window or tab. |
_parent |
The URL is displayed in the parent frame. If there is no frame structure, it behaves the same as _self. |
_top |
The URL is displayed in the top-level frame of the entire window. If there is no frame structure, it behaves the same as _self. |
<a href="https://www.google.com/" target="_blank">Go to Google in a New Tab</a>
download Attribute
This attribute indicates that the resource is intended to be downloaded rather than displayed in the browser. You can specify a new filename using the download attribute's value.
Basic Download
If no value is specified for the download attribute, clicking the link downloads the report.pdf file.
<a href="/files/report.pdf" download>Download Report</a>
Specifying a New Filename
If a value is specified for the download attribute, the file is downloaded using the specified filename.
<a href="/files/report.pdf" download="2024_report.pdf">Download Report</a>
The filename you specify does not necessarily have to use the same file extension as the original file. However, to ensure that the file can be opened and used correctly, it is recommended that the extension match the original file type. Using an incorrect extension may cause problems when opening the file.
For example, if the original file is named image.jpg, it is recommended that the filename specified in the download attribute also use the .jpg extension. This helps ensure that the file can be opened and used correctly.
Things to Keep in Mind
Resources that browsers can display directly, such as image files with extensions like .jpg and .png, or .pdf files, may be displayed in the browser instead of being downloaded. This is primarily due to the browser's default behavior. You can use appropriate HTTP headers on the server to force a download.
In addition, for security reasons, most browsers restrict the downloading of files from external domains using the download attribute. To address this limitation, the server must be configured with appropriate HTTP headers.
rel Attribute
The rel attribute explicitly specifies the relationship between the current document and the linked resource.
<a href="next-page.html" rel="next">Next Page</a>
For more information, see HTML rel Attribute – Meaning, List, and Examples.
Technical Summary
| Permitted parents | Any element that accepts flow content, but not other <a> elements. |
|---|---|
| Permitted content | Transparent, but there must be no interactive content descendant, <a> element descendant, or descendant with the tabindex attribute specified. |
Implicit ARIA role |
|
Permitted ARIA roles |
|
Browser Compatibility
| Tag and Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<a>
|
1 | 12 | 1 | 1 |
href
|
1 | 12 | 1 | 1 |
target
|
1 | 12 | 1 | 1 |
download
|
14 | 18 | 20 | 10.1 |
rel
|
1 | 12 | 1 | 1 |
Specifications
| Specification | |
|---|---|
<a>
|
HTML Standard #the-a-element |
