Definition and Usage
The <link> tag is used to link the current document to external resources by specifying the relationship between them.
- The
hrefattribute specifies the path to the external resource to be linked. - In this case, the
relattribute must be specified to define the relationship with the external resource. - It is used to connect various external resources, such as stylesheets, icons, fonts, related documents, and RSS feeds.
A document can contain multiple <link> tags.
The <link> tag is a void element that cannot contain any content. This means that it does not have a closing tag (</link>).
Syntax and Structure
Basic Structure
This is the basic syntax structure of the <link> tag.
<link rel="rel attribute value" href="external resource location">
The <link> tag has a basic structure that specifies the relationship with an external resource using the rel attribute and specifies the location of the external resource to be linked using the href attribute.
<link rel="stylesheet" href="styles.css">
<link rel="icon" href="favicon.ico">
The example code above clearly demonstrates the basic structure of the <link> tag.
In the <link> tag, the rel attribute specifies the relationship between the document and the linked resource, and this relationship is defined by the value of the rel attribute. Each link relationship has its own defined functionality. Therefore, the functionality of the <link> tag varies depending on this link relationship with the external resource.
Please refer to the Link Relationships Included in the <link> Tag.
Syntax with Additional Optional Attributes
When additional optional attributes are specified depending on the functionality of the <link> tag, the structure is as follows.
<!-- For Boolean attributes -->
<link rel="rel attribute value" href="external resource location" attribute>
<!-- For Enumerated attributes -->
<link rel="rel attribute value" href="external resource location" attribute-name="attribute value">
An HTML attribute that does not require a value is called a Boolean attribute.
Conversely, an attribute that requires a value is called an Enumerated attribute.
For additional optional attributes specified depending on the functionality of the <link> tag, please refer to the Attributes Used in the <link> Tag.
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="print.css" media="print">
Link Relationships Included in the <link> Tag
The <link> tag links the current document to other resources and uses the rel attribute to specify the link relationship between the current document and the linked external resource.
Link Relationship Between an External Resource and the Current Document
A link relationship between an external resource and the current document refers to the way the current document and the external resource are connected, and it is specified using the rel attribute and its values. This allows the relationship with external resources to be explicitly defined.
The values of the rel attribute used in the <link> tag include standard values defined in the specification as well as non-standard values that are widely used.
This article examines the commonly used rel attribute values in the <link> tag.
Common rel Attribute Types and Values Used in the <link> Tag
rel="stylesheet" |
Used to link a CSS stylesheet to an HTML document. A CSS stylesheet is used to apply styles to an HTML document. |
|---|---|
rel="icon" |
Used to link an icon to an HTML document. This icon, commonly known as a favicon, is a small image displayed in browser tabs, bookmark lists, website icons, and similar locations. |
rel="alternate"
|
Represents a link relationship to an alternate version of a document. It is used to explicitly indicate the relationship between different versions of the same content, such as multilingual versions, print versions, and mobile versions. |
rel="canonical"
|
Indicates that the linked document is the canonical (preferred) version. It is primarily used to resolve duplicate content issues and explicitly tells search engines which URL should be treated as the canonical version. |
rel="preconnect"
|
Represents a relationship that preconnects to the server hosting an external resource. In other words, it is used to establish a connection to the resource's server in advance. This helps optimize page loading performance by reducing the time required to load the HTML document. |
rel="prefetch"
|
Indicates that the linked resource is intended to be prefetched. It provides a hint to the browser to fetch a specific resource in advance and store it in the cache. This can reduce loading time when the user later accesses the resource. However, it should be used carefully because resources that are never used may be fetched unnecessarily. It is intended for resources that are not required for rendering the current page but may be useful to the user. |
rel="dns-prefetch"
|
Represents a relationship that performs a DNS lookup for a linked external domain in advance and caches the DNS lookup result. This reduces the latency when connecting to the linked domain, helping improve page loading performance. |
rel="preload"
|
Represents a link relationship for preloading a resource. It instructs the browser to prioritize loading the specified resource. This allows the browser to download the resource in advance, reducing loading time. Unlike rel="prefetch", which is intended for resources that are not required for rendering the current page but may be useful later, rel="preload" is used for resources that are required to render the current page. |
There are various other values besides these. These values are called Link types values. For more information, please refer to MDN - Link types.
Examples of <link> Tag Link Relationships
The following examples demonstrate the most common link relationships used with the <link> tag.
rel="stylesheet"
Used to link a CSS stylesheet to an HTML document. A CSS stylesheet is used to apply styles to an HTML document.
<link rel="stylesheet" href="styles.css">
This links the styles.css file to the current document as a CSS stylesheet.
You can use the media attribute to specify a media type or media query. The following example loads the print.css file only for print media.
<link rel="stylesheet" href="print.css" media="print">
rel="icon"
Used to link an icon to an HTML document. This icon, commonly known as a favicon, is a small image displayed in browser tabs, bookmark lists, website icons, and similar locations.
<link rel="icon" href="favicon.ico">
This sets the favicon.ico file as the favicon for the current document.
rel="canonical"
Indicates that the linked document is the canonical (preferred) version. It is primarily used to resolve duplicate content issues and explicitly tells search engines which URL should be treated as the canonical version.
<link rel="canonical" href="https://www.example.com">
This specifies one URL as the canonical URL among multiple URLs with the same content, helping resolve duplicate content issues. It plays an important role in helping Google Search identify the canonical URL.
rel="preconnect"
Represents a relationship that preconnects to the server hosting an external resource. In other words, it is used to establish a connection to the resource's server in advance. This helps optimize page loading performance by reducing the time required to load the HTML document.
The crossorigin attribute specifies that CORS (Cross-Origin Resource Sharing) should be used when fetching the resource through a cross-origin request.
The following example is commonly used when loading external resources such as web fonts. Since web fonts are typically hosted on external servers, allowing the browser to establish a connection to the server in advance enables the fonts to be downloaded more quickly.
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital@0;1&display=swap">
This example establishes a preconnection for the browser to use the Roboto Mono font from Google Fonts and then loads the corresponding font stylesheet.
In the first <link> tag, rel="preconnect" provides a hint to the browser to establish a connection in advance to https://fonts.gstatic.com, which is specified by the href attribute. This allows the browser to fetch the font resources more quickly.
In the second <link> tag, a stylesheet is linked to load the Roboto Mono font from Google Fonts. The stylesheet is retrieved from the Google Fonts API using the href attribute. Since the connection has already been established, the browser can retrieve the required stylesheet more efficiently.
Attributes Used in the <link> Tag
Let's take a look at the attributes used in the <link> tag.
The <link> tag supports all global attributes.
| Attribute | Value | Description |
|---|---|---|
rel |
See the standard values defined in the HTML specification.
|
Specifies the relationship with the external resource. |
href |
URL | Specifies the URL of the external resource. |
as |
Resource type | Specifies the resource type when used with rel="preload". |
crossorigin |
crossorigin or crossorigin="", is equivalent to anonymous. |
Specifies whether CORS (Cross-Origin Resource Sharing) should be used when fetching the resource. |
hreflang |
Language code string | Specifies the language of the linked resource. |
media |
Media query string | Specifies a media type or media query so that the resource is used only under those conditions. |
integrity |
Hash value string | Specifies a hash value used to verify the integrity of the resource. |
type |
MIME type string | Specifies the MIME type of the external resource. |
sizes |
Pixel value | Specifies the size of the icon. |
referrerpolicy |
|
Specifies how referrer information is sent. |
title |
Text string | Defines the preferred or an alternate stylesheet when used with rel="stylesheet". |
Optional Attributes Used with the <link> Tag
Besides the rel and href attributes that are typically used with the <link> tag, there are also optional attributes that can be specified depending on its purpose. Let's take a look at some of the most common ones through examples.
as
The as attribute specifies the resource type when used with rel="preload".
The following example shows how to use the as attribute with the <link> tag. It explicitly tells the browser what type of resource is being preloaded so that it can handle the resource appropriately.
<link rel="preload" href="style.css" as="style">
When the as attribute is used together with rel="preload", a CSS file can be preloaded. The as attribute tells the browser that the resource is a stylesheet, allowing it to handle the resource appropriately and optimize page loading performance.
crossorigin
The crossorigin attribute specifies whether CORS (Cross-Origin Resource Sharing) should be used when fetching the linked resource.
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital@0;1&display=swap">
This example is commonly used when loading cross-origin resources such as web fonts. The crossorigin attribute instructs the browser to fetch the resource using CORS. When used together with rel="preconnect", it also allows the browser to establish a connection to the external server in advance, enabling resources such as fonts to be loaded more efficiently.
hreflang
The hreflang attribute specifies the language of the linked external resource.
The following example shows how to use the hreflang attribute with the <link> tag. It demonstrates how to reference documents written in different languages to support a multilingual website.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Multilingual Web Page</title>
<!-- Link to the English document -->
<link rel="alternate" hreflang="en" href="english_page.html">
<!-- Link to the French document -->
<link rel="alternate" hreflang="fr" href="french_page.html">
<!-- Link to the Spanish document -->
<link rel="alternate" hreflang="es" href="spanish_page.html">
</head>
<body>
<h1>Multilingual Web Page</h1>
<p>This web page supports multiple languages.</p>
<!-- Web page content -->
</body>
</html>
This example shows <link> tags that reference documents in three different languages. Each link uses the rel="alternate" attribute together with the appropriate hreflang value for its language. This allows search engines to understand that each link points to an alternate version of the document provided in a specific language.
sizes
The sizes attribute specifies the size of an icon. It is primarily used with rel="icon" to provide icons in different sizes.
<link rel="icon" href="favicon-normal.png" sizes="32x32">
<link rel="icon" href="favicon-retina.png" sizes="192x192">
This allows the browser to select and load the icon that best matches the required display size.
Things to Keep in Mind
Although HTML and CSS are web standards, browsers differ in how they implement those standards.
In particular, many features of the <link> tag vary depending on the browser and device, and there are many additional features and implementation details that are beyond the scope of this article. Therefore, web developers should test their websites across different browsers and devices and, when necessary, apply browser-specific or device-specific solutions to provide the best possible user experience.
Technical Summary
| Permitted parents |
|
|---|---|
| Permitted content | None; it is a void element. |
Implicit ARIA role |
link with href attribute |
Permitted ARIA roles |
No role permitted |
Specifications
| Specification | |
|---|---|
<link>
|
HTML Standard #the-link-element |
Browser Compatibility
| Tag |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<link>
|
1 | 12 | 1 | 4 |