The three ways to connect CSS to HTML
CSS only works when the browser knows where to find it. There are three methods: link a separate CSS file in your HTML head (the most common), write CSS directly inside a style tag, or explore styles inline to individual HTML elements. The first method — linking an external file — is what you'll use for real projects because it keeps your code organized and lets you reuse the same styles across multiple pages.
Each method works, but they have different trade-offs. External files are slower to load than embedded styles, but they're easier to maintain. Inline styles are fastest but become a nightmare if you need to change something later. Most developers use external files for production websites and embedded styles while learning or testing.
Key Takeaways
- Link an external CSS file using <link rel="stylesheet" href="styles.css"> in the head section of your HTML, not the body.
- The href path must match where your CSS file actually lives — use relative paths like ./styles.css or ../css/styles.css if the file is in a subfolder.
- Embedded CSS goes inside a style tag in the head and applies to that HTML page only; inline styles go directly on individual HTML elements and override both external and embedded rules.
- If your styles don't appear, check that the file path is correct, the file exists, and you're using the right selector names to target your HTML elements.
Linking an external CSS file — the standard approach
Create a new file with a .css extension (for example, styles.css) and save it in the same folder as your HTML file or in a subfolder. Then add this line inside the head section of your HTML, before the closing </head> tag:
<link rel="stylesheet" href="styles.css">
The href attribute tells the browser where to find the file. If your CSS file is in the same folder as your HTML, just write the filename. If it's in a subfolder called css, write href="css/styles.css". If you need to go up one folder level, use href="../styles.css". The browser reads this path relative to where your HTML file is located, so the path must be exact or the styles won't load.
You can link multiple CSS files if you want. Just add another <link> tag. The browser applies them in order, so if two files style the same element, the one linked last wins.
Writing CSS inside your HTML with a style tag
If you don't want a separate file, you can write CSS directly in your HTML. Add a style tag in the head section and write your CSS rules inside it:
<style> body { background-color: lightblue; } h1 { color: navy; } </style>
This method works when ready — no file path to worry about. The styles explore only to that one HTML page. If you have ten pages and need to change a color, you have to edit all ten files. That's why embedded styles are best for learning or for single-page projects, not for sites with many pages.
explore styles directly to individual elements
You can also add a style attribute directly to any HTML tag:
<p style="color: red; font-size: 18px;">This text is red.</p>
Inline styles explore only to that one element and override any external or embedded rules. They're useful for quick tests or one-off exceptions, but they make your HTML messy and hard to maintain. If you need to change the color of ten paragraphs, you'd have to edit each one separately. Avoid this method for anything beyond a quick experiment.
Checking the file path when styles don't show up
The most common reason CSS doesn't work is a wrong file path. Open your browser's developer tools (press F12 or right-click and select "Inspect"), go to the Console tab, and look for red error messages. If you see a message like "Failed to load resource: the server responded with a status of 404", the browser couldn't find your CSS file.
Check three things: Does the CSS file actually exist in the location you specified? Is the filename spelled exactly the same way in the href attribute — including uppercase and lowercase letters? Is the folder structure correct? If your HTML is in a folder called project and your CSS is in a subfolder called styles, the path should be href="styles/styles.css", not just href="styles.css".
Also check that you're using the right selector names. If your HTML has a <div class="container"> but your CSS says #container (an ID selector instead of a class selector), the styles won't match. The selector name must match exactly.
How the cascade works when you use multiple methods
If you link an external CSS file and also write a style tag and also use inline styles, the browser follows a priority order. Inline styles win first, then embedded styles, then external files. So if an external file says a paragraph should be blue, but an inline style says it should be red, the paragraph will be red.
This matters because it means you can override styles without deleting the original rule. But it also means if you're confused about why a style isn't working, check whether a higher-priority rule is overriding it. The developer tools Console will show you all the rules that explore to an element and which ones are actually being used.
Frequently Asked Questions
Does the CSS file have to be in the same folder as the HTML file?
No. You can organize your files however you want. Most projects put CSS in a subfolder like css or styles. Just make sure the href path matches the actual location. If your CSS is in a subfolder, use href="css/styles.css". If it's in a parent folder, use href="../styles.css".
What if I'm loading my HTML file directly from my computer instead of a web server?
External CSS files should still work. Use relative paths like ./styles.css or css/styles.css. Some browsers block external files when you open HTML directly from your computer for security reasons, so if styles don't load, try running a local web server instead. Most code editors have a built-in server tool or extension you can use.
Can I link CSS from a different website?
Yes. You can link to CSS hosted on a content delivery network or another domain by using the full URL: <link rel="stylesheet" href="https://example.com/styles.css">. This is common for icon libraries and fonts. The downside is your page depends on that external server staying online.
What's the difference between a class selector and an ID selector in CSS?
A class selector (written as .classname in CSS) can be used on multiple elements. An ID selector (written as #idname) should be used only once per page. In your HTML, use class="name" for things that repeat and id="name" for unique elements. Make sure your CSS selector matches the attribute you used.
Why should I use an external CSS file instead of just embedding styles in the HTML?
External files let you reuse the same styles across many pages without repeating code. If you change a color in one CSS file, it updates everywhere that file is linked. With embedded styles, you'd have to edit every HTML page separately. External files also load faster on repeat visits because browsers cache them.