How to Link CSS to HTML

What you’ll build or solve

You’ll connect an HTML page to CSS so changes in your stylesheet affect layout, colors, and typography.

When this approach works best

Linking CSS to HTML works best when you want to:

  • Style a full page with reusable rules, such as a site header and buttons across multiple pages.
  • Keep HTML clean by moving styling into a separate file.
  • Work on a team or a larger project where separate files make reviews and updates easier.

This is a bad idea when you are adding a single one-off style for a quick test. In that case, an inline style may be faster, but it does not scale.

Prerequisites

  • A basic HTML file you can open in a browser
  • A text editor, such as VS Code
  • No extra tools required

Step-by-step instructions

1) Link an external stylesheet with a <link> tag

This is the most common approach. Add a <link> tag inside the <head> of your HTML file.

Option A: Same folder (most common)

Project structure:

index.html
styles.css

index.html:

<!doctype html>
<htmllang="en">
<head>
<metacharset="utf-8"/>
<metaname="viewport"content="width=device-width, initial-scale=1"/>
<linkrel="stylesheet"href="styles.css"/>
<title>Demo</title>
</head>
<body>
<h1class="title">Hello</h1>
</body>
</html>

styles.css:

.title {
  color:rebeccapurple;
}

What to look for: the <link> tag belongs in <head>, and href must point to the correct file location.


Option B: CSS in a subfolder

Project structure:

index.html
css/styles.css
<linkrel="stylesheet"href="css/styles.css"/>

Option C: CSS one folder up

Project structure:

pages/index.html
styles.css
<linkrel="stylesheet"href="../styles.css"/>

2) Use a <style> block for page-specific CSS

If you only need styles for one HTML page, you can keep them inside the file.

<!doctype html>
<htmllang="en">
<head>
<metacharset="utf-8"/>
<style>
      .card {
        border:1pxsolid #ddd;
        padding:16px;
        border-radius:10px;
      }
</style>
<title>Internal CSS</title>
</head>
<body>
<divclass="card">Card content</div>
</body>
</html>

What to look for: keep the <style> block in <head> so styles load before the page renders.


3) Use inline styles for a single element

Inline styles apply to one element only. Use them for quick tests or small overrides.

<buttonstyle="background:black; color:white; padding:10px14px;">
  Buy now
</button>

What to look for: inline styles override many stylesheet rules. If a class style is not working, check whether an inline style is taking priority.


Examples you can copy

Example 1: Minimal external link setup

<!doctype html>
<htmllang="en">
<head>
<metacharset="utf-8"/>
<linkrel="stylesheet"href="styles.css"/>
<title>Page</title>
</head>
<body>
<pclass="note">Styled text</p>
</body>
</html>
.note {
  font-weight:600;
}

Example 2: Two stylesheets, base and page

<linkrel="stylesheet"href="base.css"/>
<linkrel="stylesheet"href="home.css"/>

What to look for: later files can override earlier rules when selectors have the same specificity.


Example 3: Link a CDN stylesheet

<linkrel="stylesheet"href="https://unpkg.com/modern-css-reset/dist/reset.min.css"/>

What to look for: you need an internet connection for CDN styles to load.


Example 4: Add a cache-busting query string during development

<linkrel="stylesheet"href="styles.css?v=1"/>

What to look for: browsers can cache CSS aggressively. A query string often forces a reload.


Common mistakes and how to fix them

Mistake 1: Wrong file path in href

What you might do

<linkrel="stylesheet"href="/styles.css"/>

Why it breaks

A leading / points to the site root. When you open an HTML file locally, that usually does not map to your project folder.

Corrected approach

Use a relative path:

<linkrel="stylesheet"href="styles.css"/>

Mistake 2: Putting the <link> tag in the wrong place

What you might do

<body>
<linkrel="stylesheet"href="styles.css"/>
<h1>Hello</h1>
</body>

Why it breaks

Browsers may still load it, but it can cause flashes of unstyled content and confusing behavior.

Corrected approach

Move it into <head>:

<head>
<linkrel="stylesheet"href="styles.css"/>
</head>

Troubleshooting

  • If your CSS changes do not show up, hard refresh the page. Use Ctrl + Shift + R on Windows or Linux, or Cmd + Shift + R on macOS.
  • If the page is still unstyled, open DevTools, go to Network, refresh, and check if your CSS file returns a 404.
  • If your stylesheet loads but a rule does not apply, inspect the element and check the Styles panel to see which rule wins.
  • If styles apply sometimes but not always, remove inline styles and check whether another stylesheet overrides your rule.
  • If your CSS link works on one page but not another, re-check the relative path from that HTML file’s location.

Quick recap

  • Use <link rel="stylesheet" href="..."> in <head> for most projects.
  • Use a <style> block for page-specific styles.
  • Use inline styles only for small one-off changes.