Sub topic 9
Linking Stylesheets, Icons, and Canonical URLs
This chapter assumes you know basic HTML syntax, the structure of a document (doctype, html, head, body), and what a browser and a search engine do with a page. Here you will learn how the link element connects a page to related resources from inside the head.
What the link element does
The link element is a void element, so it has no closing tag and no content. It describes a relationship between the current document and another resource. Two attributes carry most of the meaning:
relnames the relationship, such asstylesheet,icon, orcanonical.hrefgives the URL of the related resource.
Other attributes refine the link. type and media limit when a resource applies, sizes describes icon dimensions, and crossorigin controls how cross-origin requests are made.
Do not confuse link with the a element. An a element creates a clickable link in the body. A link element in the head is read by the browser or by crawlers and is never shown to the reader.
Linking a stylesheet
The most common use is loading CSS:
<link rel="stylesheet" href="styles.css">The browser treats a stylesheet in the head as render-blocking. It waits for the CSS before painting the page, which prevents a flash of unstyled content. Keep the file small, and put the link in the head, not at the end of the body.
The media attribute makes a stylesheet apply only when a query matches:
<link rel="stylesheet" href="base.css">
<link rel="stylesheet" href="print.css" media="print">Both files download, but the browser does not let print.css block rendering on screen. Several link elements apply in order, and later rules win when selectors have equal specificity.
Linking icons
Browsers show a small icon in tabs, bookmarks, and history. Declare it with rel="icon":
<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 to any size. An ICO or PNG file works as a fallback for older browsers. The apple-touch-icon value is used when someone adds the page to an iOS home screen, and it should be a 180 by 180 pixel PNG.
Browsers also request /favicon.ico from the site root on their own if no icon is declared. Declaring the icon explicitly avoids a wasted request and a 404 in your server logs.
Declaring the canonical URL
One page is often reachable at several addresses, for example with tracking parameters or with and without www. Search engines may treat these as duplicates. The canonical link names the address you prefer:
<link rel="canonical" href="https://example.com/shoes">Follow these rules:
- Use an absolute URL, including the scheme and host.
- Point to a page that returns a normal 200 response, not a redirect or an error.
- Use one canonical link per page.
- Make the canonical page point to itself.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Relative `href` in a canonical link | Crawlers may resolve it to the wrong address | Use a full absolute URL |
| Missing `rel` | The browser ignores the element | Always set `rel` |
| Stylesheet link placed in the body | Late styles can cause a flash and re-layout | Move it to the head |
| Wrong path in `href` | The file returns 404 and the page is unstyled | Check the Network tab in developer tools |
| Several conflicting canonical links | Search engines may ignore all of them | Keep exactly one |
Putting it together
This head combines the pieces from this chapter. The charset and viewport tags come first, as the earlier chapters recommend.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Trail Running Shoes - Example Store</title>
<link rel="canonical" href="https://example.com/shoes">
<link rel="icon" href="/icon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Trail Running Shoes</h1>
</body>
</html>Exercise
A product page is available at https://example.com/shoes and also at https://example.com/shoes?utm_source=newsletter. Write the link element that tells search engines which address to prefer, and state whether it guarantees that outcome.
Show answer
Add <link rel="canonical" href="https://example.com/shoes"> to the head of both versions. It does not guarantee the outcome. Search engines treat it as a hint and may choose a different address if other signals disagree.
Exercise
You add <link rel="stylesheet" href="print.css" media="print"> to a page. Does the browser block on-screen rendering while it waits for this file, and when are its rules applied?
Show answer
No. The media query does not match on screen, so the file does not block rendering. Its rules apply only when the page is printed or shown in print preview.