HTML and CSS are two separate files that talk to each other

HTML is the structure — the words, images, and headings on your page. CSS is the styling — the colors, fonts, spacing, and layout. They live in different files, but HTML needs to know where to find your CSS file, or the browser will ignore your styles and show plain, unstyled text.

The connection happens with a single line of code in your HTML file's <head> section. This line tells the browser: "Go find this CSS file and use it to style this page." Without it, your HTML and CSS are just two separate documents that never meet.

Key Takeaways

  • Add a <link> tag in the <head> section of your HTML file to connect your CSS file.
  • The href attribute in the link tag must point to the correct file path where your CSS file is saved.
  • If your CSS file is in the same folder as your HTML file, use just the filename: href="styles.css".
  • If your CSS file is in a subfolder, include the folder name in the path: href="css/styles.css".
  • You can also write CSS directly inside your HTML file using a <style> tag, but linking a separate file keeps your code organized.

The link tag: the one line that connects everything

Open your HTML file in a text editor. Find the <head> section — it's the part between <head> and </head>, usually near the top of the file, before the <body> tag. Inside the head, add this line:

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

This line has three important parts. The rel="stylesheet" tells the browser this is a CSS file. The href="styles.css" tells the browser where to find it. The filename "styles.css" is just an example — your CSS file might be named something else, like "main.css" or "design.css".

The browser reads this line and when ready goes looking for the file. If it finds it, the styles load. If it doesn't find it, the page loads without any styling.

File paths: telling the browser exactly where your CSS file is

The href attribute needs the correct path to your CSS file. The path depends on where you saved the file relative to your HTML file.

If your HTML file and CSS file are in the same folder, the path is just the filename: href="styles.css". If your CSS file is in a subfolder called "css", the path is href="css/styles.css". If your CSS file is in a subfolder called "assets" inside a subfolder called "styles", the path is href="assets/styles/styles.css".

The forward slash (/) means "go into this folder". So css/styles.css means "go into the css folder, then find styles.css". If you get the path wrong, the browser won't find the file and your styles won't load. Check your folder structure in your file manager or code editor to make sure the path matches where you actually saved the file.

A complete example: HTML file with the link tag in place

Here's what a working HTML file looks like with the CSS connection:

<!DOCTYPE html> <html> <head>   <meta charset="UTF-8">   <meta name="viewport" content="width=device-width, initial-scale=1.0">   <title>My Website</title>   <link rel="stylesheet" href="styles.css"> </head> <body>   <h1>Welcome</h1>   <p>This page is styled by styles.css</p> </body> </html>

The <link> tag sits in the <head> section, before the closing </head> tag. The <body> section contains the actual content — headings, paragraphs, images — that the CSS will style. When the browser loads this page, it reads the link tag, finds styles.css, and applies all the CSS rules to the HTML elements.

Inline styles and style tags: alternatives when you don't want a separate file

You don't have to use a separate CSS file. You can write CSS directly in your HTML file using a <style> tag, also in the <head> section. This works for small projects or testing, but it makes your HTML file longer and harder to read.

You can also add styles directly to individual HTML elements using the style attribute, like <p style="color: blue;">. This is called an inline style. It works, but it's hard to maintain because the styling is scattered throughout your HTML instead of organized in one place.

For any project larger than a single test page, linking a separate CSS file is the better choice. It keeps your HTML clean, makes your CSS easier to find and edit, and lets you use the same CSS file on multiple HTML pages.

Troubleshooting: why your CSS might not be loading

If you added the link tag but your styles still aren't showing, check these things in order. First, make sure the filename in the href attribute matches the actual filename of your CSS file exactly — including capitalization. "Styles.css" and "styles.css" are different filenames on most computers. Second, check the file path. Open your file manager and look at where your CSS file is actually saved. Make sure the path in the href matches that location.

Third, save both your HTML and CSS files, then refresh the page in your browser. Sometimes the browser holds onto an old version of the page. Use Ctrl+Shift+R (Windows) or Command+Shift+R (Mac) to do a hard refresh that clears the cached version. Fourth, open the browser's developer tools by pressing F12 or right-clicking and selecting "Inspect". Look at the Console tab for error messages. The browser will often tell you if it couldn't find the CSS file.

If the file path is correct but the styles still don't work, open your CSS file and make sure it actually contains CSS rules. An empty file or a file with broken syntax won't cause an error message — the browser will just have nothing to explore.

Frequently Asked Questions

Can I link multiple CSS files to one HTML file?

Yes. Add multiple <link> tags in the <head> section, one for each CSS file. The browser will load them in order from top to bottom. If two CSS files style the same element differently, the one listed last wins.

What's the difference between href and src?

The href attribute is for linking to files that describe or style the page — CSS files, other HTML pages, or resources. The src attribute is for embedding files directly into the page — images, videos, or scripts. Use href for CSS.

Do I need to use a specific CSS filename?

No. You can name your CSS file anything — "styles.css", "main.css", "design.css", or "purple-dinosaurs.css". The filename doesn't matter. What matters is that the href in your link tag matches the actual filename exactly.

What if my CSS file is in a parent folder instead of a subfolder?

Use ../ to go up one level. If your HTML file is in a subfolder called "pages" and your CSS file is in the parent folder, use href="../styles.css". Each ../ moves up one folder level.

Can I use an absolute web address in the href instead of a file path?

Yes. You can link to a CSS file hosted on the internet by using its full web address: href="https://example.com/styles.css". This is useful for loading CSS libraries or fonts from a content delivery network, but for your own CSS files, a relative file path is simpler and faster.