HOW-TO

How to link a CSS file to HTML (and why styles go missing when you share it)

The one line that connects a stylesheet to a page, how to write the path so it actually resolves, the usual reasons styles do not show up, and what to do before you share the page.

4 min read·

A CSS file holds the styles; the HTML file has to point at it. You do that with a <link> tag inside the <head> of the page. The browser reads the tag, fetches the stylesheet and applies it before it draws the page.

Add the link tag

Put this line inside <head>, after the <meta charset> tag:

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

rel="stylesheet" tells the browser what the file is. href is the path to it. The tag has no closing tag. Save both files, then reload the page in the browser.

Write the path from where the HTML file sits

The href is relative to the HTML file, not to the folder you happen to have open in your editor.

  • ✓Same folder: href="styles.css".
  • ✓In a subfolder: href="css/styles.css".
  • ✓One folder up: href="../styles.css".
  • ✓Avoid a leading slash such as href="/styles.css" for files on your own computer. Opened from file://, the slash points at the root of your disk, so the stylesheet is not found.
  • ✓A stylesheet on another site, such as a font or framework CDN, uses its full https:// address.

When the styles do not show up

  • ✓Open the browser console (F12, or Cmd+Option+J in Chrome on a Mac). A red "Failed to load resource" line names the path the browser tried; compare it with where the file really is.
  • ✓Check the spelling and case. Styles.css and styles.css are different files on most web servers, even if your laptop treats them the same.
  • ✓Check the extension. Windows may hide a second extension, leaving you with styles.css.txt.
  • ✓Confirm rel="stylesheet" is there. Without it the browser downloads the file and ignores it.
  • ✓Hard-reload with Ctrl+Shift+R (Cmd+Shift+R on a Mac). The browser may still be using an older copy of the stylesheet.
  • ✓If one rule does not apply but others do, the file is linked. Look for a typo in the selector, or a later rule overriding it.

The other two ways to add CSS

A linked file is best when several pages share the same styles. A <style> block is best when the page has to travel on its own.

  • ✓A <style> block inside <head>: the CSS lives in the HTML file itself. Good for one page, or any page you plan to send to someone.
  • ✓A style attribute on one element, such as <p style="color: red">. Fine for a one-off; hard to maintain across a page.

Before you share the page

The <link> tag points at a second file. Send only the .html file, by email, chat or upload, and the other person gets the page without its styles: plain black text in the default font.

For a single page, copy the contents of styles.css into a <style> block in the <head> and delete the <link> line. Now the one file carries everything. Images can be embedded the same way; see making an HTML file self-contained.

Then share it as a link. With FileShare Pro you upload the .html file from the dashboard, copy the link and send it; it opens as a styled page in any browser and the person opening it needs no account. For a few pages that link to each other, a mini-site publishes them under one link, with shared styles copied into each page. Sign up free to share your first page.

Frequently asked

How do I link a CSS file to HTML?

Add <link rel="stylesheet" href="styles.css"> inside the <head> of the HTML file, with href set to the path of the CSS file relative to the HTML file.

Why is my CSS not linking to my HTML?

Usually the path is wrong, the file name or case does not match, rel="stylesheet" is missing, or the browser is showing a cached copy. The browser console shows the path it tried to load.

Where does the link tag go?

Inside <head>. Browsers also accept it in the body, but placing it in the head lets the styles load before the page is drawn.

Why does my page lose its styling when I send it to someone?

The styles are in a separate .css file that was not sent. Paste the CSS into a <style> block in the HTML file, or host the page and send the link.

Got a page to share?

Drop the file, copy the link — live in seconds, free plan included.

Start sharing free