How CSS Works and Where It Lives
What a CSS rule is made of, the three places styles can come from, and how the browser turns a stylesheet into the look of a page.
How CSS Works and Where It Lives
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A CSS rule is a selector plus a declaration block of `property: value;` pairs. The selector picks elements, the declarations say what to change.
- Styles can come from three places: an external file pulled in with `<link rel="stylesheet">`, a `<style>` element, and a `style=""` attribute on one element. External files are the default for real projects.
- The browser parses CSS into a rule tree, matches it against the DOM, and re-runs that matching whenever the DOM or a stylesheet changes. A style is never 'applied once'.
- A declaration the browser cannot parse is skipped on its own. Its neighbours and the rest of the stylesheet still work, which is what lets you write fallbacks.
- Every element already has styles before you write any, from the browser's built-in user-agent stylesheet. That is why a bare `<h1>` is big and bold.
The problem CSS solves
HTML says what a page is made of: a heading, three paragraphs, a list. It says nothing about how any of it looks. If styling lived in the HTML you would be repeating color and font-size on every heading, and changing the look of the site would mean editing every page.
CSS splits those two jobs. The HTML stays a description of content. A separate set of rules says “every h2 gets this size and this color”, and the browser applies the rules to whatever the HTML contains. Change one rule and every matching element changes. Think of it as a style guide that a print shop follows: the manuscript does not mention fonts, and the guide is written once.
Anatomy of a rule
h2 {
color: rebeccapurple;
margin-block-end: 0.5rem;
}
h2 is the selector: which elements this applies to. The braces hold the declaration block. Each declaration is a property, a colon, a value, and a semicolon. A stylesheet is a list of these rules, plus at-rules such as @media and @layer that start with @ and wrap or configure other rules.
Selectors get their own lesson in selectors and combinators. For now, hold on to the shape: what to pick, then what to change.
Three places a style can live
An external stylesheet. A separate .css file, pulled in from the <head>:
<link rel="stylesheet" href="/styles/site.css">
This is the default for almost everything. One file is shared by every page, the browser can cache it after the first visit, and it keeps CSS out of the HTML.
A <style> element. The rules sit inside the HTML document itself:
<style>
body { font-family: system-ui, sans-serif; }
</style>
Useful for a one-page demo, an email template, or a few critical rules you want to ship inline for speed. It is not shared between pages.
A style attribute. Declarations written directly on one element:
<p style="color: crimson">Careful.</p>
This styles exactly one element and nothing else. It also outranks selectors from stylesheets, as specificity and the cascade explains, which makes it awkward to override later. JavaScript sets styles this way as well, which is its main legitimate use.
There is a fourth route: @import url("other.css"); at the top of a stylesheet. Avoid it for loading files. The browser only learns about the second file after it has downloaded and parsed the first, so the downloads queue up instead of running in parallel. Two <link> elements load side by side.
What the browser does with it
The browser downloads and parses each stylesheet into a tree of rules, builds the DOM from the HTML, and then matches rules to elements. For every element it works out one value per property, a process the next few lessons take apart. Then layout and painting happen. Stylesheets in the <head> hold up the first paint, because drawing the page before its styles arrive would flash unstyled content. That is part of why you want stylesheets small and early; how browsers render a page walks through the whole pipeline.
Matching is not one-off. Add a class from JavaScript, resize the window past a media query, or hover a link, and the browser re-evaluates which rules apply and updates what you see.
Bad CSS is skipped, not fatal
CSS is deliberately forgiving. A declaration with an unknown property or an invalid value is ignored, and parsing carries on at the next semicolon:
p {
color: rgb(255 0 0);
color: banana; /* invalid: dropped */
margin: oops; /* invalid: dropped */
padding: 5px; /* still applies */
}
I ran this in Chromium: the paragraph came out red with 5px of padding. This forgiving rule is also how the web adds features without breaking old browsers. You write the old value first and the new value after it, and a browser that does not understand the new one skips it and keeps the old.
You start with styles you did not write
Before your CSS runs, the browser has already styled everything with its user-agent stylesheet: headings are large and bold, links are blue, <ul> has bullets and left padding, <body> has an 8px margin. Your stylesheet sits above it and overrides it. Many projects add a small reset on top to flatten differences between browsers.
What the video covers
Kevin Powell’s clip is “HTML & CSS for Absolute Beginners: Creating and linking to a CSS file”, under four minutes, from his free beginner course. By its title it shows creating a .css file and connecting it to a page. I could not read a transcript, so the description above of the other two placements, @import, and the forgiving parser comes from this lesson’s prose, not from the video. The runner-up under “go deeper” covers inline and internal styles.
Where this fits
Everything in the CSS roadmap runs on this loop: rules written in a stylesheet, matched to elements, resolved to one value each. Next, selectors and combinators covers how a rule picks its elements. The earlier, shallower tour of the same ground is in the box model and cascade lesson of the frontend roadmap.
Check yourself
A stylesheet contains `color: rgb(255 0 0); color: banana; padding: 5px;` in one rule. What does the element end up with?
Red text and 5px of padding. `color: banana` is not a valid value, so that one declaration is thrown away, and the earlier valid `color` and the `padding` declaration are untouched. Only the bad declaration is dropped, not the whole rule.
Why is a `<link rel="stylesheet">` in the `<head>` usually preferred over an `@import` inside a stylesheet?
The browser finds a `<link>` while it parses the HTML and can start downloading right away. An `@import` is only discovered after the importing stylesheet has downloaded and been parsed, so the downloads happen one after another instead of in parallel.
You never wrote any CSS for a page, yet headings are bold and links are blue and underlined. Where did those styles come from?
From the user-agent stylesheet, the default CSS each browser ships with. It sits at the lowest priority, so any author stylesheet you write overrides it.
Which of the three places for CSS is hardest to override later, and why does that make it a poor default?
The `style` attribute, because styles written on the element beat selectors from any stylesheet short of `!important`. You cannot override it from a stylesheet with an ordinary rule, which makes it a poor default for anything you want to change in one place.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.