Skip to content
Kuza Ndoto Academy

Adding CSS to HTML

Beginner → IntermediateChapter 1 · Page 2 of 223 min readUpdated 15 Nov 2026

There are three ways to connect CSS to an HTML page: inline styles on a single element, an internal stylesheet inside a <style> element, and an external stylesheet in its own .css file. Knowing when to use each will keep your projects tidy as they grow.

1. Inline styles

An inline style is written directly on an element using the style attribute. It affects only that one element.

<p style="color: crimson; font-weight: bold;">Offer ends on Friday!</p>

The paragraph appears in bold crimson text. Inline styles are quick for testing, but they mix presentation into your HTML, cannot be reused, and are hard to override because they beat almost every rule in a stylesheet. Avoid them in real projects. You will sometimes see them in HTML emails and in styles set by JavaScript.

2. Internal stylesheet

An internal (or embedded) stylesheet lives inside a <style> element, normally in the <head>. Its rules apply to the whole page.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Internal CSS</title>
  <style>
    body {
      font-family: system-ui, sans-serif;
      margin: 2rem;
    }
    h1 {
      color: #0f766e;
    }
    .notice {
      background-color: #fef3c7;
      padding: 1rem;
      border-left: 4px solid #d97706;
    }
  </style>
</head>
<body>
  <h1>Kuza Coding Club</h1>
  <p class="notice">Next meet-up: Saturday, 10 a.m., in the library.</p>
  <p>Bring a laptop if you have one. Everyone is welcome.</p>
</body>
</html>

The heading is teal. The first paragraph sits in a pale yellow box with an orange stripe down its left side, and the second paragraph stays plain because it does not have the notice class. Internal stylesheets are handy for single-page demos (like most examples in this series), but the styles cannot be shared with other pages.

For real websites, put your CSS in a separate file and link it from every page with a <link> element in the head.

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

h1 {
  color: #0f766e;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Home</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <h1>Welcome</h1>
</body>
</html>

The href is a path relative to the HTML file, so here styles.css must sit in the same folder. A common layout is a css folder, linked with href="css/styles.css". Every page that links the file gets the same styles, and the browser caches the file after the first download, so later pages load faster.

Common mistake

If your styles "do nothing", check the path first. A typo such as style.css instead of styles.css, or a file in the wrong folder, means the browser silently finds nothing. Open DevTools, go to the Network tab, reload and look for a red 404 next to the CSS file.

Comparing the three methods

MethodWhere it goesScopeBest for
Inlinestyle attribute on an elementThat element onlyQuick tests, HTML emails, values set by JavaScript
Internal<style> in the headOne pageDemos, single-page experiments
ExternalA .css file linked with <link>Every page that links itReal websites

When methods conflict

You can use all three at once. If they disagree about the same property on the same element, inline styles win over stylesheet rules (unless !important is involved). Between an internal and an external stylesheet, neither is automatically stronger: with equally specific selectors, whichever rule comes later in the page wins. So if the <link> comes after the <style> element, the external file's rule wins.

<head>
  <style>
    h1 { color: green; }
  </style>
  <link rel="stylesheet" href="styles.css"> <!-- contains h1 { color: purple; } -->
</head>
<body>
  <h1>Which colour?</h1>
</body>

The heading is purple, because the external rule is read last.

Comments in CSS

CSS comments start with /* and end with */. They can span several lines and are ignored by the browser. There is no // single-line comment in plain CSS.

/* ===== Header styles ===== */
header {
  padding: 1rem; /* space inside the header */
}

Key points

  • Inline styles use the style attribute and affect one element; avoid them in real projects.
  • Internal stylesheets go in a <style> element and style one page.
  • External stylesheets, linked with <link rel="stylesheet">, are the professional choice.
  • With equal specificity, the rule that appears later wins.
Up next · Getting StartedSyntax & SelectorsRules, declarations and the main selector types.
→

Tip: use the ← and → keys to move between pages.