How to Link CSS to HTML

To link a CSS file to an HTML page, put <link rel="stylesheet" href="styles.css"> inside the head. The href is a path from the HTML file to the CSS file, so a file in a css folder needs href="css/styles.css". You can also write CSS in a style element, in a style attribute, or pull files in with @import.

Four ways to add CSS to a page
MethodWhere it goesBest for
<link rel="stylesheet">In the headAlmost everything: one file shared by many pages
<style> elementIn the headSmall pages, critical CSS, demos
style="..." attributeOn one elementOne-off values set from code
@import url(...)Top of a CSS fileSplitting CSS into layers or files

Updated

Almost every case of a page with no styling comes down to the link element's path or attributes. This guide shows each way of adding CSS, includes a path checker that works out exactly which URL your href points at, and ends with a checklist for when the styles still do not appear.

Path checker

https://example.com/css/site.css

<link rel="stylesheet" href="../css/site.css">

The link element

A page with one linked stylesheet

The link sits in the head. Its href is relative to this HTML file, so styles.css must sit in a css folder next to it.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>My page</title>
  <link rel="stylesheet" href="css/styles.css">
  <link rel="stylesheet" href="css/print.css" media="print">
</head>
<body>
  <h1>Hello</h1>
</body>
</html>

The style element and inline styles

Embedded and inline CSS together

The style element colours every p. The style attribute on the second paragraph wins for that element only.

<style>
  p { color: #1d4ed8; font-weight: 600 }
</style>
<p>Styled by the style element.</p>
<p style="color: #b91c1c">Styled by its own style attribute.</p>

@import inside a stylesheet

Imports at the top of main.css

Imports come first. They can carry a media query, a supports() condition or a cascade layer.

@layer reset, base;
@import url("reset.css") layer(reset);
@import url("print.css") print;
@import url("grid.css") supports(display: grid);

body { font-family: system-ui, sans-serif; }

CSS not loading? Work down this list

Why styles do not apply, and how to check
SymptomLikely causeFix
404 for the CSS in the Network tabWrong path or folderUse the path checker above; try a root path on a server
No request for the CSS at allTypo in rel or link outside the HTMLWrite rel="stylesheet" exactly; put the link in head
Works locally, fails onlineFile name letter case differsServers are often case-sensitive: Styles.css is not styles.css
Old styles keep showingBrowser cacheHard reload, or add a version like styles.css?v=2
Some rules ignoredOverridden by later or more specific rulesInspect the element; check order and specificity
@import does nothingIt comes after other rulesMove every @import to the top of the file
Blocked in the consoleServed with the wrong content typeThe server must send text/css for .css files

How the browser resolves an href

The browser starts from the page's own URL, drops the file name, and applies the href to the folder that remains. ../ moves up one folder, ./ stays in the same one, and a leading slash restarts at the site root.

When you open a page by double-clicking it, the root is your disk, not your project. That is why /css/site.css works on a server but fails from a file:// address, while a relative path works in both.

Inline styles and style elements

A style element keeps CSS in the page, which saves a request and is handy for the small block of critical CSS that styles the first screen. It cannot be cached separately and is repeated on every page.

A style attribute applies to one element and beats rules from stylesheets unless they use !important. Use it for values your code calculates, not for regular design, which becomes impossible to change site-wide.

How it works

  1. Enter the address of your HTML page, as it appears in the browser bar.
  2. Enter the href you used in the link element.
  3. The checker shows the full URL the browser will request, worked out in your browser with the standard URL rules.
  4. Open that URL in a new tab. If it does not show your CSS, the path is wrong.

Tips

Frequently asked questions

Why is my CSS file not linking to my HTML?

Check the path first: open the browser's developer tools, look at the Network tab and find the CSS request. A 404 means the href points to the wrong place. Also check for rel="stylesheet" spelled correctly, file names in the same letter case, and that you saved the file.

What is the difference between a relative and an absolute path?

A relative path like css/site.css is resolved from the folder of the current page. A path starting with / is resolved from the site root, and a full URL starting with https:// points to an exact address. Root paths do not work when you open the file straight from disk.

Should I use @import or link?

Prefer link. Each @import is only discovered after the file that contains it has downloaded, so chained imports load one after another. @import is useful for layer() ordering or inside a build tool that bundles the files anyway.

How do I go up a folder in the href?

Use ../ for each level. A page at /blog/post.html with CSS at /css/site.css uses href="../css/site.css", or the root path href="/css/site.css" when the site runs on a server.

Where must @import go?

At the very top of the stylesheet, before any other rules. Only @charset and @layer statements may come before it. An @import placed after style rules is ignored.

Related: HTML boilerplate, CSS reset, CSS media queries, CSS specificity calculator, CSS minifier.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Guides

We.Inc is an AI-powered website builder you can resell under your own brand. Launch a branded client dashboard, bill on Stripe Connect, and deliver AI-generated websites in minutes. White-label plans start at $99 a month for 25 client sites, with a 7-day free trial and no per-site fees.

Product

Who It's For

Features

Resources

Company

View Sitemap