Course contentsShow
← Web Development
Lesson 86 of 2,9822. HTML FundamentalsFree lesson

Linking External Stylesheets

Using <link> to connect CSS files with proper attributes and media queries.

Linking External Stylesheets

What you'll learn: How to connect a separate CSS file to your HTML document using the <link> element.

The Core Idea

Just as you learned with Meta Description for SEO, the <head> section of your HTML document contains important information about your page. One crucial piece is telling the browser where to find your stylesheet—the separate CSS file that controls how your page looks.

Think of it like giving your house (HTML) an instruction manual that points to a paint and furniture catalog (CSS file) stored elsewhere. The <link> element is that instruction.

The <link> Element

The <link> element creates a relationship between your HTML document and an external resource. For stylesheets, it looks like this:

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

Required attributes:

  • rel="stylesheet" — tells the browser this link is to a stylesheet (defines the relationship)
  • href="styles.css" — the path to your CSS file (like the src in images)

Media Queries Attribute

You can specify when a stylesheet should apply using the media attribute:

<link rel="stylesheet" href="print.css" media="print">
<link rel="stylesheet" href="mobile.css" media="screen and (max-width: 600px)">

The first loads only when printing; the second loads only on smaller screens. Without a media attribute, the stylesheet applies to all media types (the default is media="all").

Placement in the Document

The <link> element belongs in the <head>, typically after your <meta> tags and <title>:

<head>
    <meta charset="UTF-8">
    <title>My Page</title>
    <link rel="stylesheet" href="styles.css">
</head>

Key Takeaway: The <link> element with rel="stylesheet" and href attributes connects external CSS files to your HTML, keeping your presentation layer separate from your document structure—a fundamental principle of web development.