Link Elements, Stylesheets and Icons

Beginner · 18 min

How link rel connects a page to stylesheets, icons, a manifest and canonical URLs, why a stylesheet blocks rendering, and how to set up favicons.

Before thisMeta Tags and the Viewport

Colby Fayock · 18 min

Link Elements, Stylesheets and Icons

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Colby FayockReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `<link>` describes a relationship between this document and another resource, and `rel` names the relationship: `stylesheet`, `icon`, `manifest`, `canonical`, `preload` and many more. It is not clickable, unlike `<a>`.
  • A stylesheet link blocks rendering, and scripts after it wait for it. In a test, an inline script placed after a slow `<link rel="stylesheet">` already saw the styles applied.
  • A `media` attribute on the link restricts when it applies. In a test, a `media="print"` stylesheet was not applied on screen.
  • For favicons, an SVG icon plus a small `.ico` fallback covers modern and old browsers. An SVG can contain a `prefers-color-scheme` media query to follow dark mode.
  • Add an `apple-touch-icon` (180 by 180 PNG) for iOS home screens and a `manifest` link if the site is installable.

A page rarely stands alone. It uses a stylesheet, an icon, a web app manifest and sometimes a different URL for the same content. The <link> element, always in <head>, declares those connections. The rel attribute names what the relationship is, and href points at the other resource.

Contrast it with <a>: an anchor is for readers to follow. A <link> is for the browser and other programs, and shows nothing.

<link rel="stylesheet" href="/app.css">
<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="/site.webmanifest">
<link rel="canonical" href="https://example.com/guide">

Stylesheets block, on purpose

Without styles, a page flashes unstyled content, then restyles itself. To avoid that, browsers treat a stylesheet as render-blocking: nothing is painted until it arrives. Scripts that follow also wait, because a script may call getComputedStyle and expect real answers.

I tested the second part. A page had a <link rel="stylesheet"> for a CSS file that the server delayed by half a second, setting html { background: rgb(1, 2, 3) }. A script placed right after the link read the computed background, and in headless Chromium, Firefox and WebKit it already saw rgb(1, 2, 3). The script had waited. A second page used media="print" on the same link, and its script saw a transparent background, so the rules were not applied on screen.

Two things follow. A big stylesheet in the head delays everything, so keep critical CSS small and the file cacheable, which is covered in web performance and Core Web Vitals. And media is a useful lever: media="print" for print rules (see print stylesheets), or a media query for a large-screen-only file. I did not test whether a non-matching stylesheet still downloads; browsers typically fetch it at low priority.

Order matters for the cascade: later stylesheets override earlier ones on equal specificity, as the specificity and cascade lesson explains.

Favicons

The tab icon has the longest history of mess. Historically a browser asked for /favicon.ico whether or not you declared it. In a test, headless Firefox did request /favicon.ico from the server unprompted; I did not check the other browsers. Today the sensible set is small:

<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">

The SVG scales to every size and can contain CSS, including a prefers-color-scheme query, so the icon can switch between a dark and a light version. The .ico is the fallback for browsers and tools that do not take SVG. The apple-touch-icon is used when someone adds the site to an iOS home screen; 180 by 180 pixels is the usual size. The dark mode lesson explains prefers-color-scheme. I could not test how a browser draws an icon in a tab, only that the markup is the usual one.

Other common rel values

  • manifest: points to the web app manifest, for installable sites (see progressive web apps).
  • canonical: the preferred URL among duplicates.
  • alternate: another version of the page, such as an RSS feed or another language.
  • preload, prefetch, preconnect, modulepreload: hints for the loader, covered in resource hints.
  • stylesheet with title and alternate: an alternate style option that is off by default; rare.

<link> has no closing tag. For attributes like crossorigin, integrity and referrerpolicy, which also apply to stylesheets, see HTML security.

What the video covers

The embed is the whole of Colby Fayock’s “Favicons the Right Way” (August 2024), nearly eighteen minutes. Its description says it covers adaptive SVG favicons for light and dark mode that update on inactive tabs, SVG icons with .ico fallbacks where browsers do not support them, CSS media queries inside SVG files, and changing favicons dynamically when the tab is inactive. That covers only the icon half of this lesson. The stylesheet part is the lesson’s own, and the weakest part of this pick: nothing here teaches rel="stylesheet", and the runner-up on that is a 2:09 freeCodeCamp Concepts clip. I could not read a transcript, and whether the video builds on a framework is unknown to me. The blocking and media results above come from my test in headless Chromium, Firefox and WebKit.

Where this fits

This follows meta tags and the viewport. The next lesson, script loading, applies the same loading ideas to scripts.

Check yourself

  1. 01A script placed right after `<link rel="stylesheet" href="app.css">` reads `getComputedStyle`. Will it see the stylesheet's rules?

    Yes. The browser waits for a stylesheet before running scripts that follow it, because scripts may ask about styles. In a test, with the CSS delayed by half a second, the script still saw the background colour from the stylesheet. This is also why large stylesheets in the head delay first paint.

  2. 02What does `<link rel="stylesheet" media="print" href="print.css">` do on screen?

    The rules do not apply on screen. In a test, the page's background was still transparent with a print-only stylesheet. It loads and applies only for printing, which makes the link a clean way to keep print styles out of the screen cascade.

  3. 03Why list both an SVG icon and an `.ico` file?

    Modern browsers can use the SVG, which scales to any size and can adapt to dark mode through a media query inside the file. Older browsers and some tools only look for `.ico`, so the fallback keeps them working.

  4. 04How is `<link rel="canonical">` different from an `<a>` link?

    It sits in `<head>`, is invisible and is not followed by the user. It tells search engines which URL is the main version of a page. `<a>` is visible and navigates.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark