What is the <link> tag?
<link> is a void element, usually in <head>, that connects the document to external resources. Its rel attribute says what the relationship is: stylesheets (rel="stylesheet"), icons (rel="icon"), resource hints (preload, preconnect, prefetch, modulepreload), the canonical URL, alternate languages, the web app manifest and more.
Common uses
- Stylesheets:
<link rel="stylesheet" href="/styles.css">, optionally withmedia="print"or a media query. - Icons:
<link rel="icon" href="/favicon.svg" type="image/svg+xml">,<link rel="apple-touch-icon" href="/apple-touch-icon.png">. - Resource hints:
preconnect: open a connection early to a critical origin (fonts, CDN).dns-prefetch: resolve DNS only.preload: fetch a resource needed soon on this page (as="font",as="image", withcrossoriginfor fonts).modulepreload: preload JavaScript modules.prefetch: low-priority fetch for a likely next navigation.
- SEO:
<link rel="canonical" href="...">,<link rel="alternate" hreflang="fr" href="...">. - PWA:
<link rel="manifest" href="/manifest.webmanifest">. - Feeds:
<link rel="alternate" type="application/rss+xml" href="/feed.xml">.
Useful attributes
href, rel, as, type, media, crossorigin, integrity (Subresource Integrity), fetchpriority, blocking="render", imagesrcset/imagesizes (for preloading responsive images).
<link> vs <a>
<link> describes relationships for the browser and is not clickable; <a> creates a hyperlink for users.
Performance tips
Preload only what is needed early; too many preloads compete with each other. Stylesheets are render-blocking by default, which avoids a flash of unstyled content.
Code example
1<!doctype html>2<html lang="en">3<head>4 <meta charset="utf-8">5 <title>Link examples</title>67 <!-- Stylesheets (one only for print) -->8 <link rel="stylesheet" href="/styles.css">9 <link rel="stylesheet" href="/print.css" media="print">1011 <!-- Icons and app manifest -->12 <link rel="icon" href="/favicon.svg" type="image/svg+xml">13 <link rel="apple-touch-icon" href="/apple-touch-icon.png">14 <link rel="manifest" href="/manifest.webmanifest">1516 <!-- Resource hints -->17 <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>18 <link rel="preload" href="/fonts/inter-var.woff2" as="font" type="font/woff2" crossorigin>19 <link rel="preload" as="image" href="/hero.avif" fetchpriority="high">20 <link rel="modulepreload" href="/js/app.js">2122 <!-- SEO -->23 <link rel="canonical" href="https://example.com/guides/link-tag">24 <link rel="alternate" hreflang="fr" href="https://example.com/fr/guides/link-tag">25</head>26<body><h1>The link element</h1></body>27</html>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.