iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

CSS How To

CSS can be attached to a page in three places. They all reach the same renderer, but they trade off scope, reuse, and ease of editing.

Three places CSS can live

Inline <h1 style="…"> Highest specificity One element only Use for one-off tweaks Internal <style> in <head> Per-page styles No extra HTTP request Use for landing pages External <link rel="stylesheet"> Shared across pages Cacheable, scalable Use for production sites
Fig 1. Three ways to attach CSS, ordered roughly by scope.

Pick the right one

MethodBest forWatch out for
ExternalShared styles across pages and teams.Don't ship hundreds of small files — bundle them.
InternalA single page that needs unique styling, or critical above-the-fold CSS.Doesn't get cached separately from the HTML.
InlineQuick demos, dynamic values from a server template, email HTML.High specificity makes overrides painful. Hard to maintain.

Cascade order (simplified)

When the same property is set in more than one place, the browser walks through:

  1. Importance — anything tagged !important wins first.
  2. Specificity — more specific selectors beat less specific ones.
  3. Source order — last declaration wins among ties.
Tip: In a typical site, inline a tiny bit of critical CSS in <head> for the first paint, then load the main stylesheet externally with <link rel="stylesheet">.

Example

Example
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Verdana, sans-serif; }
.box { background: #04AA6D; color: #fff; padding: 20px; border-radius: 6px; }
</style>
</head>
<body>

<h1>CSS How To</h1>
<div class="box">Edit the CSS on the left, then click Run &raquo;</div>

</body>
</html>
Try it Yourself »

Exercise

Add the missing tag so this <style> block is recognised by the browser.

<head> < > body { font-family: sans-serif; } </style> </head>

Test yourself

Q1. Which tag links to an external stylesheet from the document head?
Q2. Where do internal styles live?
Q3. Which is the best choice for styling a production site shared across many pages?

Discussion

Loading…