Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14
Chapter 3

Levels of Style Sheets & Cascade Priority

Inline, embedded and external CSS — plus @import vs @layer.


CSS Cascade Priority

From highest to lowest priority:

  1. Inline styles — <h1 style="color:red">. Applied directly on the element; highest priority.
  2. Embedded / document — <style> h1 { color: red; } </style> in the <head>. Applies to the whole document.
  3. External stylesheet — a separate .css file that can be shared across many pages.
  4. Browser default — the user-agent stylesheet. Lowest priority.

Inline Style Sheets

Placed directly inside the HTML tag using the style attribute. Applies only to that single element — the most specific.

<h1 style="color: #008408; font-size: 2rem;">Styled Heading</h1>
  • Useful for quick one-off overrides or dynamic styling via JavaScript.
  • 2025 warning: avoid heavy use — hard to maintain and can't be cached.
  • Better alternative: CSS custom properties + utility classes.

Embedded (Document-Level) Style Sheets

Placed inside a <style> tag in the <head>. Applies to the whole document.

<head>
  <style>
    /* Document-level styles apply to this page only */
    body {
      background-color: #001B2A;
      color: #CAD7EE;
      font-family: 'Segoe UI', sans-serif;
    }
    h1 { color: #00B4D8; }
    h1, h3, p { margin-bottom: 1rem; }
    p.tip {
      background: #142236;
      padding: 0.75rem;
      border-left: 4px solid #FFB703;
    }
  </style>
</head>
  • The type="text/css" attribute is optional in HTML5.
  • Use /* */ for CSS comments.
  • Good for single-page apps, page-specific overrides and prototyping.

External Style Sheets

Stored in a separate .css file, linked to one or more HTML pages.

<!-- HTML file: index.html -->
<head>
  <link rel="stylesheet" href="style.css">
</head>
/* style.css */
body {
  background: #001B2A;
  color: #CAD7EE;
  font-family: 'Segoe UI', sans-serif;
  font-size: 1rem;
}
h1 { color: #00B4D8; font-size: 2rem; }
h2 { color: #90E0EF; }

Why external CSS is preferred

  • Single file controls all pages — update once, site-wide change.
  • The browser caches the .css file — faster repeat visits.
  • Cleaner HTML — no style clutter mixed with content.

You can also import: @import url('style.css') inside another CSS file.

@import vs @layer — 2025 Best Practice

  • @import loads another CSS file from within a CSS file. Avoid chaining multiple imports — it blocks parallel loading.
  • The modern approach is CSS Cascade Layers with @layer, which controls specificity without using !important. Layers declared first have lower priority than later layers.
/* Traditional @import (still valid) */
@import url('reset.css');
@import url('theme.css');

/* 2025: CSS Cascade Layers */
@layer reset, base, components, utilities;

@layer reset {
  * { margin: 0; padding: 0; box-sizing: border-box; }
}
@layer base {
  body { font-family: system-ui; }
}
@layer components {
  .card { border-radius: 0.5rem; }
}
@layer utilities {
  .text-center { text-align: center; }
}
/* Later layers win — no !important needed */

Activity — complete the code

Complete the code correctly to mark this lesson as complete automatically.

Complete the three levels of style sheets.

<!-- 1. Inline -->
<h1 ="color: red;">Inline</h1>
 
<!-- 2. Embedded -->
<head>
<>
h1 { color: red; }
</style>
</head>
 
<!-- 3. External -->
<link rel="" href="style.css">