Skip to main content

How to Link a CSS File to an HTML Page

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

The line <link rel="stylesheet" href="style.css"> in a page's head loads the CSS file style.css and applies it to the page. Many pages can link the same file, so one change to it restyles all of them.

Syntax​

Syntax
<link rel="stylesheet" href="<path>">
PartWhat it does
<link>Connects the page to another file. It has no closing tag.
rel="stylesheet"Says the file is a stylesheet, so the browser loads it and applies its rules.
href="<path>"Where the CSS file is, relative to the HTML file: style.css is in the same folder, css/style.css in a folder named css next to the page.

The line goes inside <head>. MDN's link page lists all its attributes.

The Example​

The example is a coffee menu page with a heading and a paragraph. Its look comes from a separate file, style.css, which gives the page a sans-serif font and the heading a teal color.

Step 1: Write the CSS File​

Create a folder for the example. In it, create a file named style.css:

style.css
body {
font-family: sans-serif;
}

h1 {
color: #00838f;
}

The body rule sets the font for the whole page, and the h1 rule colors the heading.

Step 2: Write the HTML Page​

In the same folder, create a file named index.html:

index.html, an HTML page whose head holds a link element with rel stylesheet and href style.css, and whose body has the heading Coffee menu and the paragraph Espresso, latte and cappuccino.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Coffee menu</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Coffee menu</h1>
<p>Espresso, latte and cappuccino.</p>
</body>
</html>

The link line in head loads style.css from the same folder.

Step 3: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: the heading Coffee menu in teal, and the paragraph Espresso, latte and cappuccino., both in a sans-serif font.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The browser loaded style.css when it read the link line, and applied both rules.
  • The heading is teal: Chromium reported its color as rgb(0, 131, 143), which is #00838f, the value in style.css.
  • The whole page uses a sans-serif font, from the body rule. Without the stylesheet, the browser falls back to its default look, black text in a serif font, as the next section shows.

Common Mistakes​

MistakeWhat happensDo this instead
A wrong name or folder in href, such as styles.css for a file named style.cssThe page shows without its styles: black text in the browser's default serif font. The browser's console showed Failed to load resource: net::ERR_FILE_NOT_FOUNDCheck the file name and the folder in href
Leaving out rel="stylesheet"Nothing is applied: without rel, the browser doesn't treat the file as CSS, and the page had no stylesheet at allAlways write rel="stylesheet"

Troubleshooting​

SymptomLikely causeFix
The page has no stylesThe path in href is wrong, or rel="stylesheet" is missingFix the path and add rel="stylesheet"
net::ERR_FILE_NOT_FOUND in the consoleThe CSS file isn't where href pointsMove the file, or change the path
A change to the CSS file doesn't showThe page was loaded before you saved the changeSave the CSS file and reload the page

FAQ​

Can a page use more than one CSS file?​

Yes. Add one link line for each file.

Write the path from the HTML file to the CSS file: href="css/style.css" looks in a folder named css next to the page, and href="../style.css" looks in the folder above it.

link loads CSS from a separate file, which many pages can share. The style element holds CSS inside the page itself, so only that page gets it.

Conclusion​

A link element with rel="stylesheet" and the right href is all it takes to style a page from a separate CSS file. If the styles don't show, check the path first, then the rel.

Additional Resources​