<link>

Connects the document to an external resource: a stylesheet, a page icon, a font, or another language version.

Display
none
Closing tag
None, void element
Permitted content
None, an empty element
On this page

Example

#
HTML
<head>
  <meta charset="utf-8">
  <title>HTML and CSS Course from the Basics | CodeWorlds</title>
  <link rel="stylesheet" href="/css/style.css">
  <link rel="icon" href="/favicon.ico" sizes="32x32">
  <link rel="icon" href="/icon.svg" type="image/svg+xml">
  <link rel="canonical" href="https://www.codeworlds.com/html-css-course">
</head>

Definition and usage

#

The <link> element describes the relationship between the current page and another resource. You give the kind of relationship in the rel attribute and the address in href. Most often you use it to attach a CSS file and the page icon (favicon), but <link> can also speed up loading of resources or point a search engine to the right page address.

It is an empty element with no closing tag, and it usually sits in <head>. It is neither visible nor clickable. That’s the difference from <a>: a link is for the user, while <link> is information for the browser and robots.

A stylesheet attached in the head holds up rendering of the page until it has been downloaded. It sounds scary, but it’s deliberate: that way you don’t briefly see the page without styles. So it’s worth keeping CSS files light.

Syntax

#
Syntax
<link rel="relationship" href="address">

Attributes

#
  • rel

    list of keywords

    The kind of relationship with the resource, and at the same time information about what the browser should do with it. Without rel the element does nothing. The most important values are described in the following rows of this table.
  • href

    URL

    The address of the resource, relative (/css/style.css) or full (https://…). Without it the element points to nothing.
  • rel="stylesheet"

    CSS stylesheet

    Attaches an external CSS file. It’s the most popular way of styling pages: one file serves all subpages, and after the first download the browser keeps it in its cache. You don’t need to add the type="text/css" attribute.
  • media

    media query

    Applies the stylesheet only when the condition is met, for example when printing (print) or in dark mode. The browser still downloads a file whose condition isn’t met, but with lower priority and without holding up rendering of the page.
  • rel="icon"

    page icon (favicon)

    The icon visible on the browser tab and in bookmarks. The sizes attribute gives the dimensions (e.g. 32x32), and type the file format. A popular set is an .ico file for older programs, an SVG icon that scales without losing quality, apple-touch-icon (180 × 180 px) for the iOS home screen, and an app manifest with icons for Android.
  • rel="preload"

    early fetch of a resource, together with `as`

    Tells the browser to fetch a resource right away, before it would discover it on its own, for example a font buried deep in a CSS file or the page’s main image. The as attribute (font, image, style, script, fetch) is required and says what the resource is. Fonts also need the crossorigin attribute, even when they come from your own server.
  • rel="preconnect"

    early connection to another server

    Opens a connection to a server you’re about to fetch resources from, e.g. a font service. The browser takes care of the address lookup, connection, and encryption ahead of time, so the file itself arrives faster. A lighter version, dns-prefetch, only resolves the server’s IP address.
  • rel="canonical"

    the page’s main address

    Tells search engines the primary address of a page when the same content is available at several addresses, for example with ?utm_source=… parameters or with and without a trailing slash. Give the full address. A search engine treats it as a strong hint, not an absolute command.
  • rel="alternate"

    another version of the page, e.g. with `hreflang`

    Points to an alternative version of the same content: in another language (with the hreflang attribute) or in another format, e.g. an RSS feed (with the type attribute). The value hreflang="x-default" marks the default version for people whose language you don’t handle separately.
  • crossorigin

    anonymous | use-credentials

    Fetches the resource in CORS mode. anonymous (or the attribute alone, with no value) doesn’t send cookies to other domains, while use-credentials does. It’s needed for fonts, for integrity on files from other domains, and for preconnect to servers you fetch fonts from.
  • integrity

    cryptographic hash, e.g. `sha384-…`

    A safeguard for files from external servers, e.g. from a CDN (Subresource Integrity). You give the file’s hash, and after downloading the browser checks that it matches. If someone swaps the file on the server, the browser rejects it. For files from another domain, also add crossorigin="anonymous".

The element also supports all global attributes, such as class, id, style and hidden.

Attributes in practice

#

rel="stylesheet"

CSS stylesheet

Attaches an external CSS file. It’s the most popular way of styling pages: one file serves all subpages, and after the first download the browser keeps it in its cache. You don’t need to add the type="text/css" attribute.

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

media

media query

Applies the stylesheet only when the condition is met, for example when printing (print) or in dark mode. The browser still downloads a file whose condition isn’t met, but with lower priority and without holding up rendering of the page.

HTML
<link rel="stylesheet" href="/css/style.css">
<link rel="stylesheet" href="/css/print.css" media="print">
<link rel="stylesheet" href="/css/dark.css" media="(prefers-color-scheme: dark)">

rel="icon"

page icon (favicon)

The icon visible on the browser tab and in bookmarks. The sizes attribute gives the dimensions (e.g. 32x32), and type the file format. A popular set is an .ico file for older programs, an SVG icon that scales without losing quality, apple-touch-icon (180 × 180 px) for the iOS home screen, and an app manifest with icons for Android.

HTML
<link rel="icon" href="/favicon.ico" sizes="32x32">
<link rel="icon" href="/icon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<link rel="manifest" href="/manifest.webmanifest">

rel="preload"

early fetch of a resource, together with `as`

Tells the browser to fetch a resource right away, before it would discover it on its own, for example a font buried deep in a CSS file or the page’s main image. The as attribute (font, image, style, script, fetch) is required and says what the resource is. Fonts also need the crossorigin attribute, even when they come from your own server.

HTML
<link rel="preload" href="/fonts/inter.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="/img/hero.webp" as="image">

rel="preconnect"

early connection to another server

Opens a connection to a server you’re about to fetch resources from, e.g. a font service. The browser takes care of the address lookup, connection, and encryption ahead of time, so the file itself arrives faster. A lighter version, dns-prefetch, only resolves the server’s IP address.

HTML
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>

rel="canonical"

the page’s main address

Tells search engines the primary address of a page when the same content is available at several addresses, for example with ?utm_source=… parameters or with and without a trailing slash. Give the full address. A search engine treats it as a strong hint, not an absolute command.

HTML
<link rel="canonical" href="https://www.codeworlds.com/html-css-course">

rel="alternate"

another version of the page, e.g. with `hreflang`

Points to an alternative version of the same content: in another language (with the hreflang attribute) or in another format, e.g. an RSS feed (with the type attribute). The value hreflang="x-default" marks the default version for people whose language you don’t handle separately.

HTML
<link rel="alternate" hreflang="pl" href="https://www.codeworlds.com/kurs-html">
<link rel="alternate" hreflang="en" href="https://www.codeworlds.com/en/html-course">
<link rel="alternate" hreflang="x-default" href="https://www.codeworlds.com/en/html-course">
<link rel="alternate" type="application/rss+xml" title="CodeWorlds Blog" href="/blog/rss.xml">

integrity

cryptographic hash, e.g. `sha384-…`

A safeguard for files from external servers, e.g. from a CDN (Subresource Integrity). You give the file’s hash, and after downloading the browser checks that it matches. If someone swaps the file on the server, the browser rejects it. For files from another domain, also add crossorigin="anonymous".

HTML
<link
  rel="stylesheet"
  href="https://cdn.example.com/reset.min.css"
  integrity="sha384-WDMDqPkpoQaPl6uhHNxkwhv3yB5Ae46EbjyRWmKdfWPSXPigfz2MdYGomGZE9FBE"
  crossorigin="anonymous"
>

Default CSS

#

Most browsers display this element with these default styles:

CSS
link {
  display: none;
}

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Attach CSS files with <link> in the head, not with @import in another CSS file. @import downloads stylesheets one after another and slows the page start.
  • Don’t overdo preload. Fetching many files early takes bandwidth away from what is truly urgent, and the browser prints a console warning for an unused resource.
  • <link> doesn’t create a clickable link. For that, use the <a> element.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.