The link tag
You need two files in the same folder: the page (index.html) and the stylesheet
(styles.css). The <link> line in the head connects them. rel="stylesheet"
tells the browser what kind of file it is, and href says where to find it.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My first styled page</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Hello, CSS</h1>
<p class="intro">This page gets its look from styles.css.</p>
</body>
</html> body {
font-family: Georgia, serif;
background: #f7fee7;
color: #1f2937;
}
h1 {
color: #4d7c0f;
}
.intro {
font-size: 20px;
}
Here’s what that page looks like. The live boxes on this page can’t open a second file, so this
preview has the same rules copied into a <style> block. On your computer, with two real files,
you get the same result.
To try it for real, make a folder, save both files in it, and open index.html in your browser. If you
haven’t made an HTML file before, how to create an HTML file walks you
through it on Windows and Mac. Change a color in styles.css, save, reload the page, and you’ll see it
change.
-
<link>has no closing tag.<link rel="stylesheet" href="styles.css" />with a slash at the end works the same. -
You don’t need
type="text/css"any more. You’ll see it in older code, and it does no harm, but stylesheet is already the default. -
Keep the link in the
<head>. Browsers will still load a stylesheet linked further down, but the page can show up unstyled for a moment before it kicks in.
What is CSS in HTML?
CSS stands for Cascading Style Sheets. HTML says what things are: this is a heading, this is a paragraph, this is a link. CSS says how they look: dark green, 20px, a rounded button. A browser can show HTML without any CSS, it just uses its plain default look. The two boxes below hold the same HTML. Only the second one has CSS.
One small cheat: the preview boxes on this page start with a plain sans-serif font. A browser on its own uses a serif font like Times New Roman, so “HTML only” looks even plainer in a real file.
A CSS rule has a selector (which elements) and declarations (what to change):
h2 { color: #4d7c0f; } makes every h2 green. The “cascading” part is how the
browser settles arguments between rules, which matters a lot when your CSS seems to be ignored. More on that
further down. If HTML itself is new to you, start with what HTML is.
Three ways to add CSS to HTML
There are three places CSS can live: in its own file (external), in a <style> block on the page
(internal), or in a style attribute on one element (inline). They use the same CSS, only the spot
changes.
1. An external stylesheet with <link>
This is the one from the quick answer, and what nearly every real website uses. All the rules sit in a
.css file, and every page that should look the same links to it:
<head>
<meta charset="utf-8">
<title>About us</title>
<link rel="stylesheet" href="styles.css">
</head>
Change a color in styles.css and it changes on every page at once. The browser also keeps a copy of the
file after the first visit, so the next page someone opens doesn’t download it again.
2. Internal CSS in a <style> block
Put your rules between <style> and </style> inside the head. They only apply
to that one page. In the examples on this page the style block sits at the top of the code, because the box adds
the rest of the page for you. In your own file, put it in the <head>.
3. Inline CSS with style=""
Any element can take a style attribute with declarations in it, no selector and no braces. It only
affects that element. Fine for a quick test, messy for anything bigger, and it can’t do hover effects or
different sizes for phones.
Which way should you use?
| Way | Good for | Watch out for |
|---|---|---|
| External file | Any site with more than one page. One file to edit, and browsers cache it. | The path in href has to be right, or nothing loads. |
| <style> block | A single page, a quick experiment, a demo you send to someone as one file. | Copying the same block into ten pages means ten places to fix later. |
| style="" | A one-off tweak, HTML emails (mail apps differ in what they keep, so inline is the safe bet). | No hover, no media queries, and it’s hard to override from a stylesheet. |
You can mix them, and most pages do. When two rules give the same element different values, the browser follows a
few rules of its own: a style attribute beats rules in stylesheets (unless a rule says !important), a class
beats a plain tag name, and when two rules are equally specific, the one that comes later wins.
Try deleting the second p rule: the first paragraph turns blue. “Later” counts across the whole page,
so a <style> block placed after your <link> tag beats the linked file, and one
placed before it loses.
File paths for href
href is a path from the HTML file to the CSS file. Most “it doesn’t load” problems come down to this
path. Take a small site laid out like this:
my-site/
├── index.html
├── styles.css
├── css/
│ └── theme.css
└── blog/
└── first-post.html | Where the CSS file is | href |
|---|---|
| Same folder as the HTML file (index.html → styles.css) | styles.css |
| In a folder next to it (index.html → css/theme.css) | css/theme.css |
| One folder up (blog/first-post.html → styles.css) | ../styles.css |
| Up, then into another folder (blog/first-post.html → css/theme.css) | ../css/theme.css |
| From the top of the site, on a web server | /css/theme.css |
| On another site, like a CDN | https://example.com/styles.css |
-
..means “the folder above”. You can stack it:../../styles.cssgoes up two folders../styles.cssis the same asstyles.css. -
A path starting with
/starts at the top of the website. That’s great on a server, but if you double-click the HTML file on your computer,/means the top of your whole drive, so the file isn’t found. Use relative paths (no leading slash) while you work locally. -
Watch the capital letters.
Styles.cssandstyles.cssare the same file on most Windows and Mac computers, but different files on most web servers. A page that looks fine on your laptop can lose its CSS once it’s online. - Skip spaces in file names.
my-styles.cssis safer thanmy styles.css.
My CSS isn’t working: a checklist
Start by opening the browser’s developer tools: press F12 (or Ctrl+Shift+I) on Windows and Linux, or Cmd+Option+I
on a Mac. In Safari you need to switch on the developer features in Settings > Advanced first. Then reload the
page and look at the Console. A red error that mentions styles.css (a 404, or “file not found”) means
the browser couldn’t find the file. No error at all means either the file loaded and something inside it is the
problem, or the link tag has a typo and the browser skipped it without a word.
- The path is wrong. Check the folder and the spelling against the table above. Being one folder off
catches a lot of people: a page in
blog/needs../styles.css, notstyles.css. - A typo in the link tag. It has to be
rel="stylesheet"(one word, no s at the end) andhref, notsrc. And<style src="styles.css">doesn’t load anything, a style tag only holds CSS you write between its tags. - The file is really called styles.css.txt. Some editors add
.txt, and Windows hides file extensions by default, so you won’t see it. On Windows 11, open File Explorer and turn on View > Show > File name extensions. On a Mac, turn on Finder > Settings > Advanced > Show all filename extensions. - The browser is showing an old copy. Browsers cache CSS files, so your change may not show after a
normal reload. Do a hard refresh: Ctrl+F5 or Ctrl+Shift+R on Windows and Linux, Cmd+Shift+R on a Mac in Chrome,
Edge or Firefox, and Option+Cmd+R in Safari. On a live site you can also add a version to the link,
href="styles.css?v=2", and change the number each time you update the file. - There’s a <style> tag inside the .css file. A CSS file holds only rules. If you copied a
style block over with its tags, delete
<style>and</style>from the file. This is what the wrong file looks like:
The browser reads the tag as part of the first selector and throws that whole rule away, so it looks like the top of your file is ignored.<style> h1 { color: green; } </style> - Another rule wins. A later rule, a more specific selector or a
styleattribute can beat yours (see which rule wins). Right-click the element, choose Inspect, and look at the styles panel: rules that lost are shown crossed out, with the winner above them. - A small mistake in the CSS. A missing
;drops that line and the one after it. A missing}swallows the next rule. Here neither line works, becausegreenhas no;:
Paste the file into the CSS Validator and it points at the line.h1 { color: green font-size: 32px; } - The server sends the wrong thing. Online, if the console says the stylesheet was refused because of its MIME type, the server usually sent back an error page instead of your CSS. In other words, it’s the path again.
More than one stylesheet
Add one <link> per file. They load in parallel, and their rules count in the order the links
appear, so a rule in the second file beats an equally specific rule in the first. Put general files first (a reset,
your base styles) and the ones that change things last.
<head>
<meta charset="utf-8">
<title>Shop</title>
<link rel="stylesheet" href="css/base.css">
<link rel="stylesheet" href="css/theme.css">
<link rel="stylesheet" href="css/print.css" media="print">
</head> The live box can’t load files, so here the two files are played by two style blocks, in the same order as the links. Swap the blocks around and the button goes gray.
media="print" on the third link means that file is only used when someone prints the page. You can use
any media query there, like media="(max-width: 600px)", though most people keep everything in one file
and put the media queries inside it.
What about @import?
A CSS file can pull in another one with @import. It goes at the very top, before any normal rules:
/* The first line of styles.css */
@import url("buttons.css");
body {
font-family: system-ui, sans-serif;
}
It works, but it’s slower: the browser only finds out about buttons.css after it has downloaded
styles.css, so the files load one after the other instead of side by side. Two
<link> tags in the head do the same job faster.
A CSS framework from a CDN
A CSS framework is a big ready-made stylesheet with classes for buttons, grids and forms. Many of them are hosted on a CDN (a network of servers that hands out files quickly), so you link to them with a full URL instead of downloading anything. Here’s Bootstrap 5.3.8, one well-known example:
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB"
crossorigin="anonymous">
<link rel="stylesheet" href="styles.css"> - Link the framework first and your own file after it, so your rules can override the framework’s.
-
integrityis a fingerprint of the file. If the file on the CDN ever changes, the browser refuses to use it. Copy the whole tag from the framework’s own site rather than typing it, because one wrong character in that fingerprint and the file won’t load at all. -
Pick a fixed version number in the URL (like
5.3.8), so an update can’t change your site overnight. The framework’s site lists the newest one. - Your visitors’ browsers download the file from that other server, so the page needs an internet connection to look right, even when you open it from your own computer.
Need a blank page with all of this set up? The HTML Starter Template builds the
head for you, stylesheet links included. Scripts work in a similar way, with a <script> tag
instead of <link>: see how to link JavaScript to
HTML.
Questions people ask
How do I link a CSS file to HTML?
Save your CSS in a file that ends in .css, for example styles.css, and add <link rel="stylesheet" href="styles.css"> inside the <head> of your HTML page. If the CSS file is in another folder, change the href to match, like href="css/styles.css". Save both files and reload the page.
Where does the link tag go in HTML?
Inside the <head>, usually after <meta charset> and <title>. Browsers will also load a stylesheet linked from the body, but the page can show up unstyled for a moment first, so the head is the right place. The link tag has no closing tag.
What is CSS in HTML?
CSS (Cascading Style Sheets) is the language that controls how HTML looks: colors, fonts, sizes, spacing and layout. HTML says what the content is, for example a heading or a list, and CSS says how it should look. You can add it to a page with a linked .css file, a <style> block, or a style attribute on one element.
Why is my CSS file not working?
Most of the time the path in href is wrong, so the browser can’t find the file. Open the developer tools (F12 on Windows, Cmd+Option+I on a Mac) and look for a red error that mentions the CSS file. Other common causes: rel="stylesheets" or src instead of href, a file that is really called styles.css.txt, an old copy in the cache (try Ctrl+F5 or Cmd+Shift+R), <style> tags inside the .css file, or another rule that wins because it comes later or is more specific.
What is the difference between inline, internal and external CSS?
External CSS lives in its own .css file and is linked with <link>, so many pages can share it. Internal CSS sits in a <style> block in one page’s head and only styles that page. Inline CSS is a style attribute on a single element, like <p style="color: red;">. They can be mixed: a style attribute beats rules from stylesheets (unless they use !important), and between two equally specific rules, the later one wins.
Can I link more than one CSS file?
Yes. Add one <link rel="stylesheet"> per file in the head. The order matters: when two rules are equally specific, the one in the file linked later wins. So link general files first (a reset or a framework) and your own changes last.
Do I need type="text/css" in the link tag?
No. In HTML5, stylesheets are CSS by default, so <link rel="stylesheet" href="styles.css"> is enough. type="text/css" still works if it’s already in your code, it just isn’t needed.
Should I use @import or link?
Use <link> tags in the head. @import inside a CSS file works, but the browser only discovers the imported file after it has downloaded the first one, so the files load one after another and the page appears later. Several link tags load side by side.
How do I link a CSS file that is in another folder?
Write the path from the HTML file to the CSS file. A file in a css folder next to the page is href="css/styles.css". If the page is in a subfolder and the CSS is one level up, use ../ for each folder you go up: href="../styles.css". A path starting with / starts at the top of the website, which works on a server but not when you open the file straight from your computer.
Keep going
Tools that help
- HTML Editor HTML, CSS and JavaScript tabs side by side, with a live preview.
- CSS Validator Finds a missing ; or } and shows you the line.
- CSS Formatter Tidies a squashed stylesheet so you can read it.
- HTML Starter Template A blank page with the head, charset and stylesheet link filled in.
More tutorials
- How to Center Text, a Div or an Image in HTML & CSS
- How to Add a Line Break in HTML
- How to Add Spaces in HTML
- How to Change Text Color in HTML
- How to Change Background Color in HTML
- How to Add an Image in HTML
- What Is a URL? Meaning, Parts and Examples
- How to Redirect a Page in HTML
- How to Get an Image URL
- Canonical URL
- What Is HTML? What It Stands For and How It Works
- How to Create, Open and Run an HTML File
- How to See the HTML Code of a Website
- How to Link JavaScript to HTML with the script Tag
- HTML hr Tag
- HTML Comments
- How to Make Text Bold in HTML
- HTML Font and Font Size
- All tutorials