Sub topic 9
Choosing the right method
This chapter assumes you can write a basic HTML page, know the shape of a CSS rule (selector, property, value), and have used the style attribute, the style element, and a linked stylesheet. Here you compare the three methods and pick one for a given project.
The three methods side by side
Each method solves a different problem. The table summarizes how they differ on the points that matter in practice.
| Concern | Inline (`style` attribute) | Internal (`<style>` element) | External (`<link>` to a .css file) |
|---|---|---|---|
| Scope | One element | One page | Any page that links the file |
| Reuse | None. Repeat the declarations on each element | Within the page only | Shared across the whole site |
| Maintenance | Hardest. Styles are scattered through the markup | Moderate. All rules sit in one place per page | Easiest. Change one file, update every page |
| Caching | Cached only with the HTML | Cached only with the HTML | Cached separately and reused between pages |
| Content Security Policy | Blocked by a strict `style-src` unless allowed | Blocked by a strict `style-src` unless allowed | Allowed by `style-src 'self'` when the file is same-origin |
| Specificity | Beats selector rules (without `!important`) | Same as any selector rule | Same as any selector rule |
Decision criteria
Ask these questions in order.
- Will more than one page use these rules? If yes, use an external stylesheet. One file means one place to edit, and the browser caches it after the first page.
- Does the page need a strict Content Security Policy? If yes, prefer external files. A policy such as
style-src 'self'blocks style elements and style attributes unless you add a nonce or'unsafe-inline'. - Is it a single, self-contained page? Examples are a one-off landing page, an HTML email template, or a quick demo. An internal style element keeps everything in one file and avoids an extra request.
- Is the value computed or unique to one element? A progress bar width set by a script is a good case for an inline style. Use inline styles for dynamic, per-element values and not for general design.
<div class="progress">
<div class="progress-bar" style="width: 72%;"></div>
</div>Here the look of the bar (color, height) belongs in a stylesheet. Only the changing width is inline.
A common arrangement
Many sites combine the methods, each for what it does best.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Combined approach</title>
<link rel="stylesheet" href="css/site.css">
<link rel="stylesheet" href="print.css" media="print">
<style>
.hero { background: #eef; padding: 2rem; }
</style>
</head>
<body>
<div class="hero">
<p style="color: crimson;">One-off emphasis on this element.</p>
</div>
</body>
</html>site.cssholds the shared design: fonts, colors, layout.print.cssloads only when printing, because of themediaattribute.- The
<style>element holds rules that only this page needs. - The inline style handles a single exception.
Because the <style> element comes after the <link>, its rules win over site.css when specificity is equal. Keep this order in mind when you mix methods.
Common mistakes
- Using inline styles for repeated design. Ten paragraphs with the same
styleattribute should be one rule with a selector. - Copying the same
<style>block into every page. The copies drift apart over time. Move shared rules into an external file. - Chaining
@importrules. Each import is fetched only after the file that contains it has loaded. Separate<link>elements let the browser fetch files in parallel. - Forgetting the CSP. Code that works locally can lose all its styling in production when a strict
style-srcheader is added.
Exercise
A five-page site shares one color scheme. Page 3 needs one extra rule for a special table, and a script sets the width of a progress bar on page 5. The server sends Content-Security-Policy: style-src 'self'. Which method do you use for each need, and what must you change for the page 3 rule and the progress bar to work?
Show answer
Put the shared color scheme in an external stylesheet linked from all five pages. The page 3 rule would normally go in a style element, but the policy blocks it, so move it into an external file (for example a page-specific table.css linked only from page 3). The progress bar width would normally be an inline style, but the policy blocks the style attribute too. Either set the width through script using the CSSOM (for example element.style.width = "72%", which CSP allows) or relax the policy with a nonce for style elements. Do not add 'unsafe-inline' unless you accept the weaker protection.