HTML Reference ยท Document and Metadata
The <link> element
Reference for the HTML <link> element: rel values for style sheets, icons, preload and preconnect, every attribute explained, examples and technical details.
The <link> element declares a relationship between the current document and another resource. The rel attribute names the relationship and href points to the resource. Some relationships make the browser fetch something (a style sheet, an icon, a preloaded font); others are just information (the canonical URL, an alternate language version).
<link rel="stylesheet" href="/css/site.css"> <link rel="icon" href="/favicon.svg" type="image/svg+xml"> <link rel="canonical" href="https://example.com/recipes/lentil-soup">
<link> is a void element: it has no content and no end tag. It normally lives in the <head>.
Try it
The example uses data: URLs so it runs in the sandbox; on a real site href points to a .css file. The second sheet has media="print", so it only applies when the page is printed.
Common rel values
| Value | Meaning |
|---|---|
stylesheet |
Load and apply a CSS style sheet. Add alternate and a title to offer an alternative sheet. |
icon |
The page's icon (favicon). apple-touch-icon is a widely used non-standard variant. |
manifest |
A web app manifest (JSON) for installable apps. |
canonical |
The preferred URL for this content, used by search engines to merge duplicates. |
alternate |
Another version of the page: a translation (with hreflang), a feed (with type), and so on. |
preload |
Fetch a resource the page will definitely need soon. Requires as. |
modulepreload |
Fetch and prepare a JavaScript module early. |
prefetch |
Fetch a resource that a future navigation will probably need, at low priority. |
preconnect |
Open a connection to another origin early. |
dns-prefetch |
Look up another origin's domain name early. |
author, license, help, search, next, prev |
Informational relationships that point to related documents. |
Attributes
| Attribute | Values | Description |
|---|---|---|
href |
A URL | The linked resource. Required, unless imagesrcset is used with rel="preload". |
rel |
Space-separated keywords | The relationship(s); see the table above. Required (unless itemprop is used). |
as |
style, script, font, image, fetch, audio, video, track, document and others |
The kind of resource for preload and modulepreload, which sets priority and request headers. |
type |
A MIME type | The type of the linked resource, letting the browser skip types it can't use. |
media |
A media query | Apply or load the resource only when the query matches, for example (prefers-color-scheme: dark). |
sizes |
any or sizes like 32x32 64x64 |
The icon sizes available, used with rel="icon". |
hreflang |
A language tag | The language of the linked resource. |
crossorigin |
anonymous, use-credentials |
Fetch the resource with CORS. Fonts are always fetched this way, so font preloads need it. |
integrity |
A hash, e.g. sha384-โฆ |
Subresource integrity: the browser refuses the file if its hash doesn't match. |
referrerpolicy |
A referrer policy, e.g. no-referrer |
How much referrer information to send with the request. |
fetchpriority |
high, low, auto |
A hint about the relative priority of the request. |
blocking |
render |
Block rendering until this resource has loaded. |
imagesrcset, imagesizes |
Same syntax as srcset and sizes on <img> |
Preload a responsive image. |
disabled |
Boolean | Turns off a style sheet link. |
title |
Text | For style sheets, names a style sheet set; for other links, a description. |
It also accepts the global attributes.
Usage notes
- Use
link, not@import. Separate<link rel="stylesheet">elements download in parallel; an@importinside CSS is found only after the first file arrives. See Adding CSS to HTML. - Style sheets block rendering. A style sheet in the head stops the page from painting until it has loaded, which avoids a flash of unstyled content but costs time. Keep the critical CSS small. Fast-Loading HTML covers the trade-offs.
- Preload sparingly.
preloadjumps the queue, so preloading too much slows down what matters. Use it for late-discovered critical files such as a hero font, and always matchas(andcrossoriginfor fonts) to how the file will be used, or it will be fetched twice. More in Loading Resources Efficiently. - Icons. One SVG icon plus a PNG fallback covers most needs. The Favicons and App Icons lesson walks through the set.
- Links in the body.
<link>may appear in the body only for "body-ok" relationships:stylesheet,preload,modulepreload,prefetch,preconnect,dns-prefetchandpingback. - Not a hyperlink.
<link>never creates anything clickable. For links people follow, use<a>.
Technical summary
| Content categories | Metadata content. If it is allowed in the body: also flow content and phrasing content. |
| Permitted content | Nothing; it is a void element |
| Tag omission | Must have a start tag and no end tag |
| Permitted parents | Any element that accepts metadata content; a <noscript> inside the head; any element that accepts phrasing content, if the link is body-ok |
| Implicit ARIA role | No corresponding role |
| DOM interface | HTMLLinkElement |
| Default display | none |
Browser support
The element and the core rel values are supported in every browser. Newer keywords and attributes, such as modulepreload, fetchpriority and blocking, arrived at different times, so check support before relying on them.
Related
<style>โ CSS written inside the document<script>โ scripts, which can also be preloaded<meta>โ other document metadata<a>โ clickable hyperlinks- Lessons: Adding CSS to HTML, Favicons and App Icons, Loading Resources Efficiently, The head Element