Skip to main content

HTML Tags

HTML <link> Tag

Written by Updated HTML <link> Tag exercises

The link tag connects the page to another file, most often a stylesheet: <link rel="stylesheet" href="styles.css">. The same tag also adds the favicon, preloads fonts and tells search engines the page's canonical address. The rel attribute decides which.

It lives in the <head> and has no closing tag.

Syntax

html

<link rel="stylesheet" href="styles.css">

Attributes

AttributeWhat it does
relThe relationship: stylesheet, icon, preload, canonical and more. Required.
hrefThe address of the linked file.
typeThe file type. Not needed for CSS.
mediaOnly use this file when a media query matches, like media="print".
asWith rel="preload": what kind of file, like font or image.
crossoriginNeeded for preloading fonts, even from your own site.

Example

html

<!DOCTYPE html>
<html>
<head>
  <title>Link Tag Example</title>
  <link rel="stylesheet" href="/samples/example.css">
</head>
<body>
  <h1>This heading is styled with an external CSS file.</h1>
</body>
</html>

The heading is styled by the CSS in the linked file, not by anything in the page itself.

The most common rel values

Typical link tags in a head

html

<link rel="stylesheet" href="/css/site.css">
<link rel="icon" href="/favicon.ico">
<link rel="canonical" href="https://example.com/html-link-tag/">
<link rel="preload" href="/fonts/inter.woff2" as="font" type="font/woff2" crossorigin>

Stylesheet loads CSS. Icon sets the tab icon. Canonical tells search engines the main address of a page that can be reached at several. Preload starts downloading an important file early.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Good to know

  • A stylesheet that is not found fails silently; the page just looks unstyled. Check the Network panel in DevTools for a 404.
  • Several stylesheets apply in the order they are linked; later ones win when selectors are equal.
  • Do not confuse <link> with <a>: <link> connects files to the page, <a> creates links people click. How CSS gets into a page is covered in How to Add CSS.

HTML <link> Tag exercises

Link a stylesheet

Write the line that connects the stylesheet https://html5andcss3.org/samples/styles.css to a page.

Result

Check your answer to see it here.