liveBook · Manning

Link element

As explained in Web Design Playground

An HTML element that defines a link to an external resource and can also connect a document to a stylesheet or other relationship via attributes.

A link element is a signpost in a web page’s head section: it tells the browser where to find an external style sheet. Put the CSS in its own .css file, then use href for its location and rel="stylesheet" to identify the relationship. section 7.7

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

This compact link element attaches styles.css when that CSS file sits in the same directory as the web page. section 7.7

The href value changes with the CSS file’s location:

  • Use just the filename for a file alongside the page: styles.css. section 7.7
  • Start with a subdirectory name for a nested CSS file: css/styles.css. section 7.7
  • Start with / for a CSS directory from the website’s main directory: /css/styles.css. section 7.7
  • Use a full URL when the stylesheet lives on a remote server. section 7.7

External style sheets keep style rules separate from the web page. The same stylesheet syntax used inside a style element works in the .css file, without the style element itself. In HTML5, type="text/css" is not needed on the link element, though it was required in HTML 4.01. section 7.7

Linking a hosted font

A font service can generate a link element for the head section. After it loads the font stylesheet, CSS can select the font with font-family. Add only the font variants you need: linking more fonts can slow page loading. section 18.6

<link href="https://fonts.googleapis.com/css?family=Lato:400,400i,700" rel="stylesheet">

This generated link element requests Lato in regular, italic, and bold variants from Google Fonts. section 18.6

Try it: create a styles.css file, place a link element in the page head, and give one element a style rule. If the file is elsewhere, adjust href like giving the browser a more detailed address. section 7.7

How Web Design Playground covers it

How deep it goes
Explains same-directory, subdirectory, root-relative, and remote href paths, plus the HTML4.01-to-HTML5 type attribute change.
Examples
Uses styles.css paths, a remote Google Fonts URL, and Lato regular, italic, and bold variants.
What you can do after
Apply it via a font host’s generated tag or by writing a stylesheet reference in the head section.
How it connects
Connects external style sheets, CSS style rules, head sections, web fonts, and font-family.

Also explained in

Compare how 3 books explain link element →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Design Playground. The section links point to the parts of the book it draws on.