Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to make link open in new tab HTML

Before we dive into the specifics of how to make a link open in a new tab in HTML, let's first establish what a hyperlink is. Imagine you're reading a book and you come across a term you're unfamiliar with. You'd likely flip to the glossary at the back, find the term, and read its definition. In the digital world, hyperlinks function similarly. They are clickable elements that redirect the user to another location, be it a different section of the same webpage or a completely different website altogether.

HTML, or HyperText Markup Language, uses a tag system to create these hyperlinks. The 'a' tag is used to define the hyperlink. The syntax is simple:

<a href="URL">Clickable Text</a>

The 'href' attribute specifies the destination URL, and the text between the opening <a> tag and the closing </a> tag is the text that users will see and click on.

Now that we have a basic understanding of hyperlinks, let's explore how to make them open in a new tab.

The key to this is the 'target' attribute of the 'a' tag. The 'target' attribute specifies where to open the linked document. It can have several values, but the one we are interested in is _blank. This value tells the browser to open the linked document in a new tab or window.

Here's the syntax with the 'target' attribute:

<a href="URL" target="_blank">Clickable Text</a>

With this line of code, when a user clicks on 'Clickable Text', the link will open in a new tab.

You might be wondering why you'd want to open links in new tabs. In the realm of user experience, it's a bit of a contentious issue. Some argue it disrupts the user's flow, while others maintain it can help keep users on your site.

Consider it like this: You're in a library, engrossed in a book, and you come across a reference to another book. If you had to close the first book to open the second one, it would likely be annoying, especially if you wanted to continue reading the first book. Opening a link in a new tab is akin to laying the second book open next to the first, allowing you to reference both simultaneously.

Adding Extra Security

When using target="_blank", it's also recommended to add rel="noopener noreferrer" to the 'a' tag.

<a href="URL" target="_blank" rel="noopener noreferrer">Clickable Text</a>

The rel attribute defines the relationship between the current document and the linked document. The noopener value prevents the new page from being able to access the window object of the original page via the window.opener property, which could potentially be a security risk. The noreferrer value prevents the new page from getting referrer information. This is particularly useful to prevent the new page from knowing where the request came from, which can help in protecting user's privacy.

Conclusion

HTML provides a simple yet robust way to create hyperlinks through the 'a' tag. Adding the target="_blank" attribute enables these links to open in new tabs, providing users with the convenience of viewing multiple pages simultaneously. However, it's important to also consider the potential security and privacy implications, which is why adding rel="noopener noreferrer" is recommended.

Like learning any new language, mastering HTML takes practice. The more you write and experiment with your code, the more intuitive it will become. Keep practicing, and soon, these tags and attributes will be second nature. Happy coding!