Thuta Learning
CSS
BasicWeb DevelopmentBeginner

How To Add CSS

CSSLesson 420 min

What you'll walk away with

  • Write <link rel="stylesheet">, <style> and a style="" attribute in the right places
  • Predict that, at equal specificity, source order (the later one) decides between external and internal CSS
  • Explain why an inline style beats selector-based rules and makes later changes harder
  • Know that a url() path inside a CSS file is relative to that CSS file, not to the HTML page

Explain it simply

Explain it simply

There are three places to put CSS. An external stylesheet is a separate .css file that many pages share through <link>. Internal CSS is a <style> block in the <head> of one page. An inline style is written straight into the style attribute of one element.

Compare it with house rules: the external stylesheet is the rule sheet everyone in the house follows, internal CSS is a note taped to one room's door, and an inline style is an exception told directly to one person.

Problem → Why → Solution

The problem: you edit the stylesheet, but the price colour will not change

In the first version, a developer made 30 product prices red as fast as possible by adding style="color: red;" to each element. Months later a new design wants green prices, so .price { color: green; } goes into shop.css. Nothing changes. The next developer "fixes" it with !important, and an !important arms race begins.

shop.html
<link rel="stylesheet" href="shop.css">  <!-- contains .price { color: green; } -->

<p class="price" style="color: red;">$19</p>  <!-- still red -->
<p class="price" style="color: red;">$24</p>  <!-- still red -->

Why does this happen?

Declarations in a style attribute beat every selector-based rule in a stylesheet (unless that rule uses !important), however specific its selector. So the more inline styles you add, the more control the stylesheet loses, and every change means editing HTML elements one by one again.

Core concept: three places, one cascade

MethodWhere it livesBest for
ExternalA .css file linked with <link> in <head>Real sites, styles shared by many pages
InternalA <style> block in <head>Single-page experiments, demos, email templates
InlineAn element's style="" attributeOne dynamic value computed by JavaScript

All three methods produce author styles and enter the same cascade. Neither external nor internal CSS outranks the other by nature; "internal always beats external" is a myth. When selector specificity is equal, the rule that appears later in the HTML wins, so a <link> placed after a <style> block lets the external file win. Inline styles are different: they target the element directly, without a selector, and beat every selector-based rule.

External files also help performance. The browser downloads a .css file once and caches it, so the next pages do not download it again, whereas internal and inline CSS are re-sent with every HTML page. The browser waits for stylesheets in <head> before drawing the page (they are render-blocking), so visitors never see a flash of unstyled content; that is why <link> belongs in <head>.

Inline styles have limits too: states such as :hover, media queries and pseudo-elements such as ::before cannot be written inside a style attribute. Keep inline styles for exceptions only.

One stylesheet can also pull in another with @import url("forms.css");, which must sit at the top of the file. But the browser only discovers the @import after downloading and reading the first file, so the second download starts later. That one-after-another chain slows the page down, so it is better to list several <link> tags side by side in the HTML or let a build tool merge the files.

Mental model: how the browser gathers CSS
  1. Read the HTML top to bottom
  2. Collect <link> and <style> rules
  3. Equal specificity: later rule wins
  4. Inline style beats selector rules

Between external and internal CSS, position in the source decides, not the kind of file.

General pattern: the three methods

page.html
<head>
  <link rel="stylesheet" href="path/to/file.css">  <!-- external -->
  <style>
    selector { property: value; }                  /* internal */
  </style>
</head>
<body>
  <element style="property: value;">...</element>  <!-- inline -->
</body>

Both <link> and <style> belong in <head>. href is a path from the HTML file to the CSS file, and a style attribute holds only declarations, with no selector and no braces.

Example: all three methods on one page

Create two files in one folder. The first is styles.css, containing a single rule:

styles.css
body {
  font-family: system-ui, sans-serif;
  margin: 2rem;
}

The second is index.html below. Open it, inspect both buttons, and notice that the Styles pane shows the inline style at the top under "element.style", with the .btn background-color crossed out beneath it.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Three ways to add CSS</title>
  <!-- 1. external: one file shared by every page that links it -->
  <link rel="stylesheet" href="styles.css">
  <!-- 2. internal: rules for this page only -->
  <style>
    .btn {
      padding: 8px 16px;
      border: none;
      color: white;
      background-color: #1d4ed8;
    }
  </style>
</head>
<body>
  <h1>Account</h1>
  <button class="btn">Save</button>
  <!-- 3. inline: this one element only -->
  <button class="btn" style="background-color: #b91c1c;">Delete</button>
</body>
</html>

Code breakdown

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

    rel="stylesheet" tells the browser the file is CSS, and href="styles.css" points to a file in the same folder as the HTML. A wrong file name shows no error, just an unstyled page, so check the Network tab in DevTools for a 404.

  2. Internal block<style>

    The .btn rule in this block applies only to index.html. If another page needs the same buttons, the rule should move into styles.css.

  3. Class rule for both buttonsbackground-color: #1d4ed8;

    Both buttons have class="btn", so this declaration selects both. On "Save" nothing competes with it, so the button is blue.

  4. Inline override<button class="btn" style="background-color: #b91c1c;">Delete</button>

    The background-color in the style attribute beats .btn's blue. padding and color are not written inline, so they still come from .btn: an inline style replaces only the properties it actually sets.

You should see
The "Account" heading gets the sans-serif font from styles.css, and the page has a 2rem (32px) margin. Both buttons get padding and white text. "Save" is blue (#1d4ed8); "Delete" is dark red (#b91c1c) because its inline style beats .btn.

Paths inside a CSS file

As a project grows, CSS usually moves into a css/ folder. An href in the HTML is resolved from the HTML file, but a url() inside a CSS file is resolved from the CSS file. So url(img/bg.png) in css/site.css makes the browser look for css/img/bg.png; to reach the img/ folder at the project root, write ../img/bg.png. A broken image path fails silently too, so look for the red 404 line in the Network tab.

text
Where each path is resolved from
--------------------------------
project/
  index.html     <link href="css/site.css">   (from index.html)
  css/
    site.css     url(../img/bg.png)           (from site.css)
  img/
    bg.png

In real projects

Where this shows up in real projects

On most production sites, a build tool (Vite, Next.js and so on) merges many CSS files, minifies them, and outputs one or a few external files with a content hash in the name, such as app-3f9a1c.css. When the content changes, the name changes, so browsers can cache the file for a long time and still get updates immediately.

Internal and inline CSS still have their places. HTML emails rely mainly on inline styles because many email clients ignore external stylesheets. JavaScript writes values it computes at run time, such as a progress bar's width (style.width = "40%"), as inline styles. On security-hardened sites a Content-Security-Policy header can block inline styles, which makes external files the safest default.

Common mistakes

Common mistakes

1. A wrong href path

Writing href="styles.css" when the file lives in a css/ folder, or Styles.css versus styles.css (Linux servers are case-sensitive), are the most common slips. If the page shows no styling at all, check the CSS file's status in the Network tab.

2. Mixing up the wrappers

A .css file contains only CSS rules; adding a <style> tag to it makes the browser drop the first rule as an invalid selector. The reverse slip, writing CSS rules in the HTML without a <style> wrapper, prints them on the page as text.

3. Reaching for inline styles to win every override

Inline styles are tempting because they always win, but afterwards only !important can change them from the stylesheet. When a rule loses, check in DevTools which rule is winning and fix the selector or the order instead.

Best practices

Best practices

  • Start every new site with an external stylesheet, even for a single page, so adding pages later costs nothing extra.
  • Keep <link> tags in <head> and order them from general (reset, base) to specific (one page), because the later file wins ties.
  • When JavaScript must set an inline style, set only the one property (width, transform) and keep the rest of the look in classes.
  • After experimenting in an internal <style> block, remember to move the rules into a .css file.

Mini challenge

Mini challenge: turn inline styles into rules

The menu in the starter is full of inline styles. Keep the same look, but move every style into class rules in a <style> block in <head> and delete the style attributes.

  • No style="" attribute may remain
  • The two repeated styles (the menu item paragraphs) must be selected by one class rule
  • Add a comment at the top of the <style> block noting it can later move to menu.css
  • The page must look the same as before in the browser
menu.html
<!-- TODO: add <!DOCTYPE html>, <head> with a <style> block, and <body> -->
<!-- TODO: move each style="..." into a class rule, then delete the attribute -->
<h2 style="color: #1d4ed8; font-family: Georgia, serif;">Menu</h2>
<p style="color: #374151; line-height: 1.6;">Mohinga - $4</p>
<p style="color: #374151; line-height: 1.6;">Tea leaf salad - $5</p>
<p style="color: #166534; font-weight: bold;">Today: free green tea</p>

Expected: The page looks exactly as before: a blue Georgia heading, two dark grey menu items and a bold green offer line. The difference is that the HTML now carries only classes.

Show hint

Name classes after meaning (menu-title, menu-item, offer) rather than appearance (blue-text), so the names stay true even if the colours change later.

Show solution
menu.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Menu</title>
  <style>
    /* later: cut these rules into menu.css and use <link> instead */
    .menu-title {
      color: #1d4ed8;
      font-family: Georgia, serif;
    }
    .menu-item {
      color: #374151;
      line-height: 1.6;
    }
    .offer {
      color: #166534;
      font-weight: bold;
    }
  </style>
</head>
<body>
  <h2 class="menu-title">Menu</h2>
  <p class="menu-item">Mohinga - $4</p>
  <p class="menu-item">Tea leaf salad - $5</p>
  <p class="offer">Today: free green tea</p>
</body>
</html>

Quick quiz 1 / 3

<head> contains <style> .btn { background: blue; } </style>, followed by a <link> to a file with .btn { background: green; }. What colour is the button?

Quick quiz 2 / 3

Which of these cannot be written in a style="" attribute?

Quick quiz 3 / 3

css/site.css contains background-image: url(img/bg.png);, and index.html is at the project root. Which file does the browser request?

What to remember

  • External CSS (<link>) is the default for real sites: shared by many pages and cached.
  • Internal CSS (<style>) suits single-page experiments and demos.
  • At equal specificity, the rule that comes later in the HTML wins between external and internal CSS.
  • Inline styles beat all selector rules, so keep them for exceptions such as dynamic values.

CSS reference — MDN Web Docs

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

How To Add CSS | Thuta Learning