Links in HTML, also known as hyperlinks, are used to create connections between web pages. Links are created using the <a> element (stands for “anchor”), which is used to define the start and end of a link. The href attribute of the <a> element is used to specify the destination of the link.
Here is an example of how you can create a link in HTML:
<a href="https://www.example.com">Click here to visit example.com</a>
This will create a link with the text “Click here to visit example.com” that, when clicked, will take the user to the website https://www.example.com.
You can also use the target attribute of the <a> element to control where the linked webpage will be opened. For example, target=”_blank” will open the linked webpage in a new browser window or tab. There are others such as : _self, _parent, _top.
Here is an example of how you can use the target attribute:
<a href="https://www.example.com" target="_blank">Click here to visit example.com in a new window</a>
This will create a link that, when clicked, will open the website Click here to visit example.com in a new window in a new browser window or tab.
<a> Attributes
Here are some other attributes that you can use with the <a> element in HTML:
download:
This attribute specifies that the linked resource should be downloaded rather than opened in the browser. For example, download=”report.pdf” will cause the linked resource to be downloaded with the name “report.pdf”.
hreflang:
This attribute specifies the language of the linked resource. For example, hreflang=”fr” indicates that the linked resource is in French.
rel:
This attribute specifies the relationship between the current webpage and the linked resource. Possible values include alternate, author, bookmark, help, license, next, nofollow, noreferrer, prefetch, prev, search, tag.
type:
This attribute specifies the media type of the linked resource. For example, type=”application/pdf” indicates that the linked resource is a PDF file.
Here is an example of how you can use some of these attributes:
<a href="report.pdf" download="annual-report.pdf" hreflang="en" rel="bookmark" type="application/pdf">Click here to download the annual report</a>
This will create a link to a PDF file that, when clicked, will be downloaded with the name “annual-report.pdf” and that is in English and relates to the current webpage as a bookmark.
Difference between Absolute URLs and Relative URLs
In HTML, a URL (Uniform Resource Locator) is used to specify the location of a web resource, such as a webpage or an image. There are two types of URLs: absolute URLs and relative URLs.
Absolute URL
An absolute URL is a complete URL that includes all the information needed to locate the resource, including the protocol (e.g., http or https), the domain name (e.g., www.example.com), and the path to the resource (e.g., /images/logo.png). Absolute URLs are used to reference resources on other websites.
Here is an example of an absolute URL:
<a href="https://www.example.com/images/logo.png">This is an absolute URL</a>
Relative URL
A relative URL, on the other hand, is a URL that is relative to the current webpage. It only includes the path to the resource and does not include the protocol or domain name. Relative URLs are used to reference resources on the same website as the current webpage.
Here is an example of a relative URL:
<a href="/images/logo.png">This is a relative URL</a>
The advantage of using relative URLs is that they are shorter and easier to read, and they allow you to change the domain name or protocol of your website without having to update all the links. However, absolute URLs are more reliable and are recommended for external links.
Using an Image as a Link
To use an image as a link in HTML, you can nest the <img> element inside an <a> element. The <img> element is used to insert an image into a webpage, and the <a> element is used to create a link.
Here is an example of how you can use an image as a link:
<a href="https://www.example.com">
<img src="/images/logo.png" alt="Example Website">
</a>
This will create a link that, when clicked, will take the user to the website “https://www.example.com”. The image specified by the src attribute of the <img> element will be used as the link. The alt attribute of the <img> element is used to specify an alternate text for the image, which will be displayed if the image cannot be loaded.
You can also use the title attribute of the <a> element to specify a tooltip for the link. The tooltip will be displayed when the user hovers over the link.
Here is an example of how you can use the title attribute:
<a href="https://www.example.com" title="Visit Example Website">
<img src="/images/logo.png" alt="Example Website">
</a>
This will create a link with a tooltip that says Visit Example Website when the user hovers over the link.
Using a Link to an Email Address
To create a link to an email address in HTML, you can use the mailto URL scheme in the href attribute of the <a> element. The mailto URL scheme specifies that the link should open the user’s default email client with the specified email address in the “To” field.
Here is an example of how you can create a link to an email address in HTML:
<a href="mailto:info@example.com">Send an email to info@example.com</a>
This will create a link with the text “Send an email to info@example.com” that, when clicked, will open the user’s default email client with the email address info@example.com in the “To” field.
You can also use the subject and body parameters of the mailto URL to specify the subject and body of the email, respectively. The subject and body parameters should be URL encoded.
Here is an example of how you can use the subject and body parameters:
<a href="mailto:info@example.com?subject=Inquiry&body=Hello,%0A%0AI%20have%20a%20question%20regarding%20your%20products.%0A%0AThank%20you,%0A[Your%20Name]">
Send an email to info@example.com
</a>
This will create a link “Send an email to info@example.com” that, when clicked, will open the user’s default email client with the email address info@example.com in the To field, the subject Inquiry, and the body:
“Hello,
I have a question regarding your products.
Thank you, [Your Name]”.
Using a Button as a Link
To use a button as a link in HTML, you can nest the <button> element inside an <a> element. The <button> element is used to create a button that the user can interact with, and the <a> element is used to create a link.
Here is an example of how you can use a button as a link in HTML:
<a href="https://www.example.com">
<button>Click here to visit example.com</button>
</a>
This will create a button with the text that, when clicked, will take the user to the website https://www.example.com.
You can also use the title attribute of the <a> element to specify a tooltip for the button. The tooltip will be displayed when the user hovers over the button.
Here is an example of how you can use the title attribute:
<a href="https://www.example.com" title="Visit Example Website">
<button>Click here to visit example.com</button>
</a>
This will create a button with a tooltip that says Visit Example Website when the user hovers over the button.