Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add hyperlink in HTML

Hyperlinks, often just called "links", are references to data that you can directly access by clicking on them or by tapping a device's touchscreen. They are the fundamental building blocks of the internet, networking web pages and resources through a web of connections. In a way, they're like the digital equivalent of portals, taking you from one place (web page) to another with just a simple click.

The Anchor Element

In HTML, we use the anchor element <a> to create hyperlinks. The anchor tag is written as <a href=" "> in HTML. Here, href stands for "hyperlink reference", and signifies the destination of the hyperlink. The destination could be another webpage, a different section of the same webpage, or even a downloadable file.

In essence, the anchor element is like a teleportation device that takes you to the destination specified in the href attribute.

For example:

<a href="https://www.google.com">Visit Google</a>

In this example, "Visit Google" is the text that will be visible on the webpage, and the URL https://www.google.com is the destination the user will be taken to when they click on the link.

Relative and Absolute URLs

Hyperlinks can point to either absolute URLs or relative URLs. An absolute URL provides the full address to navigate to, including the protocol (http:// or https://), the domain name, and the specific path. It's like giving someone your full home address, including the street, city, and country.

On the other hand, a relative URL points to a file in relation to the current file. It's analogous to telling someone how to get to your kitchen from your living room.

Example of an Absolute URL:

<a href="https://www.example.com/blog">My Blog</a>

Example of a Relative URL:

<a href="contact.html">Contact Me</a>

In the relative URL example, the hyperlink will navigate to the contact.html file in the same directory as the current page.

By default, a clicked hyperlink will open in the current browser tab. However, we can change this behavior by using the target attribute with the _blank value which will open the link in a new tab.

<a href="https://www.example.com" target="_blank">Visit Example.com</a>

It's like opening a new door instead of reusing the existing one.

Linking to Different Sections of the Same Page

We can also use hyperlinks to navigate to different sections of the same page. This is achieved by using the id attribute of the destination element as the href value in the anchor element, prefixed by #.

<a href="#section2">Go to Section 2</a>

<!-- Later in the document -->

<h2 id="section2">Section 2</h2>

In this example, clicking on "Go to Section 2" will take the user to the <h2> element with the id of section2. It's like using an elevator in a building to go directly to the floor you want.

Conclusion

Hyperlinks are an essential part of HTML and the web. They are the bridges that connect different web pages and resources, making the vast expanse of the internet navigable. Whether you're linking to a different website, a file, or a section on the same page, the <a> element and its href attribute are your keys to creating these connections.

Remember, practice makes perfect. So, try creating different types of links in your HTML documents to get a firm grasp on these concepts. Happy coding!