Types of CSS (inline, internal, external)?

There are three ways to add CSS to HTML: inline styles in a style attribute on one element, internal styles in a <style> block in the document, and external stylesheets linked with <link rel="stylesheet">. External stylesheets are the default choice for real sites because they are reusable and cached.

Inline: <p style="color: red">

  • Applies to one element only, with very high priority (beats any selector except !important).
  • Hard to maintain, cannot use media queries, pseudo-classes or pseudo-elements.
  • Reasonable for dynamic values set from JavaScript (el.style.setProperty("--x", ...)) or in emails.

Internal: <style> in <head>

  • Styles one page; no extra request.
  • Good for critical CSS: inlining the styles needed for above-the-fold content so the first paint is fast.

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

  • One file shared across pages and cached by the browser.
  • Clean separation of concerns and easier collaboration.
  • It is render-blocking, so keep it lean and serve it with good caching headers.

Other ways CSS arrives

  • @import inside CSS: avoid on the critical path because it chains requests.
  • CSS-in-JS, CSS Modules and Tailwind all compile down to one of these.

Priority

Inline styles beat internal and external ones (unless !important is used); between internal and external, normal cascade rules (specificity, then source order) apply.

Code example

html
1<!doctype html>
2<html lang="en">
3<head>
4 <meta charset="utf-8">
5 <title>Three ways to add CSS</title>
6 <!-- External: shared and cached (a data URL keeps this demo self-contained) -->
7 <link rel="stylesheet" href="data:text/css,p%7Bfont-family:system-ui;padding:6px%7D.box%7Bborder:2px%20solid%20%230b1020%7D">
8 <!-- Internal: this page only -->
9 <style>
10 p { color: #2b4bff; }
11 </style>
12</head>
13<body>
14 <p class="box">Styled by external + internal CSS (blue).</p>
15 <!-- Inline: one element, highest normal priority -->
16 <p class="box" style="color: #e11d48">Inline style wins (red).</p>
17 <p id="dyn" class="box">Inline styles set from JavaScript</p>
18 <script>
19 document.getElementById("dyn").style.background = "#fef9c3";
20 </script>
21</body>
22</html>

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.

Open in study app