Table of contents
1.
Introduction
2.
Definition and Usage  
3.
Syntax
3.1.
Example
4.
Attribute Values
4.1.
1. stylesheet
4.2.
2. icon
4.3.
3. alternate
4.4.
4. author
4.5.
5. canonical
4.6.
6. preload
5.
Example of HTML link rel Attribute
6.
Importance of the <link> rel Attribute
7.
Supported Browsers
8.
Frequently Asked Questions
8.1.
What is the purpose of the <link> element in HTML?
8.2.
How does the rel attribute improve SEO?
8.3.
Can I use multiple <link> elements in a single HTML document?
9.
Conclusion
Last Updated: Feb 24, 2025
Medium

HTML link rel Attribute

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The rel attribute in the HTML <link> tag specifies the relationship between the linked resource and the current document. It helps browsers understand how the linked file should be used. Common values include stylesheet for CSS files, icon for website icons, and preload for resource optimization. 

HTML link rel Attribute

In this article, you will learn about the rel attribute, its values, and how to use it effectively in HTML.

Definition and Usage  

The `link` tag in HTML is used to connect external resources to a webpage. These resources can include stylesheets, favicons, or metadata that helps browsers understand how to display the page. The `rel` attribute inside the `link` tag specifies the relationship between the current document and the linked resource. For example, if you want to link a CSS file to style your webpage, you use `rel="stylesheet"`.  

Let’s take a look at an example of how the `link` tag with the `rel` attribute works:  

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>Link Rel Example</title>  
    <!-- Linking a CSS file -->  
    <link rel="stylesheet" href="styles.css">  
    <!-- Adding a favicon -->  
    <link rel="icon" href="favicon.ico" type="image/x-icon">  
</head>  
<body>  
    <h1>Welcome to My Website</h1>  
    <p>This is an example of using the link tag with the rel attribute.</p>  
</body>  
</html>  

 

Output

Output

 

In this code:  

  • The first `link` tag connects a CSS file (`styles.css`) to the webpage. The `rel="stylesheet"` tells the browser that the linked file is used for styling.  
     
  • The second `link` tag adds a favicon (a small icon displayed in the browser tab). The `rel="icon"` specifies that the linked file is an icon.  

 

The `rel` attribute is important because it gives meaning to the connection between the webpage and the resource. Without it, the browser wouldn’t know how to handle the linked file.  

Syntax

The basic syntax of the <link> element with the rel attribute is:

<link rel="value" href="URL" />

 

  • rel specifies the relationship type.
     
  • href contains the link to the external resource.

Example

<link rel="stylesheet" href="styles.css" />

 

In this example, the rel="stylesheet" indicates that the linked file is a CSS stylesheet.

Attribute Values

The rel attribute can have multiple values depending on the type of external resource. Some common values include:

1. stylesheet

Links to an external CSS file.

<link rel="stylesheet" href="styles.css" />

2. icon

Defines a shortcut icon (favicon) for the website.

<link rel="icon" href="favicon.ico" type="image/x-icon" />

3. alternate

Specifies an alternative version of the document, such as a different language version.

<link rel="alternate" hreflang="fr" href="example-fr.html" />

4. author

Provides a link to the author's webpage.

<link rel="author" href="https://example.com/author" />

5. canonical

Helps prevent duplicate content issues by specifying the preferred URL.

<link rel="canonical" href="https://example.com/page" />

6. preload

Loads resources in advance to improve performance.

<link rel="preload" href="script.js" as="script" />

Example of HTML link rel Attribute

Here is a real-world example of how multiple <link> elements can be used in a webpage:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML link rel Example</title>
    <link rel="stylesheet" href="styles.css">
    <link rel="icon" href="favicon.ico" type="image/x-icon">
    <link rel="canonical" href="https://example.com/page">
</head>
<body>
    <h1>Understanding the HTML link rel Attribute</h1>
</body>
</html>

 

Output

Output

Explanation:

  • The rel="stylesheet" links an external CSS file.
     
  • The rel="icon" sets a favicon for the page.
     
  • The rel="canonical" defines the preferred URL to avoid duplicate content.

Importance of the <link> rel Attribute

  1. Improves Website Functionality: The rel attribute helps link essential resources like stylesheets, favicons, and scripts.
     
  2. Enhances SEO: The rel="canonical" tag prevents duplicate content issues, improving search rankings.
     
  3. Boosts Performance: Using preload and prefetch can improve loading times.
     
  4. Ensures Accessibility: alternate links help serve different language versions of a page.
     
  5. Defines Relationship Clearly: Search engines and browsers understand the page structure better when the rel attribute is used correctly.

Supported Browsers

The rel attribute is supported in all modern browsers, including:

Attribute ValueChromeFirefoxEdgeSafariOpera
stylesheetYesYesYesYesYes
iconYesYesYesYesYes
canonicalYesYesYesYesYes
preloadYesYesYesYesYes

Frequently Asked Questions

What is the purpose of the <link> element in HTML?

The <link> element is used to define relationships between an HTML document and external resources, such as stylesheets, favicons, and alternate versions.

How does the rel attribute improve SEO?

The rel="canonical" tag prevents duplicate content issues by specifying the preferred URL, helping improve search rankings.

Can I use multiple <link> elements in a single HTML document?

Yes, multiple <link> elements can be used to include stylesheets, favicons, and other resources, ensuring better page functionality.

Conclusion

In this article, we discussed the rel attribute in HTML, which defines the relationship between the current document and the linked resource. It is commonly used in <link> and <a> tags for stylesheets (rel="stylesheet"), icons, alternate links, and more. Understanding the rel attribute helps in optimizing SEO, improving navigation, and enhancing webpage functionality.

Live masterclass