How to Link a CSS File to an HTML Page
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
<link rel="stylesheet" href="<path>">
| Part | What 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:
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:

<!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 screenshot comes from Chromium 159, the open-source browser that Chrome is built on.
What Happened
- The browser loaded
style.csswhen it read thelinkline, and applied both rules. - The heading is teal: Chromium reported its color as
rgb(0, 131, 143), which is#00838f, the value instyle.css. - The whole page uses a sans-serif font, from the
bodyrule. Without the stylesheet, the browser falls back to its default look, black text in a serif font, as the next section shows.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
A wrong name or folder in href, such as styles.css for a file named style.css | The 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_FOUND | Check 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 all | Always write rel="stylesheet" |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The page has no styles | The path in href is wrong, or rel="stylesheet" is missing | Fix the path and add rel="stylesheet" |
net::ERR_FILE_NOT_FOUND in the console | The CSS file isn't where href points | Move the file, or change the path |
| A change to the CSS file doesn't show | The page was loaded before you saved the change | Save 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.
How do I link a CSS file in another folder?
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.
What's the difference between link and the style element?
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
- link: The External Resource Link element, on MDN.
- HTML attribute: rel, on MDN.
