The Web FrontendDeeper

Kayavak / The Web Frontend / FE·14 CSS / Deeper

Deeper · The Web Frontend

CSS

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

By 1994 page authors wanted control over fonts, colours and layout, and browsers were adding presentational HTML to give it to them, which mixed appearance into every document. Håkon Wium Lie proposed Cascading Style Sheets at CERN in October 1994, and with Bert Bos turned it into CSS1, a W3C Recommendation in December 1996. The word cascading was the key idea: several parties have a say in how a page looks (the browser's defaults, the author, and the reader, who might need larger text), and the cascade is the rule for combining their wishes.

Layout was the long struggle. For a decade, pages were laid out with tables and then with floats, techniques built for other purposes. Flexbox (widely usable from about 2012–2015) gave one-dimensional layout, and CSS Grid, which shipped in all major browsers in 2017, gave real two-dimensional layout. Modern additions (custom properties, container queries, :has(), cascade layers, nesting) have made CSS a language people architect rather than patch.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

CSS is a negotiation, not a sequence of commands. Many rules may apply to one element; the cascade decides which wins, then layout turns the winners into boxes.

Question Answer
What is it? Rules (selector + declarations) that assign values to properties of elements
Problem it solves Styling documents separately from their content, for many devices, honouring several parties' wishes
Parts Selectors, the cascade (origin, layers, specificity, order), inheritance, values and units, layout modes
How it works For each element and property: collect matching declarations, pick a winner by the cascade, compute, then lay out boxes
Assumptions Style is declarative: you describe the result, the browser works out how
When they change Treat CSS as imperative ("this rule then that") and you fight specificity with !important and ever-longer selectors
Where it fails Large codebases where any rule can affect any element: hence layers, scoping, naming conventions, CSS-in-JS
Related ideas The rendering pipeline (ch18); design systems (ch21); accessibility and user styles (ch20)

What if?

What if CSS had no cascade, only "last rule wins"?

Reason it through, then open

Every stylesheet would depend on load order. A reader's high-contrast style would be overridden by whichever site stylesheet loaded later, and a component library couldn't set defaults that applications reliably override. The cascade (origins, importance, layers, specificity) exists so that who wrote a rule, and how specific it is, matters as well as when it appeared.

What if lengths could only be in pixels?

Reason it through, then open

Pages couldn't adapt to readers who set a larger default font, to narrow phones or to zoom levels without breaking. Relative units (em, rem, %, viewport and container units) and intrinsic sizing (min-content, fr) let layout follow content and context. Pixels in CSS aren't even device pixels: one CSS pixel is defined by a reference viewing angle, so it maps to several device pixels on high-density screens.

Where the boundary is

Aspect Specificity Cascade layers
Decided by The selector's shape (ids, classes, types) The order layers are declared
Controlled by Whoever writes each selector The architecture, in one place
Problem it causes Wars of ever-more-specific selectors None of its own: it's the fix
Which wins first Layers are compared before specificity: a simple selector in a later layer beats a complex one in an earlier layer

How sure are we?

Claim How it's known Where from
Lie proposed CSS in 1994; CSS1 a Recommendation in 1996 Historical W3C records
Layers are compared before specificity Convention CSS Cascading and Inheritance Level 5
Grid shipped in all major browsers in 2017 Historical Browser release notes (spring 2017)
One CSS pixel = 1/96 inch Simplification It's defined via a reference angle; on screens it's not a physical inch

Still open

How to structure CSS at scale is still debated: utility classes (Tailwind), scoped component styles, CSS-in-JS (losing favour because of runtime cost), and now native scoping and layers. Which wins depends on teams as much as on technology.

What would change your mind? If native @scope and layers proved enough for large teams, most CSS methodologies would become unnecessary. It's early.

Recall

What problem does the cascade solve?

Combining styles from several parties (browser, author, reader) by who wrote them and how specific they are, not just order.

Are cascade layers or specificity compared first?

Layers first; specificity only decides within the same layer.

Why are relative units important?

They let layout follow the reader's font size, zoom, screen and container instead of breaking.

When did CSS Grid become usable in all major browsers?

2017.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.