How to Use the Rel Attribute in HTML

How to Use the Rel Attribute in HTML

ยท

3 min read

The HTML rel attribute is like a secret doorway into the world of web technology. Though often overlooked, it's essential.

A woman smiling while learning about the rel attribute in HTML

Download Now: 25 HTML & CSS Hacks [Free Guide]

Have you ever wondered why some links behave differently from others? This is where the rel attribute plays a crucial role. It helps establish relationships between various elements on your webpage, making your website more user-friendly.

In this article, we'll explain what the rel attribute is, how it works, and its most practical uses. So, grab your flashlight as we explore the world of HTML attributes!

What is the HTML Rel Attribute?

The rel attribute in HTML defines the relationship between a particular link or element and others on your webpage. It's like an invisible bridge, providing browsers with context about the relationship between links.

For instance, if you link to an external website, the rel attribute can direct the browser to open the link in a new tab or trigger a popup. It can also inform search engines about the relationship between different pages.

How Do I Use the Rel Attribute?

Using the rel attribute is straightforward. You just need to add it to your link or element like this: rel="attribute", replacing "attribute" with the relationship type.

For example, to open a link in a new tab, you'd use rel="noopener noreferrer". To signify that two pages are connected, use rel="related". Here are a couple of examples:

html

Copy code

<a href="www.example.com" rel="noopener noreferrer">Open in New Tab</a> <a href="www.relatedexample.com" rel="related">Related Page</a>

Types of Rel Attributes

Here are some common types of rel attributes to use on your site:

  • noopener noreferrer: Opens a link in a new tab and prevents websites from accessing your referrer URL.

  • related: Indicates that two pages are connected.

  • alternate: Suggests an alternative version of the page, like a print-friendly option.

  • canonical: Identifies the primary page if multiple versions exist.

  • help: Links to a page with helpful documentation.

  • appendix: Denotes that the linked page is an appendix.

  • contents: Links to a table of contents.

  • copyright: Indicates a page containing copyright info.

  • index: Identifies a page serving as an index.

  • license: Links to content licensing information.

  • shortcut: Links to a shortcut page.

  • prefetch: Pre-fetches the linked page for faster loading.

  • prev: Links to the previous page in a series.

  • next: Links to the next page in a series.

  • nofollow: Tells search engines not to follow this link.

When to Use the Rel Attribute

The rel attribute can be used in many ways to improve your site. Here are some common use cases:

  • External Links: Add rel="noopener noreferrer" to ensure links open in a new tab and protect your referrer URL.

  • Related Content: Use rel="related" to show the connection between multiple pages on the same topic.

  • Canonical Pages: Specify the primary page version using rel="canonical".

  • Shortcuts: Create a shortcut link with rel="shortcut" to direct visitors to the main page.

  • Copyright & Licensing: Use rel="copyright" and rel="license" to point to copyright and licensing info.

Wrapping Up

The rel attribute is a powerful tool to make your website more user-friendly and better organized. Try these different relationship types to keep your visitors engaged and help search engines understand your content.

coding-hacks

ย