HTML links
HTML links (anchors) are the core part of navigation between web pages.
HTML uses links to connect to another document on the Web.
Links in HTML are elements used to navigate between different web pages.
Links are typically used to associate one web page with another web page or resource (such as a document, image, audio file, etc.).
Links allow users to click on text or images while browsing to jump to other locations, thereby achieving interconnection between web pages.
HTML links are created through the tag, and are typically used to navigate users from one page to another, jump from one section to another within a page, download files, open email applications, or execute JavaScript functions, etc.
Basic Syntax
链接文本
The tag defines a hyperlink (anchor). It is the main tag used in HTML to create clickable links.hrefThe attribute specifies the target URL. When the link is clicked, the browser will navigate to this URL.
The following example demonstrates how to create links in an HTML document:
Example
This text
is a link to a page within this website.
is a link to a page on the World Wide Web.
Try it »
HTML Hyperlinks (Links)
HTML uses the tag to set up hypertext links.
A hyperlink can be a single character, a word, a group of words, or an image. You can click on these to jump to a new document or to a section within the current document.
When you move the mouse pointer over a link on a web page, the arrow changes into a small hand.
Inside the tag the href attribute is used to describe the address of the link.
By default, links will appear in the browser in the following forms:
- An unvisited link is displayed as blue text with an underline.
- A visited link is displayed as purple with an underline.
- When the link is clicked, it is displayed as red with an underline.
Note: If CSS styles are set for these hyperlinks, the display style will follow the CSS settings.
HTML Link Attributes
The href attribute describes the target of the link.
1. href: Defines the link target.
This is the most important attribute of a hyperlink, used to specify the destination of the link, which can be another web page, a file, an email, a phone number, or JavaScript.
Example
Visit Example
2. target: Defines how the link opens.
_blank: Opens the link in a new window or new tab._self: Opens the link in the current window or tab (default)._parent: Opens the link in the parent frame._top: Opens the link in the entire window, canceling any frames.
Example
Open Example in a New Window
3. rel: Defines the relationship between the link and the target page.
nofollow: Indicates that search engines should not follow this link, commonly used for external links.
noopener and noreferrer: Prevent security issues when opening links in a new tab, especially when using target="_blank".
noopener: Prevents the new browsing context (page) from accessingwindow.openerThe property andopenmethod.noreferrer: Does not send the referer header (i.e., does not tell the target site where you came from).noopener noreferrer: Use bothnoopenerandnoreferrer. Example:安全链接
Example
Secure Link
4. download: Instructs the browser to download the link target instead of navigating to it.
If a file name is specified, the browser will prompt to download and save it with the specified file name.
For example:
Example
Download Files
5. title: Defines extra information about the link, displayed as a tooltip when the mouse hovers over the link.
Example
Visit Example
6. id: Used for link anchors, typically to jump to a specific location within the same page.
Example
Jump to Part 1
This is Part 1
7. hreflang: Specifies the language of the link's target URL
Example
Visit the Spanish-language website
8. type: Specifies the MIME type of the linked resource
Example
Style sheet
9. class: Used to specify the class name of the element (defined in CSS)
Example
External Link
10. style: Defines CSS styles directly on the element
Example
Red Link
Example
访问Runoob Tutorial
The code above is displayed as:Visit the Runoob Tutorial
Clicking this hyperlink will take the user to the home page of Runoob Tutorial.
Tip: "Link text" does not have to be text. Images or other HTML elements can also become links.
**Text links:** The most common type of link is the text link, which uses elements to turn a piece of text into a clickable link, for example:
访问示例网站
**Image links:** You can also use an image as a link. In this case, elements surround the element. For example:
**Anchor links:** In addition to linking to other web pages, you can also create internal links within the same page, which are called anchor links. To create an anchor link, you need to use the element to define a marker at the target location, and reference that marker using the # symbol. For example:
跳转到第二部分
**Download links:** If you want a link to download a file rather than navigate to another web page, you can use the download attribute. For example:
下载文档
HTML Links - The target Attribute
With the target attribute, you can define where the linked document will be displayed.
The following line opens the document in a new window:
Example
<a href="https://www.runoob.com/" target="_blank" rel=">noopener noreferrer
"
Visit Runoob Tutorial!
a
Try it »
HTML Links - The id Attribute
The id attribute can be used to create a bookmark within an HTML document.
Tip: A bookmark is not displayed in any special way; that is, it is not visible in the HTML page, so it is hidden from readers.
Example
Insert an ID in the HTML document:
有用的提示部分
Create a link in the HTML document to the "Useful Tips section (id="tips")":
访问有用的提示部分
Or, create a link from another page to the "Useful Tips section (id="tips")":
访问有用的提示部分
Empty Links
The following are several common ways to set empty links, and the differences between them:
| Method | Purpose | Whether it will navigate | Scenario applicability |
|---|---|---|---|
href="#" |
Navigate to the top of the page | Is | Placeholder that captures click events |
href="javascript:void(0)" |
Prevent the default behavior without refreshing the page | No | Prevent navigation, used together with JS |
href="" |
Refresh the current page | Is | When a page refresh is needed |
href="about:blank" |
Open a blank page | Is | Open a blank page in a new window |
role="button" |
The link behaves as a button with no default behavior | No | Used with JS to implement button functionality, no navigation |
Basic Notes - Useful Tips
Note: Always add a forward slash to subfolders. If you write a link like this: href="https://www.runoob.com/html", it will generate two HTTP requests to the server. This is because the server adds a forward slash to this address and then creates a new request, like this: href="https://www.runoob.com/html/".
More examples
Image Links
How to use image links.
Linking to a Specific Location on the Current Page
How to use bookmarks
Break out of the frame
This example demonstrates how to break out of a frame if your page is locked within one.
Creating an Email Link
This example demonstrates how to link to an email. (This example only works after a mail client program is installed.)
Creating an Email Link 2
This example demonstrates a more complex email link.
HTML Link Tags
| tag | Description |
|---|---|
| Defines a hyperlink |