docs.rodeo

MDN Web Docs mirror

: The External Resource Link element

{{HTMLSidebar}} 

The <link> HTML element specifies relationships between the current document and an external resource. This element is most commonly used to link to {{Glossary("CSS", "stylesheets")}} , but is also used to establish site icons (both “favicon” style icons and icons for the home screen and apps on mobile devices) among other things.

{{EmbedInteractiveExample("pages/tabbed/link.html", "tabbed-shorter")}} 

To link an external stylesheet, you’d include a <link> element inside your {{HTMLElement("head")}}  like this:

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

This 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 one of the key features of the <link> element — the value denotes how the item being linked to is related to the containing document.

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"
  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 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 {{Glossary("CORS")}}  request.

Other usage notes:

Attributes

This element includes the global attributes.

Non-standard attributes

Obsolete attributes

Examples

Including a stylesheet

To include a stylesheet in a page, use the following syntax:

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

Providing alternative stylesheets

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" />

Providing icons for different usage contexts

You can include links to several 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.

<!-- iPad Pro with high-resolution Retina display: -->
<link
  rel="apple-touch-icon"
  sizes="167x167"
  href="/apple-touch-icon-167x167.png" />
<!-- 3x resolution iPhone: -->
<link
  rel="apple-touch-icon"
  sizes="180x180"
  href="/apple-touch-icon-180x180.png" />
<!-- non-Retina iPad, iPad mini, etc.: -->
<link
  rel="apple-touch-icon"
  sizes="152x152"
  href="/apple-touch-icon-152x152.png" />
<!-- 2x resolution iPhone and other devices: -->
<link rel="apple-touch-icon" href="/apple-touch-icon-120x120.png" />
<!-- basic favicon -->
<link rel="icon" href="/favicon.ico" />

For information about what sizes to choose for Apple icons, see Apple’s documentation on configuring web applications and the referenced Apple human interface guidelines. Usually, it is sufficient to provide a large image, such as 192x192, and let the browser scale it down as needed, but you may want to provide images with different levels of detail for different sizes, as the Apple design guideline recommends. Providing smaller icons for lower resolutions also saves bandwidth.

It may not be necessary to provide <link> elements at all. For example, browsers automatically request /favicon.ico from the root of a site, and Apple also automatically requests /apple-touch-icon-[size].png, /apple-touch-icon.png, etc. However, providing explicit links protects you against changes to these conventions.

Conditionally loading resources with media queries

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)" />

Stylesheet load events

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:

<link rel="stylesheet" href="mystylesheet.css" id="my-stylesheet" />

<script>
  const stylesheet = document.getElementById("my-stylesheet");

  stylesheet.onload = () => {
    // Do something interesting; the sheet has been loaded
  };

  stylesheet.onerror = () => {
    console.log("An error occurred loading the stylesheet!");
  };
</script>

[!NOTE] The 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.

Preload examples

You can find a number of <link rel="preload"> examples in Preloading content with rel="preload".

Blocking rendering till a resource is fetched

You can include render token inside a blocking attribute; the rendering of the page will be blocked till the resource is fetched. For example:

<link blocking="render" rel="stylesheet" href="example.css" crossorigin />

Technical summary

Content categories Metadata content. If itemprop is present: Flow content and phrasing content.
Permitted content None; it is a `{{Glossary("void element")}}` .
Tag omission Must have a start tag and must not have an end tag.
Permitted parents Any element that accepts metadata elements. If itemprop is present: any element that accepts phrasing content.
Implicit ARIA role link with href attribute
Permitted ARIA roles No role permitted
DOM interface `{{DOMxRef("HTMLLinkElement")}}` 

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN