The three ways to connect CSS to HTML
CSS doesn't work on its own — it needs to be connected to your HTML file to change how the page looks. There are three ways to do this: link an external CSS file (the most common method), write CSS inside your HTML file using a <style> tag, or apply styles directly to individual HTML elements. Most websites use the external file method because it keeps your code organized and lets you reuse the same styles across multiple pages.
The method you choose depends on how much styling you need and whether you plan to use the same styles on other pages. A single-page project might work fine with CSS inside the HTML file, but anything larger benefits from a separate CSS file.
Key Takeaways
- The external CSS file method requires a <link> tag in your HTML <head> section that points to your CSS file's location.
- The <link> tag syntax is <link rel="stylesheet" href="styles.css">, where "styles.css" is replaced with your actual filename.
- Internal CSS uses a <style> tag in the <head> section and works for single pages but doesn't carry over to other HTML files.
- Inline CSS applies styles directly to individual elements using the style attribute, but this method becomes difficult to manage as your project grows.
Linking an external CSS file (the standard method)
This is the approach used by nearly all professional websites. You create a separate file with a .css extension (for example, styles.css or main.css), write your CSS rules in it, then tell your HTML file where to find it using a <link> tag.
Place the <link> tag inside the <head> section of your HTML file, before the closing </head> tag. The tag looks like this:
<link rel="stylesheet" href="styles.css">
The href attribute tells the browser where your CSS file is located. If your CSS file is in the same folder as your HTML file, you only need the filename. If it's in a subfolder called css, write href="css/styles.css". If it's in a parent folder, write href="../styles.css".
Once the link is in place, any CSS rules you write in your styles.css file will automatically apply to your HTML. You can link multiple CSS files if you need to — just add another <link> tag for each one.
Writing CSS inside your HTML file
If you're building something small or learning, you can put CSS directly in your HTML using a <style> tag. Place this tag in the <head> section, and write your CSS rules between the opening and closing tags.
Here's what it looks like:
<head> <style> h1 { color: blue; font-size: 24px; } </style> </head>
This method works fine for a single HTML page, but the styles only apply to that one file. If you create a second HTML page and want the same styling, you'd have to copy and paste the <style> block into that file too. This is why external CSS files are better for anything beyond a quick test — they let you update styles in one place and have the changes appear everywhere.
Applying styles directly to HTML elements
You can also add a style attribute directly to any HTML tag. This is called inline CSS. For example:
<h1 style="color: blue; font-size: 24px;">Welcome</h1>
Inline styles work immediately and are useful for quick testing, but they become hard to manage as your project grows. If you need to change the color of all your headings, you'd have to edit each one individually instead of changing one rule in your CSS file. Most developers avoid inline styles in production code for this reason.
Checking that your CSS is actually connected
If your styles aren't showing up, the connection is likely broken. Open your browser's developer tools by pressing F12 (or Cmd+Option+I on Mac), then click the Console tab. Look for red error messages mentioning your CSS file — they usually say something like "Failed to load resource" or show a 404 error.
If you see an error, check three things: First, make sure the filename in your href attribute matches your actual CSS filename exactly, including capitalization. Second, verify the file path is correct — if your CSS file is in a subfolder, the path must reflect that. Third, confirm your CSS file actually exists in the location you specified.
If there's no error message but your styles still aren't working, open the Elements or Inspector tab in developer tools, right-click on an element you styled, and select "Inspect" or "Inspect Element". Look at the Styles panel on the right — it will show you which CSS rules are being applied and which ones are being ignored, helping you spot conflicts or typos in your code.
File organization for larger projects
As your project grows, keeping all your CSS in one file can become unwieldy. Many developers create a folder structure like this: a css folder for stylesheets, a js folder for JavaScript files, and an images folder for pictures. Your HTML files stay in the root folder.
If you organize this way, your <link> tag would look like <link rel="stylesheet" href="css/styles.css">. You can also split your CSS across multiple files — for example, styles.css for general page layout and buttons.css for button-specific styles — and link both files in your HTML. The order matters: if two files style the same element, the last linked file wins.
Frequently Asked Questions
What's the difference between rel="stylesheet" and other rel values?
The rel attribute tells the browser what kind of file you're linking to. rel="stylesheet" means it's a CSS file that should style your page. Other values like rel="icon" (for a favicon) or rel="alternate" (for alternate versions of your page) tell the browser to handle the file differently. For CSS, always use rel="stylesheet".
Can I link CSS from a different website?
Yes. You can link to CSS hosted on another server by using the full URL in your href attribute, like href="https://example.com/styles.css". This is common for using CSS frameworks or icon libraries, but it means your page depends on that external server staying online and fast.
Does the order of my <link> tags matter?
Yes. If two CSS files style the same element, the last linked file takes priority. If you link a general styles.css first and then a buttons.css that also styles buttons, the button styles from buttons.css will be used. This is useful for organizing code, but it can also cause confusion if you're not paying attention.
What if my CSS file is in a parent folder?
Use ../ to go up one folder level. If your HTML is in a pages folder and your CSS is in the root folder, write href="../styles.css". For two levels up, use ../../styles.css. This relative path approach works as long as your folder structure stays the same.
Is there a difference between .css and .CSS file extensions?
On Mac and Linux, .css and .CSS are treated as different files, so your href must match exactly. On Windows, they're treated the same. To avoid confusion, always use lowercase .css — it's the standard convention and works everywhere.