Lesson 28 of 36
HTML Tutorial

Linking CSS, Favicons and Other Resources

Connect stylesheets, icons and preloaded resources to a page with the link element.

3 min readBeginnerWeb FundamentalsHead Elements

The <link> element connects the current page to an external resource. It is a void element, and the rel attribute (relationship) says what the resource is for.

Stylesheets#

Your HTML describes structure; CSS describes presentation. Keep them in separate files and connect them with:

html
<link rel="stylesheet" href="css/style.css">
  • rel="stylesheet" says this file contains styles.
  • href is the path to the file, relative or absolute.

You can link several stylesheets. They apply in order, so later rules override earlier ones when they conflict. Place <link> elements in the head so the page is styled as it first appears, rather than flashing unstyled content.

Media-specific styles#

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

The media attribute applies a stylesheet only when the condition matches, such as printing or a dark colour scheme.

Favicons#

A favicon is the tiny icon on the browser tab and in bookmarks, and it makes your site recognisable.

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

An SVG icon scales cleanly, an .ico file covers older browsers, and apple-touch-icon is used when someone adds your site to an iOS home screen.

Web app manifest#

html
<link rel="manifest" href="/site.webmanifest">

A manifest is a small JSON file describing the name, colours and icons of an installable web app.

Other relationships#

rel valuePurpose
canonicalTells search engines the preferred URL of this page
preloadStarts fetching a critical file early
preconnectOpens a connection early to another domain
alternatePoints to another version, such as a language or feed
html
<link rel="canonical" href="https://example.com/menu">
<link rel="preconnect" href="https://fonts.example.net">
<link rel="preload" href="fonts/brew.woff2" as="font" type="font/woff2" crossorigin>

Use preload sparingly, only for a few resources that matter immediately, because preloading everything slows everything.

Both use href, but the difference is important. An <a> is a visible link the user clicks, found in the body. A <link> is an invisible relationship declared in the head.

Common mistakes#

  • Wrong relative path to the stylesheet, such as forgetting the folder name.
  • Putting <link rel="stylesheet"> in the body.
  • Misspelling rel="stylesheet".
  • Forgetting to commit the favicon so the tab shows a generic icon.
  • Preloading too many files.

Try it yourself#

Create css/style.css with a rule colouring your headings. Link it from your page. Then add a favicon (any small image will do), reload with the cache disabled, and check the tab. Finally add a print stylesheet that hides the navigation and test it using the browser's print preview.

Recap#

link connects resources to a page. Use rel="stylesheet" for CSS, rel="icon" for the favicon, and canonical, preconnect and preload for search and performance. Declare these in the head with correct paths.

Check your understanding

Lesson quiz

Optional

A short quiz to check what you just learned.

Questions
4
Time limit
5 min
Pass mark
70%
Best score
-