Skip to main content

HTML Links

Links are found on nearly every web page. They let users click their way from one page to another.


HTML Links - Hyperlinks

HTML links are hyperlinks. You click a link to jump to another document, and when you move the mouse over a link the pointer turns into a little hand.

Note: A link does not have to be text. It can be an image or any other HTML element.


HTML Links - Syntax

The <a> tag defines a hyperlink. Its most important attribute is href, which sets the link's destination. The link text is the visible part the reader clicks to go to that address.

By default, an unvisited link is underlined and blue, a visited link is underlined and purple, and an active link is underlined and red.

Tip: Links can be styled with CSS to look different.

Example

A link to PromptCookbook.

Prompt
A page with a single hyperlink to https://prompt-cookbook.com with the visible text "Visit PromptCookbook!"
Output: complete HTML page

Link syntax

The shape of a hyperlink.

index.html
<a href="url">link text</a>

HTML Links - The target Attribute

By default a link opens in the current window. The target attribute changes where the linked document opens. Its values are: _self (the default, same window or tab), _blank (a new window or tab), _parent (the parent frame), and _top (the full window).

Example

A link that opens in a new tab with target="_blank".

Prompt
A page with a link that opens in a new browser tab using target="_blank", to https://prompt-cookbook.com with text "Visit PromptCookbook!"
Output: complete HTML page

Absolute URLs vs. Relative URLs

An absolute URL is a full web address, like https://www.w3.org/. A relative URL points to a page within the same website and leaves out the https://www part.

Example

A page with both absolute and relative links.

Prompt
A page with two groups of links: absolute URLs (Linkedin and Google with full addresses) and relative URLs (HTML Images via PromptCookbook.jpg and HTML Tutorial via /html/html-intro), each group under an h2.
Output: complete HTML page

HTML Links - Use an Image as a Link

To use an image as a link, put the <img> tag inside the <a> tag.

Example

An image that works as a link.

Prompt
A page using an image as a link: an img (src /sites/default/files/html/HtmlLinks/smiley.gif, alt "HTML tutorial", 42x42 pixels) wrapped inside an a tag pointing to home page.
Output: complete HTML page

Link to an Email Address

Use mailto: inside the href to make a link that opens the user's email program with a new message.

Example

A link that starts a new email.

Prompt
A page with a link that opens the user's email program using mailto: in the href, with text "Send email".
Output: complete HTML page

Button as a Link

To make a button act as a link, add a little JavaScript that runs when the button is clicked and changes the page location.

Example

A button that navigates like a link when clicked.

Prompt
A page with an HTML button that acts as a link by using onclick JavaScript to change document.location to https://prompt-cookbook.com, with button text "HTML Tutorial".
Output: complete HTML page

Link Titles

The title attribute gives extra information about an element, usually shown as a tooltip when the mouse moves over it.

Example

A link with a tooltip from its title attribute.

Prompt
A page with a link that has a title attribute giving tooltip text "Go to PromptCookbook HTML section", linking to the PromptCookbook HTML tutorial with text "Visit our HTML Tutorial".
Output: complete HTML page

More on Absolute URLs and Relative URLs

You can link with a full URL, a path relative to the domain, or a path relative to the current page's folder.

Full URL

A link using a full web address.

Prompt
A page with a link using a full URL: https://prompt-cookbook.com with text "HTML tutorial".
Output: complete HTML page

Relative to domain

A link to a page in the html folder of the current site.

Prompt
A page with a link to a page in the html folder of the current site: /html/html-intro with text "HTML tutorial".
Output: complete HTML page

Same folder

A link to a page in the same folder.

Prompt
A page with a link to a page in the same folder: /html/html-intro with text "HTML tutorial".
Output: complete HTML page

Chapter Summary

To recap: use the <a> element to make a link, the href attribute to set its address, and the target attribute to set where it opens. Put an <img> inside an <a> to use an image as a link, and use mailto: in the href to open the user's email program.


HTML Link Tags

The main tag here is <a>, which defines a hyperlink.