<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
Example
#<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
#<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. Withoutrelthe 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.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 thetype="text/css"attribute.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.page icon (favicon)
The icon visible on the browser tab and in bookmarks. Thesizesattribute gives the dimensions (e.g.32x32), andtypethe file format. A popular set is an.icofile 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.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. Theasattribute (font,image,style,script,fetch) is required and says what the resource is. Fonts also need thecrossoriginattribute, even when they come from your own server.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.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.another version of the page, e.g. with `hreflang`
Points to an alternative version of the same content: in another language (with thehreflangattribute) or in another format, e.g. an RSS feed (with thetypeattribute). The valuehreflang="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, whileuse-credentialsdoes. It’s needed for fonts, forintegrityon files from other domains, and forpreconnectto servers you fetch fonts from.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 addcrossorigin="anonymous".
The element also supports all global attributes, such as class, id, style and hidden.
Attributes in practice
#rel="stylesheet"
CSS stylesheetAttaches 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.
<link rel="stylesheet" href="/css/style.css">media
media queryApplies 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.
<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.
<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.
<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 serverOpens 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.
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>rel="canonical"
the page’s main addressTells 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.
<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.
<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".
<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:
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@importin another CSS file.@importdownloads 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
#- <head>The invisible part of the document holding metadata: the title, character encoding, description, styles, and scripts.
- <style>A CSS stylesheet written directly in the HTML document.
- <meta>Page metadata: character encoding, mobile viewport settings, a description for search engines, and a link preview on social networks.
- <a>A link to another page, a file, an email address, or a place on the same page.
- <script>Embeds JavaScript code in the page or loads it from a file to give the page behavior.
- <base>Sets the base address for all relative addresses in the document and the default target of links.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.