The <link>
specifies relationships between the current document and an external resource. This element is most commonly used to link to stylesheets, but is also used to establish site icons (both "favicon" style icons and mobile home screen/app icons) among other things.
To link an external stylesheet, you'd include a <link>
element inside your <head>
like this:
<link href="main.css" rel="stylesheet">
This simple example provides the path to the stylesheet inside an href
attribute, and a rel
attribute with a value of stylesheet
. The rel
stands for "relationship", and is probably one of the key features of the <link>
element — the value denotes how the item being linked to is related to the containing document. As you'll see from our Link types reference, there are many different kinds of relationship.
There are a number of other common types you'll come across. For example, a link to the site's favicon:
<link rel="icon" href="favicon.ico">
There are a number of other icon rel
values, mainly used to indicate special icon types for use on various mobile platforms, e.g.:
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="apple-icon-114.png" type="image/png">
The sizes
attribute indicates the icon size, while the type
contains the MIME type of the resource being linked. These provide useful hints to allow the browser to choose the most appropriate icon available.
You can also provide a media type or query inside a media
attribute; this resource will then only be loaded if the media condition is true. For example:
<link href="print.css" rel="stylesheet" media="print"> <link href="mobile.css" rel="stylesheet" media="screen and (max-width: 600px)">
Some interesting new performance and security features have been added to the <link>
element too. Take this example:
<link rel="preload" href="myFont.woff2" as="font" type="font/woff2" crossorigin="anonymous">
A rel
value of preload
indicates that the browser should preload this resource (see Preloading content with rel="preload" for more details), with the as
attribute indicating the specific class of content being fetched. The crossorigin
attribute indicates whether the resource should be fetched with a CORS request.
Other usage notes:
<link>
element can occur either in the <head>
or <body>
element, depending on whether it has a link type that is body-ok. For example, the stylesheet
link type is body-ok, and therefore <link rel="stylesheet">
is permitted in the body. However, this isn't a good practice to follow; it makes more sense to separate your <link>
elements from your body content, putting them in the <head>
.<link>
to establish a favicon for a site, and your site uses a Content Security Policy (CSP) to enhance its security, the policy applies to the favicon. If you encounter problems with the favicon not loading, verify that the Content-Security-Policy
header's img-src
directive is not preventing access to it.<link>
element, but it is unclear how they would be used.<link>
require a trailing slash: <link />
.next
for rel
to preload the next page in a document series.This element includes the global attributes.
as
rel="preload"
has been set on the <link>
element. It specifies the type of content being loaded by the <link>
, which is necessary for content prioritization, request matching, application of correct content security policy, and setting of correct Accept
request header.crossorigin
<canvas>
element without being tainted. The allowed values are: anonymous
Origin
HTTP header) is performed, but no credential is sent (i.e. no cookie, X.509 certificate, or HTTP Basic authentication). If the server does not give credentials to the origin site (by not setting the Access-Control-Allow-Origin
HTTP header) the image will be tainted and its usage restricted.use-credentials
Origin
HTTP header) is performed along with a credential sent (i.e. a cookie, certificate, and/or HTTP Basic authentication is performed). If the server does not give credentials to the origin site (through Access-Control-Allow-Credentials
HTTP header), the resource will be tainted and its usage restricted.Origin
HTTP header), preventing its non-tainted usage. If invalid, it is handled as if the enumerated keyword anonymous was used. See CORS settings attributes for additional information.href
hreflang
href
attribute is present.importance
auto
: Indicates no preference. The browser may use its own heuristics to decide the priority of the resource.
high
: Indicates to the browser that the resource is of high priority.
low
: Indicates to the browser that the resource is of low priority.
importance
attribute may only be used for the <link>
element if rel="preload"
or rel="prefetch"
is present.integrity
media
print
, screen
, aural
, braille
. HTML5 extended this to any kind of media queries, which are a superset of the allowed values of HTML 4.referrerpolicy
'no-referrer'
means that the Referer
header will not be sent.'no-referrer-when-downgrade'
means that no Referer
header will be sent when navigating to an origin without TLS (HTTPS). This is a user agent’s default behavior, if no policy is otherwise specified.'origin'
means that the referrer will be the origin of the page, which is roughly the scheme, the host, and the port.'origin-when-cross-origin'
means that navigating to other origins will be limited to the scheme, the host, and the port, while navigating on the same origin will include the referrer's path.'unsafe-url'
means that the referrer will include the origin and the path (but not the fragment, password, or username). This case is unsafe because it can leak origins and paths from TLS-protected resources to insecure origins.rel
sizes
rel
contains a value of icon
or a non-standard type such as Apple's apple-touch-icon
. It may have the following values: any
, meaning that the icon can be scaled to any size as it is in a vector format, like image/svg+xml
.<width in pixels>x<height in pixels>
or <width in pixels>X<height in pixels>
. Each of these sizes must be contained in the resource.sizes
contains only one entry. MS's ICO format does, as well as Apple's ICNS. ICO is more ubiquitous; you should definitely use it.title
title
attribute has special semantics on the <link>
element. When used on a <link rel="stylesheet">
it defines a preferred or an alternate stylesheet. Incorrectly using it may cause the stylesheet to be ignored.type
rel="preload"
link types, to make sure the browser only downloads file types that it supports.disabled
Note: While there is no disabled
attribute in the HTML standard, there is a disabled
attribute on the HTMLLinkElement
DOM object.
methods
prefetch
target
charset
iso-8859-1
. Content-Type
HTTP header on the linked resource.rev
href
attribute. The attribute thus defines the reverse relationship compared to the value of the rel
attribute. Link type values for the attribute are similar to the possible values for rel
.Note: This attribute is considered obsolete by the WHATWG HTML living standard (which is the specification MDN treats as canonical). However, it's worth noting that rev
is not considered obsolete in the W3C specification. That said, given the uncertainty, relying on rev
is unwise.
Instead, you should use the rel
attribute with the opposite link type value. For example, to establish the reverse link for made
, specify author
. Also this attribute doesn't stand for "revision" and must not be used with a version number, even though many sites misuse it in this way.
The <link>
element has no visual presence on a web document, therefore it has no styling considerations to worry about.
To include a stylesheet in a page, use the following syntax:
<link href="style.css" rel="stylesheet">
You can also specify alternative style sheets.
The user can choose which style sheet to use by choosing it from the View > Page Style menu. This provides a way for users to see multiple versions of a page.
<link href="default.css" rel="stylesheet" title="Default Style"> <link href="fancy.css" rel="alternate stylesheet" title="Fancy"> <link href="basic.css" rel="alternate stylesheet" title="Basic">
You can include links to several different icons on the same page, and the browser will choose which one works best for its particular context using the rel
and sizes
values as hints.
<!-- third-generation iPad with high-resolution Retina display: --> <link rel="apple-touch-icon-precomposed" sizes="144x144" href="favicon144.png"> <!-- iPhone with high-resolution Retina display: --> <link rel="apple-touch-icon-precomposed" sizes="114x114" href="favicon114.png"> <!-- first- and second-generation iPad: --> <link rel="apple-touch-icon-precomposed" sizes="72x72" href="favicon72.png"> <!-- non-Retina iPhone, iPod Touch, and Android 2.1+ devices: --> <link rel="apple-touch-icon-precomposed" href="favicon57.png"> <!-- basic favicon --> <link rel="icon" href="favicon32.png">
You can provide a media type or query inside a media
attribute; this resource will then only be loaded if the media condition is true. For example:
<link href="print.css" rel="stylesheet" media="print"> <link href="mobile.css" rel="stylesheet" media="all"> <link href="desktop.css" rel="stylesheet" media="screen and (min-width: 600px)"> <link href="highres.css" rel="stylesheet" media="screen and (min-resolution: 300dpi)">
You can determine when a style sheet has been loaded by watching for a load
event to fire on it; similarly, you can detect if an error has occurred while processing a style sheet by watching for an error
event:
<script> var myStylesheet = document.querySelector('#my-stylesheet'); myStylesheet.onload = function() { // Do something interesting; the sheet has been loaded } myStylesheet.onerror = function() { console.log("An error occurred loading the stylesheet!"); } </script> <link rel="stylesheet" href="mystylesheet.css" id="my-stylesheet">
load
event fires once the stylesheet and all of its imported content has been loaded and parsed, and immediately before the styles start being applied to the content.You can find a number of <link rel="preload"> examples in Preloading content with rel="preload".
Content categories | Metadata content. If itemprop is present: flow content and phrasing content. |
---|---|
Permitted content | None, it is an empty element. |
Tag omission | As it is a void element, the start tag must be present and the end tag must not be present |
Permitted parents | Any element that accepts metadata elements. If itemprop is present: any element that accepts phrasing content. |
Permitted ARIA roles | None |
DOM interface | HTMLLinkElement |
Specification | Status | Comment |
---|---|---|
Preload | Candidate Recommendation | Defines <link rel="preload"> , and the as attribute. |
Subresource Integrity The definition of '<script>' in that specification. | Recommendation | Added the integrity attribute. |
HTML Living Standard The definition of '<link>' in that specification. | Living Standard | No changes from latest snapshot |
HTML5 The definition of '<link>' in that specification. | Recommendation | Added crossorigin and sizes attributes; extended values of media to any media queries; added numerous new values for rel . |
HTML 4.01 Specification The definition of '<link>' in that specification. | Recommendation | |
Resource Hints The definition of 'prefetch' in that specification. | Working Draft | Added dns-prefetch , preconnect , prefetch , and prerender . |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 1 | Yes | 1 | Yes | Yes | Yes |
charset
|
1 | Yes | 1 | Yes | Yes | Yes |
crossorigin |
25 | No | 18 | No | 15 | ? |
disabled
|
No | Yes | No | Yes | No | No |
href |
1 | Yes | 1 | Yes | Yes | Yes |
hreflang |
1 | Yes | 1 | Yes | Yes | Yes |
integrity
|
45 | No | ? | No | ? | No |
media |
1 | Yes | 1 | Yes | Yes | Yes |
methods
|
No | ? | No | 4 | No | No |
prefetch
|
56 | ? | ? | No | 43 | ? |
referrerpolicy |
51 | No | 50 | No | 38 | 11.1 |
rel |
1 | Yes | 1 | Yes | Yes | Yes |
rel : Alternative stylesheets. |
? | ? | 3 | ? | Yes | ? |
rel.dns-prefetch
|
46 | ? | 3 | ? | ? | ? |
rel.manifest
|
No | ? | ? | ? | ? | ? |
rel.modulepreload
|
66 | ? | ? | ? | 53 | ? |
rel.noopener
|
49 | No | 52
|
No | 36 | 10.1 |
rel.preconnect
|
46 | No | 39
|
No | ? | No |
rel.prefetch
|
? | ? | ? | ? | ? | ? |
rel.preload
|
50 | ? | 56 — 57
|
? | ? | ? |
rel.prerender
|
? | ? | ? | ? | ? | ? |
rev |
1 | Yes | 1 | Yes | Yes | Yes |
sizes
|
No | No | No
|
No | No | No |
target
|
1 | Yes | 1 | Yes | Yes | Yes |
title |
1 | Yes | 1 | Yes | Yes | Yes |
type |
1 | Yes | 1 | Yes | Yes | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | 4 | Yes | Yes | Yes |
charset
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
crossorigin |
? | Yes | No | 18 | ? | ? | Yes |
disabled
|
No | No | Yes | No | No | No | No |
href |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
hreflang |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
integrity
|
45 | 45 | No | ? | ? | No | 5.0 |
media |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
methods
|
No | No | ? | No | No | No | No |
prefetch
|
56 | 56 | ? | ? | 43 | ? | 6.0 |
referrerpolicy |
51 | 51 | No | 50 | 38 | No | 7.2 |
rel |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
rel : Alternative stylesheets. |
? | ? | ? | 4 | ? | ? | ? |
rel.dns-prefetch
|
46 | Yes | ? | 4 | ? | ? | Yes |
rel.manifest
|
39 | 39 | ? | ? | ? | ? | 4.0 |
rel.modulepreload
|
66 | 66 | ? | ? | 53 | ? | ? |
rel.noopener
|
49 | 49 | No | 52
|
32 | 10.3 | 5.0 |
rel.preconnect
|
46 | 42 | No | 39
|
? | No | 4.0 |
rel.prefetch
|
? | ? | ? | ? | ? | ? | ? |
rel.preload
|
50 | 50 | ? | 56 — 57
|
? | ? | 5.0 |
rel.prerender
|
? | ? | ? | ? | ? | ? | ? |
rev |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
sizes
|
No | No | No | No
|
No | No | No |
target
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
title |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
type |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link