The HTML <link>
element specifies relationships between the current document and an external resource. Possible uses for this element include defining a relational framework for navigation. This Element is most used to link to style sheets.
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 |
This element includes the global attributes.
charset
deletediso-8859-1
. Content-Type
HTTP header on the linked resource.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, no X.509 certificate and no HTTP Basic authentication is sent). 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 with credential is sent (i.e. a cookie, a certificate and HTTP Basic authentication is performed). If the server does not give credentials to the origin site (through Access-Control-Allow-Credentials:
HTTP header), the image will be tainted and its usage restricted.Origin:
HTTP header), preventing its non-tainted used in <canvas>
elements. If invalid, it is handled as if the enumerated keyword anonymous was used. See CORS settings attributes for additional information.disabled
non standardNote: While there is no disabled
attribute in the HTML standard, there is a disabled
attribute on the HTMLLinkElement
DOM object.
The use of disabled
as an HTML attribute is non-standard and only used by some browsers (W3 #27677). Do not use it. To achieve a similar effect, use one of the following techniques:
disabled
attribute has been added directly to the element on the page, do not include the <link>
element instead;disabled
property of the StyleSheet
DOM object via scripting.href
hreflang
href
attribute 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.methods
non standardreferrerpolicy
'no-referrer'
meaning that the Referer
header will not be sent.'no-referrer-when-downgrade'
meaning 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'
meaning that the referrer will be the origin of the page, that is roughly the scheme, the host and the port.'origin-when-cross-origin'
meaning 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'
meaning 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
stylesheet
, and the href attribute is set to the URL of an external style sheet to format the page. WebTV also supports the use of the value next
for rel to preload the next page in a document series.rev
deletedhref
attribute. The attribute thus defines the reverse relationship compared to the value of the rel attribute. Link types values for the attribute are similar to the possible values for rel
. rel
attribute with the opposite link types values, e.g. made
should be replaced by author
. Also this attribute doesn't mean revision and must not be used with a version number, which is unfortunately the case on numerous sites.sizes
rel
contains the icon
link types value. It may have the following values: any
, meaning that the icon can be scaled to any size as it is in a vectorial 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 being more ubiquitous, you should use it.apple-touch-icon
and apple-touch-startup-icon
.target
non standardtitle
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
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 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> function sheetLoaded() { // Do something interesting; the sheet has been loaded } function sheetError() { console.log("An error occurred loading the stylesheet!"); } </script> <link rel="stylesheet" href="mystylesheet.css" onload="sheetLoaded()" onerror="sheetError()">
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.<link>
tag can occur only in the head element; however, there can be multiple occurrences of <link>
.<link>
element. The methods and urn attributes were later removed from the specifications.<link>
element, but it is unclear how they would be used.<link>
require a trailing slash: <link />
.Specification | Status | Comment |
---|---|---|
Subresource Integrity The definition of '<script>' in that specification. | Recommendation | Added the integrity attribute. |
WHATWG 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 |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 1.0 | (Yes) | 1.0 (1.7 or earlier) | (Yes) | (Yes) | (Yes) |
Alternative stylesheets | ? | ? | 3.0 (1.9) | ? | (Yes) | ? |
disabled attribute non standard | No support | (Yes) | No support | (Yes) | No support | No support |
methods attribute non standard | No support | No support | No support | 4.0 | No support | No support |
sizes attribute | No support | No support | No support bug 441770 | No support | No support | No support |
load and error events | 19 (Webkit: 535.23) | ? | 9.0 (9.0) | ? | 11.60 | ? |
crossorigin attribute | 25 | No support | 18.0 (18.0) | ? | 15 | ? |
integrity attribute | 45.0 | No support | ? | ? | ? | No support[1] |
referrerpolicy attribute | ? | No support | 50.0 (50.0) | ? | ? | ? |
Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | 1.0 (1.0) | (Yes) | (Yes) | (Yes) | (Yes) |
Alternative stylesheets | ? | ? | ? | 4.0 (2.0) | ? | ? | ? | ? |
disabled attribute non standard | No support | ? | (Yes) | No support | ? | No support | No support | ? |
methods attribute non standard | No support | ? | No support | No support | 4.0 | No support | No support | ? |
sizes attribute | No support | ? | No support | No support bug 441770 | No support | No support | No support | ? |
load and error events | ? | ? | ? | 9.0 (9.0) | ? | ? | ? | ? |
crossorigin attribute | ? | ? | No support | 18.0 (18.0) | ? | ? | ? | ? |
integrity attribute | No support | 45.0 | No support | ? | ? | ? | ? | 45.0 |
referrerpolicy attribute | ? | ? | No support | 50.0 (50.0) | ? | ? | ? |
[1] WebKit bug 148363 tracks WebKit implementation of Subresource Integrity (which includes the integrity
attribute).
© 2005–2017 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