HTML links function as hyperlinks, enabling you to navigate to other documents with a click. When you hover your cursor over a link, it changes into a small hand symbol.
Note: A link is not restricted to text. It can be an image or any other HTML element! |
The <a> tag in HTML is used to create a hyperlink, and it is structured as follows:
<a href=”url“>link text</a> |
The href attribute of the <a>
element is crucial, as it specifies the destination of the link.
The visible portion to users is the link text.
By clicking on this text, users are directed to the designated URL.
<a href=”https://code7school.com/”>Visit code7school.com!</a> |
Normally, clicking a link opens the linked page in the same browser window. To alter this behavior, you can set a different target for the link using the target attribute.
The target attribute determines the location where the linked document will open.
Possible values for the target attribute include:
<a href=”https://code7school.com/” target=”_blank”>Visit code7school.com!</a> |
Both examples above employ an absolute URL (a complete web address) within the href attribute.
A local link (a link to a page within the same website) is designated with a relative URL (excluding the “https://www” portion):
Example
<h2>Absolute URLs</h2> <p><a href=”https://www.w3.org/”>W3C</a></p> <p><a href=”https://www.google.com/”>Google</a></p> <h2>Relative URLs</h2> <p><a href=”html_images.asp”>HTML Images</a></p> <p><a href=”/css/default.asp”>CSS Tutorial</a></p> |
To make an image into a link, simply enclose the <img> tag within the <a> tag.
<a href=”default.asp”> <img src=”smiley.gif” alt=”HTML tutorial” style=”width:42px;height:42px;”> </a> |
To generate a link that opens the user’s email program to compose a new email, include “mailto:” within the href attribute.
<a href=”mailto:[email protected]”>Send email</a> |
To transform an HTML button into a link, JavaScript code must be incorporated.
JavaScript enables you to define actions for specific events, such as clicking a button.
<button onclick=”document.location=’default.asp'”>HTML Tutorial</button> |
<a href=”https://www.code7school.com/courses” title=”Go to code7 course section”>Visit Tutorial’s</a> |
Link to a webpage using the complete URL:
<a href=”https://www.code7school.com/courses”>Tutorial’s</a> |
tag to create a hyperlink.<img>
tag within an <a>
tag to make an image function as a link.