Ora

How do I add a link to open in new tab?

Published in HTML Link Management 3 mins read

To make a link open in a new tab, you need to use the HTML <a> (anchor) tag and include the target="_blank" attribute. This attribute instructs the browser to open the linked document in a new window or tab.

How to Create a Link that Opens in a New Tab

Adding a link that opens in a new tab is a straightforward process in HTML. It involves two key attributes within your <a> tag: href and target.

Understanding the <a> Tag

The <a> tag is fundamental for creating hyperlinks on web pages. It takes the href attribute, which specifies the URL (web address) of the page you want to link to.

Basic Link Structure:

<a href="https://www.example.com">Link Text</a>

Here, "https://www.example.com" is the destination URL, and "Link Text" is what users will see and click on.

Adding target="_blank"

To ensure the link opens in a new tab, you simply add the target="_blank" attribute to your <a> tag. This tells the browser to open the link in a new tab when clicked.

Example:

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

Enhancing Security with rel="noopener noreferrer"

While target="_blank" effectively opens links in new tabs, it introduces a potential security vulnerability known as "tabnabbing." To mitigate this, it's a best practice to also include the rel="noopener noreferrer" attribute when using target="_blank".

  • noopener: Prevents the newly opened page from accessing the window.opener property of the original page. This stops the new page from potentially redirecting your original page to a malicious site.
  • noreferrer: Prevents the new page from knowing the referrer (the page that linked to it). This enhances user privacy and security.

Best Practice Example:

<a href="https://www.crediblesource.com" target="_blank" rel="noopener noreferrer">Learn More (Opens Securely in New Tab)</a>

Practical Applications and Considerations

  • User Experience: Use target="_blank" judiciously. While useful for external links or documents, opening internal links in new tabs can disrupt a user's navigation flow.
  • Accessibility: Inform users when a link will open in a new tab, especially if it's not immediately obvious. You can do this with visual cues (like an icon) or descriptive link text.
  • SEO: Search engines understand and process target="_blank" correctly; it doesn't negatively impact your SEO.

Key Attributes for External Links

Attribute Purpose Example Value
href Specifies the URL the link points to. "https://example.com"
target Defines where to open the linked document (e.g., new tab, same frame). "_blank"
rel Describes the relationship between the current document and the linked document, crucial for security with target="_blank". "noopener noreferrer"

For more detailed information on the anchor tag and its attributes, you can refer to the MDN Web Docs on <a> (Anchor) element.