Skip to content

<link> - The External Resource Link Element

Metadata HTML 2.0

The <link> element specifies relationships between the current document and external resources. Most commonly used to link stylesheets, but also for defining favicons, preloading resources, and establishing document relationships.

Result
<link rel="relationship" href="url">

The <link> element is a void element (self-closing) that must be placed within the <head> section. The rel attribute defines the relationship type, and href specifies the resource location.

AttributeRequiredDescriptionExample Values
relYesRelationship between current document and linked resourcestylesheet, icon, preload, prefetch, canonical
hrefConditionalURL of the linked resourceURL path or absolute URL
typeNoMIME type of the linked resourcetext/css, image/png, application/json
mediaNoMedia query for when resource should be loadedscreen, print, (max-width: 768px)
AttributeDescriptionValues
asType of content being preloaded (required with rel="preload")style, script, image, font, fetch
crossoriginHow to handle CORS requestsanonymous, use-credentials
integritySubresource Integrity hash for securityBase64-encoded hash value
referrerpolicyReferrer policy for fetchingno-referrer, origin, strict-origin
AttributeDescriptionUse Case
sizesIcon sizes (for rel="icon" or rel="apple-touch-icon")16x16, 32x32, any
hreflangLanguage of linked resourceen, es, fr
disabledDisables stylesheet (non-standard, use with caution)Boolean attribute
imagesizesImage sizes for responsive preloadingSame as <img> sizes
imagesrcsetImage sources for responsive preloadingSame as <img> srcset

The most common use of <link> is to include external CSS:

Result

Load different stylesheets for different media types or conditions:

<!-- Default stylesheet -->
<link rel="stylesheet" href="styles.css">
<!-- Print-specific styles -->
<link rel="stylesheet" href="print.css" media="print">
<!-- Mobile-specific styles -->
<link rel="stylesheet" href="mobile.css" media="screen and (max-width: 768px)">
<!-- High-resolution displays -->
<link rel="stylesheet" href="retina.css" media="screen and (min-resolution: 2dppx)">
<!-- Dark mode preference -->
<link rel="stylesheet" href="dark.css" media="(prefers-color-scheme: dark)">

Define various icon sizes for different platforms and use cases:

Result
<!-- Standard favicon -->
<link rel="icon" type="image/x-icon" href="/favicon.ico">
<!-- SVG favicon (modern, scalable) -->
<link rel="icon" type="image/svg+xml" href="/favicon.svg">
<!-- PNG favicons for different sizes -->
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="192x192" href="/favicon-192x192.png">
<!-- Apple touch icons -->
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<!-- Android Chrome -->
<link rel="manifest" href="/site.webmanifest">

Preload resources that will be needed soon to improve performance:

<!-- Preload critical CSS -->
<link rel="preload" href="/styles/critical.css" as="style">
<link rel="stylesheet" href="/styles/critical.css">
<!-- Preload fonts (with crossorigin!) -->
<link rel="preload" href="/fonts/main.woff2" as="font" type="font/woff2" crossorigin>
<!-- Preload hero image -->
<link rel="preload" href="/images/hero.jpg" as="image">
<!-- Preload critical JavaScript -->
<link rel="preload" href="/js/app.js" as="script">
<!-- Preload fetch/XHR data -->
<link rel="preload" href="/api/data.json" as="fetch" crossorigin>

Hint to the browser about resources that might be needed on future page navigation:

<!-- Prefetch next page -->
<link rel="prefetch" href="/page2.html">
<!-- Prefetch images for next page -->
<link rel="prefetch" href="/images/page2-hero.jpg" as="image">
<!-- DNS prefetch for external domains -->
<link rel="dns-prefetch" href="https://cdn.example.com">
<link rel="dns-prefetch" href="https://analytics.example.com">
<!-- Preconnect to external origin (DNS + TCP + TLS) -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<!-- Prerender entire page (use sparingly!) -->
<link rel="prerender" href="/next-page.html">

Specify the preferred URL for duplicate content to help with SEO:

<!-- Original content URL -->
<link rel="canonical" href="https://example.com/original-article">
<!-- Use on paginated content -->
<link rel="canonical" href="https://example.com/article">
<!-- Use on AMP or mobile versions -->
<link rel="canonical" href="https://example.com/article">

Link to alternate versions of the current page:

<!-- RSS/Atom feeds -->
<link rel="alternate" type="application/rss+xml" title="Blog RSS Feed" href="/feed.xml">
<link rel="alternate" type="application/atom+xml" title="Blog Atom Feed" href="/atom.xml">
<!-- Alternate language versions -->
<link rel="alternate" href="https://example.com/en/page" hreflang="en">
<link rel="alternate" href="https://example.com/es/page" hreflang="es">
<link rel="alternate" href="https://example.com/fr/page" hreflang="fr">
<link rel="alternate" href="https://example.com/page" hreflang="x-default">
<!-- AMP version -->
<link rel="amphtml" href="https://example.com/amp/article">
<!-- Mobile version -->
<link rel="alternate" media="only screen and (max-width: 640px)" href="https://m.example.com/page">

Link to a web app manifest for Progressive Web Apps:

Result

Typical manifest file (manifest.json):

{
"name": "My Application",
"short_name": "App",
"description": "A progressive web application",
"start_url": "/",
"display": "standalone",
"theme_color": "#4285f4",
"background_color": "#ffffff",
"icons": [
{
"src": "/icons/icon-192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "/icons/icon-512.png",
"sizes": "512x512",
"type": "image/png"
}
]
}

Preload ES modules for better performance:

<!-- Preload main module -->
<link rel="modulepreload" href="/js/app.js">
<!-- Preload module dependencies -->
<link rel="modulepreload" href="/js/utils.js">
<link rel="modulepreload" href="/js/components.js">

Understanding the different resource hints and when to use them:

Use when: You know you’ll need resources from a domain but not exactly which ones yet.

What it does: Resolves DNS early, saving 20-120ms when the actual resource is requested.

Best for: Third-party domains like analytics, ads, or CDNs.

<link rel="dns-prefetch" href="https://analytics.example.com">

Optimize CSS delivery for faster page rendering:

<head>
<!-- Inline critical CSS for above-the-fold content -->
<style>
/* Critical styles here */
body { margin: 0; font-family: system-ui; }
.header { background: #333; color: white; }
</style>
<!-- Preload full stylesheet -->
<link rel="preload" href="/styles/main.css" as="style">
<!-- Load full stylesheet asynchronously -->
<link rel="stylesheet" href="/styles/main.css" media="print" onload="this.media='all'">
<!-- Fallback for no-JS -->
<noscript>
<link rel="stylesheet" href="/styles/main.css">
</noscript>
</head>

Properly load web fonts to avoid layout shifts and flash of invisible text:

<head>
<!-- Preconnect to font provider -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<!-- Preload critical fonts -->
<link rel="preload"
href="/fonts/main-regular.woff2"
as="font"
type="font/woff2"
crossorigin>
<!-- Load font stylesheet -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap">
</head>

Ensure third-party resources haven’t been tampered with:

<!-- CDN stylesheet with integrity check -->
<link rel="stylesheet"
href="https://cdn.example.com/styles.css"
integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/ux9AAYV2R7aDK4mBH9G8sQ5o3N3d3d3"
crossorigin="anonymous">
<!-- CDN script preload with integrity -->
<link rel="preload"
href="https://cdn.example.com/app.js"
as="script"
integrity="sha384-xyz123..."
crossorigin="anonymous">

Generate integrity hashes using tools like SRI Hash Generator.

Preload responsive images efficiently:

<!-- Preload responsive image -->
<link rel="preload"
as="image"
href="hero-small.jpg"
imagesrcset="hero-small.jpg 400w,
hero-medium.jpg 800w,
hero-large.jpg 1200w"
imagesizes="(max-width: 600px) 400px,
(max-width: 1200px) 800px,
1200px">

When linking alternate versions, provide descriptive titles:

<!-- Good: Descriptive title -->
<link rel="alternate"
type="application/rss+xml"
title="Tech Blog - Latest Articles RSS Feed"
href="/feed.xml">
<!-- Avoid: Generic title -->
<link rel="alternate"
type="application/rss+xml"
title="RSS"
href="/feed.xml">

Use theme color to match browser UI to your site:

<!-- Standard theme color -->
<meta name="theme-color" content="#4285f4">
<!-- Theme color for dark mode -->
<meta name="theme-color" content="#1a1a1a" media="(prefers-color-scheme: dark)">

Provide printer-friendly styles for better accessibility:

<link rel="stylesheet" href="print.css" media="print">
print.css
@media print {
/* Hide navigation, ads, etc. */
nav, aside, .no-print { display: none; }
/* Use appropriate colors for printing */
body { color: black; background: white; }
/* Add page breaks */
h1, h2 { page-break-after: avoid; }
/* Show link URLs */
a::after { content: " (" attr(href) ")"; }
}

Help users and search engines find content in their language:

<!-- Self-referencing is recommended -->
<link rel="alternate" href="https://example.com/en/page" hreflang="en">
<link rel="alternate" href="https://example.com/es/page" hreflang="es">
<link rel="alternate" href="https://example.com/page" hreflang="x-default">
<!-- Also provide language switcher in page content -->
<nav aria-label="Language selection">
<a href="/en/page" hreflang="en">English</a>
<a href="/es/page" hreflang="es">Español</a>
</nav>
<!-- Fonts ALWAYS need crossorigin, even same-origin -->
<link rel="preload"
href="/fonts/main.woff2"
as="font"
type="font/woff2"
crossorigin>
<!-- Browser knows how to prioritize -->
<link rel="preload" href="/app.js" as="script">
<link rel="preload" href="/hero.jpg" as="image">

Too many preloads can hurt performance rather than help:

<!-- BAD: Preloading everything -->
<link rel="preload" href="/style1.css" as="style">
<link rel="preload" href="/style2.css" as="style">
<link rel="preload" href="/style3.css" as="style">
<link rel="preload" href="/img1.jpg" as="image">
<link rel="preload" href="/img2.jpg" as="image">
<!-- ... 20 more preloads ... -->
<!-- GOOD: Only preload critical resources -->
<link rel="preload" href="/critical.css" as="style">
<link rel="preload" href="/hero.jpg" as="image">
BrowserVersionNotes
Chrome1+Full support; rel="preload" since Chrome 50
Firefox1+Full support; rel="preload" since Firefox 56
Safari1+Full support; rel="preload" since Safari 11.1
Edge12+Full support; rel="preload" since Edge 79
IE3+Basic support; limited support for modern rel values
FeatureChromeFirefoxSafariEdge
rel="preload"50+56+11.1+79+
rel="prefetch"8+2+13+12+
rel="modulepreload"66+115+16.4+79+
rel="dns-prefetch"46+3+5+79+
rel="preconnect"46+39+11.1+79+
Module Preload66+115+16.4+79+
Subresource Integrity45+43+11.1+17+

Container for metadata elements. The <link> element must be placed inside <head>. Learn more →

Defines metadata that cannot be defined using other HTML meta elements. Often used alongside <link>. Learn more →