Skip to content

Styling

codingncaffeine edited this page Sep 3, 2026 · 19 revisions

Styling

Everything between the stylesheet's bytes and a computed style for each element.

Syntax

The CSS tokenizer and parser follow css-syntax-3 (the current draft, nesting-era block contents), with a unicode-range gate. Both are fuzzed.

Selectors

Every attribute matcher, the four combinators, :not(), :is(), :where(), the structural pseudo-classes with the full an+b grammar, and specificity. ::before, ::after and ::first-letter are lifted out of the last compound so the rest of the selector matches the originating element and the cascade files the rule under the box it addresses; a pseudo-element anywhere but the end of a selector invalidates it, as the specification says, and one that is not written stays in the compound and matches nothing.

Matching is indexed: rules are bucketed by the rightmost compound's id, class or type, and an ancestor filter (a counting Bloom filter over the compounds to the left of descendant and child combinators) rejects most candidates before they are tried. Wikipedia's 900-rule cascade matches in a few milliseconds.

The cascade

  • Origins and importance: the UA sheet, author sheets, style attributes; !important; specificity; order.
  • Inheritance, and the CSS-wide keywords inherit, initial and unset (and revert, read as unset) on every property the engine knows, on shorthands, and on all.
  • font-size is settled first, in cascade order, so em and the font-relative units in the same element's other declarations resolve against it.
  • A border color that was never written is currentColor and inherits as that keyword — the child's own color — not as the parent's resolved color.

Units and values

  • Lengths: px, em, rem, ex, ch, pt, pc, in, cm, mm, Q, vw, vh, vmin, vmax, and percentages. ex is the x-height of the first available face and ch the advance of its "0", read from the face itself — half the font size only where a face does not say. The family, weight and slant are settled before any length is, so those two measure the face the element ends up with.
  • calc(), min(), max() and clamp() over lengths and percentages. A mixed sum such as calc(100% - 20px) stays pixels plus a percentage until layout resolves it against its containing block.
  • Colors: hex (3, 4, 6 and 8 digits), rgb()/rgba() and hsl()/hsla() in the modern and the legacy syntax, transparent, currentcolor, and all 148 CSS named colors.

Custom properties

--name declarations cascade and inherit like any property; var() with a fallback is substituted anywhere in a value, inside functions and blocks included. Cycles and references with no value and no fallback drop the declaration, as the specification says.

Stylesheets

  • <style> sheets (a <![CDATA[ wrapper is unwrapped), <link rel="stylesheet">, and @import ahead of its sheet, bounded in depth and count.
  • The charset rules: a byte order mark, the HTTP Content-Type charset, @charset, then UTF-8.
  • External sheets and images load through the page's own session, with the page as first party, so third-party cookies stay out.
  • Media queries (MQ4): types, not/only/and/or, width and height in the plain and the range syntax, orientation, aspect ratio, resolution, preference and pointer features — evaluated against the real viewport, with @media blocks, <link media> and @import conditions all gated, and the shell restyling on resize.

Web fonts

@font-face with TrueType sources (format("truetype"), format("opentype"), or an unlabeled source that is not WOFF, WOFF2, EOT or SVG): the file is fetched through the page's session, parsed by the TrueType reader, and answers ahead of the machine's fonts for its family, weight and slant. WOFF and WOFF2 are not written yet.

The property set

Box model (display with the two-value syntax and every table value, width, height, min-*, max-*, margins, padding, borders as shorthands and every side's width, style and color longhand, border-radius as the shorthand — one to four radii on each side of the slash — and the four corner longhands), float, clear, overflow (per axis, clip kept apart from the values that scroll), positioning (position, the four offsets, inset, z-index), visibility, opacity, transform/translate (translations).

Flexbox (flex-*, justify-content, align-*, gap, order) and grid (grid-template-* with repeat() and named lines, grid-template-areas, the grid/grid-template shorthands, grid-auto-*, grid-row/grid-column/grid-area and their longhands, justify-items/justify-self, place-*).

Tables (border-collapse, border-spacing, caption-side, empty-cells, table-layout), text (color, font-* and the font shorthand, line-height, vertical-align, direction (and the dir attribute), text-align (start, end, match-parent and justify-all included), text-align-last, text-justify, text-indent, letter-spacing, word-spacing, white-space, text-transform, text-decoration), the list properties (list-style-type with the numbering systems — decimal, decimal-leading-zero, roman, alphabetic, greek, armenian, georgian , list-style-position and the list-style shorthand), the counter properties (counter-reset, counter-increment, counter-set), content and quotes.

Backgrounds: background-color, background-image with url() and the gradient functions, background-repeat (repeat, space, round, no-repeat, per axis), background-position, background-size, background-origin, background-clip, and the background shorthand with its layers.

Values: lengths and percentages, calc()/min()/max()/clamp(), viewport units, custom properties with var() and its fallback, and the CSS-wide keywords inherit, initial, unset and all.

Generated content

::before and ::after boxes with content strings, attr() (with a fallback), open-quote/close-quote from the quotes property nested by depth in tree order, and counter()/counters(); content: normal/none generate nothing and url() leaves the declaration unapplied. The boxes lay out as inline text, as blocks of their own (the clearfix idiom included), as floats, and as flex items.

Counters follow CSS 2.1 nested scope: a counter-reset makes an instance covering its own box, that box's descendants and its following siblings, so a counter nests itself when a list resets it again; counter-increment and counter-set reach the innermost instance in scope, and a counter nothing has reset reads as zero. A box that is never generated — display: none, and everything inside it — does no counter work, while a visibility: hidden box still counts. counter() takes the innermost value and counters() joins every value in scope with a separator; both take a counter style as their last argument.

The first letter

::first-letter dresses the first letter of a block container's first line, in either spelling — the one colon CSS 2.1 wrote, or the two of the current draft. The run it selects is the letter, the marks that hang off it, and the punctuation on either side: the Unicode Ps, Pe, Pi, Pf and Po categories, read from tables the Unicode data generates. A word of nothing but punctuation leads into the letter the next word holds and comes along with it. A space the line kept in front of the letter — a no-break, en, em or thin space — is not selected, and nothing is dressed.

The letter need not be written in the block itself: it is looked for through the inline boxes on the line, and when a block holds boxes rather than text, its first line lives inside the first of them and the style is handed down that chain. As the fictional start tag goes inside those inline boxes, an inherited property the ::first-letter rules did not set comes from the box the letter was written in rather than from the block. A flex or grid container is passed by — it holds items, not lines.

Colour, font, weight and size apply; a background, border or padding on the letter alone waits on inline box painting (Not written yet).

The UA sheet

A small user-agent stylesheet gives the HTML elements their defaults: block and list-item displays, heading sizes and margins, pre and code in monospace, anchors with an href in blue and underlined, sub and sup raised and lowered at a smaller size, form controls at 13.333 px, and the table displays with cells middle-aligned.

Clone this wiki locally