Sub topic 9
Linking stylesheets, icons, and canonical URLs
This chapter assumes you can write basic HTML elements and attributes. It also assumes you know the document skeleton (doctype, html, head, body) and have seen meta and title in the head. Here you will learn the link element, which connects the page to other resources. You will use it for three jobs: loading CSS, declaring icons, and naming the canonical URL.
What the link element does
The link element describes a relationship between the current document and another resource. It is a void element, so it has no closing tag and no content. Two attributes matter most:
relnames the relationship, such asstylesheet,icon, orcanonical.hrefgives the URL of the other resource.
Other attributes refine the link. media limits when a stylesheet applies. sizes and type describe icons. crossorigin controls how cross-origin requests are made. hreflang marks the language of an alternate version.
The rel value decides what the browser does. Some values make the browser download and use the resource. Others only record information that other software reads later.
| rel value | Who uses it | Effect |
|---|---|---|
| `stylesheet` | Browser | Downloads CSS and applies it to the page |
| `icon` | Browser | Supplies the tab, bookmark, or history icon |
| `apple-touch-icon` | iOS and some other platforms | Supplies the home-screen icon |
| `canonical` | Search engines | Names the preferred URL for this content |
| `preconnect` | Browser | Opens a connection early to another origin |
Because link is a head element, the browser reads it while parsing the head, before it paints anything. That timing is what makes stylesheets and icons work well, and it is why canonical URLs must live there too.
Linking stylesheets
Linking an external stylesheet takes one line:
<link rel="stylesheet" href="styles.css">The type="text/css" attribute was once required. It is now the default and you can leave it out.
Stylesheets block rendering
The browser treats a stylesheet in the head as render-blocking. It will not paint the page until the CSS has loaded and been parsed. This prevents a flash of unstyled content, where raw HTML appears and then jumps into its final layout. The cost is that a slow stylesheet delays the first paint.
Two habits keep the cost low:
- Keep the critical CSS small and served from a fast, cacheable URL.
- Put the stylesheet
linkin the head, not at the end of the body. A stylesheet far down the page forces the browser to restyle content it has already laid out.
Order and the cascade
When you link several stylesheets, the browser applies them in document order. If two rules have equal specificity, the one from the later stylesheet wins. Put general styles first and overrides later:
<link rel="stylesheet" href="/css/base.css">
<link rel="stylesheet" href="/css/components.css">
<link rel="stylesheet" href="/css/theme.css">Conditional stylesheets with media
The media attribute takes a media query. The browser always downloads the file, but it only blocks rendering on it when the query matches. A stylesheet whose query does not match is fetched at a low priority and does not delay the first paint.
<link rel="stylesheet" href="/css/base.css">
<link rel="stylesheet" href="/css/print.css" media="print">
<link rel="stylesheet" href="/css/wide.css" media="(min-width: 60rem)">This is a cheap way to keep print rules out of the critical path.
Linking icons
The icon is the small image shown in browser tabs, bookmarks, and history lists. Operating systems also use larger versions when a user adds your site to a home screen.
The default and why to declare it anyway
If you declare no icon, many browsers request /favicon.ico from the site root on their own. That works, but it gives you no control over format or size. Declaring icons explicitly is better.
A practical icon set
Most sites need only a few files:
<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="/manifest.webmanifest">Each line has a purpose:
- The
.icofile is the fallback for older browsers and tools.sizes="32x32"stops some browsers from picking it over the SVG. - The SVG icon scales to any size and can respond to dark mode through CSS inside the file. The
typeattribute lets a browser skip formats it cannot read. - The Apple touch icon is a 180 by 180 pixel PNG. Provide it without transparency, because iOS fills transparent areas with black.
- The web app manifest is a JSON file that lists larger icons, such as 192 and 512 pixel versions, for installable apps.
When several rel="icon" links are present, the browser chooses the best match using type and sizes. That is why those attributes matter. Without them, the browser may download a file it cannot use.
Declaring a canonical URL
The same content is often reachable at several URLs. A product page might load at all of these:
https://example.com/shoeshttps://example.com/shoes?utm_source=newsletterhttps://example.com/shoes/http://example.com/shoes
Search engines may treat these as duplicates. They then have to guess which one to show and may split ranking signals among them. A canonical link tells them which URL you prefer:
<link rel="canonical" href="https://example.com/shoes">Rules for canonical links
- Use an absolute URL, with scheme and host. A relative URL can be resolved incorrectly, especially when the page is served from several hosts.
- Use one canonical link per page. If a page declares two that disagree, search engines may ignore both.
- Place it in the head. A canonical link in the body is not valid and is ignored.
- A self-referencing canonical, where the page points to its own preferred URL, is normal and good practice. It protects against tracking parameters creating accidental duplicates.
- Point to a page that actually works. A canonical that leads to a redirect, an error, or a
noindexpage sends contradictory signals.
A hint, not a command
A canonical link is a strong hint, but the search engine can disagree. It may choose another URL if other signals, such as redirects, internal links, or sitemaps, point elsewhere. This is the same kind of limit you saw with the robots meta tag. You are making a request, not issuing a rule. For a permanent move of content, use an HTTP redirect, which browsers must follow.
Putting it together
This complete document uses all three jobs. It can run as shown if you place matching files at the paths named.
<!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>
<meta name="description" content="Compare lightweight trail running shoes by grip, cushioning, and weight.">
<link rel="canonical" href="https://example.com/shoes">
<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="stylesheet" href="/css/base.css">
<link rel="stylesheet" href="/css/print.css" media="print">
</head>
<body>
<h1>Trail Running Shoes</h1>
</body>
</html>The encoding and viewport come first, then the title and description. The canonical and icons follow, and the stylesheets come last so the metadata is not stuck behind other requests in the source. The next chapter covers ordering in detail.
Common errors
| Mistake | What happens | Fix |
|---|---|---|
| `<link rel="stylesheet" href="styles.css"></link>` | The extra closing tag is invalid, though browsers tolerate it | Write the void element once, without a closing tag |
| Stylesheet link placed at the end of the body | Content is painted unstyled, then restyled | Move it into the head |
| Relative canonical such as `href="/shoes"` | Search engines may resolve it wrongly | Use the full `https://` URL |
| Two canonical links with different URLs | Both may be ignored | Keep exactly one |
| Canonical pointing to a `noindex` page | Conflicting signals about indexing | Make the target indexable or remove the canonical |
| Apple touch icon with transparency | iOS shows a black background | Use an opaque image |
| Misspelled `rel` such as `rel="stylesheets"` | The browser ignores the link silently | Check the value against the spec |
Checking your work
Open the browser developer tools and look at the Elements panel to confirm each link sits inside the head. Then check the Network panel. A stylesheet that returns a 404 status shows in red, and the page will look unstyled. An icon request that returns 404 is easy to miss, so filter by "Img" and look for your icon files. To check the canonical, view the page source, not only the Elements panel, because scripts can change the live DOM and some crawlers read only the original HTML.
Exercise
A page is available at https://example.com/shoes and also at https://example.com/shoes?color=blue, and both show the same content. Write the link element for the head that tells search engines which one you prefer, and name one rule it must follow.
Show answer
<link rel="canonical" href="https://example.com/shoes"> It must use an absolute URL. Other valid rules: it goes in the head, only one per page, and the target must be indexable. It is a hint, not a command.
Exercise
You link print.css with media="print". Does the browser download the file on a normal screen, and does it delay the first paint?
Show answer
The browser downloads it, at a low priority, but it does not block rendering because the media query does not match on screen. It applies the rules only when printing.
Exercise
A site declares only <link rel="icon" href="/icon.svg"> and some users on older browsers see no icon. What would you add, and what attribute keeps the browser from choosing the wrong file?
Show answer
Add a fallback, such as <link rel="icon" href="/favicon.ico" sizes="32x32">. The type and sizes attributes let the browser compare candidates and pick a file it can use.