Study guides · Intermediate

Choose CSS values through matching and the cascade

Separate selector matching, specificity, source order, and inheritance while deriving a computed style from competing declarations.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Receive DOM and CSS as separate inputs
  2. Hold origin, importance, and layer conditions constant
  3. Compare specificity as ordered components
  4. Use source order only after earlier comparisons tie
  5. Inheritance does not compete using the parent selector specificity
  6. Pass computed style information to layout
  7. Exercise: remove child rules and reveal inheritance
  8. Ask Logeia about this articleCheck your understanding

Receive DOM and CSS as separate inputs

Use the DOM element <p id="intro" class="note">A</p> and rules for p, .note, and #intro. CSS declarations can provide values even though no color appears directly in the HTML. First determine which selectors match this element. An unmatched declaration cannot choose its color, however specific it looks. Display all rules, matched candidates, and the final value separately. Keeping style calculation after tree construction permits comparison with another stylesheet or a later rule change.

A fixed demo document with h1, p#intro, and p.note makes candidate sets easy to inspect. A type selector h1 matches the heading; p matches both paragraphs; #intro and .note match their respective paragraphs. Identical color:red declarations can therefore apply to different element sets. Display matching first, then choose a winner separately for every element and property. Do not replace an entire style object merely because one rule wins for color. A declaration that changes color should not erase width or margin supplied elsewhere. Test the property-wise merge as well as final color. That reveals an implementation error a single-property example would never expose.

Hold origin, importance, and layer conditions constant

The full cascade considers origins, importance, layers, and other axes. The initial model fixes author origin, normal declarations, and no layers. Within those conditions it compares simple type, class, and ID selectors. It does not force important declarations, inline styles, animations, or complex selectors into an incomplete specificity calculation. Report those as unsupported and add them through later lessons. The shortcut that IDs are stronger is useful only when its omitted conditions are stated.

Compare specificity as ordered components

For simple selectors, compare #intro as (1,0,0), .note as (0,1,0), and p as (0,0,1). Compare components from left to right rather than converting them into an ordinary decimal score. Under the fixed cascade conditions, a matching #intro color declaration outranks a matching .note declaration. Specificity does not evaluate whether a value is desirable; it only orders candidates. Keep losing candidates visible and show which component determined the comparison.

Separate selector parsing from matching a parsed selector to a DOM element. If the first version supports only a type name, one ID, or one class, compound p.note and descendant div p are additional features. Do not guess at them. Even a simple class selector needs token membership: class="note extra" matches .note, while notebook alone does not. Substring search would incorrectly accept the latter. Tests should cover both cases and keep unsupported compound syntax distinguishable from a supported selector that simply matches no element. A no-match result is a valid outcome; it must not be used to conceal parser limitations.

SelectorSpecificityMatches?
p0,0,1Yes
.note0,1,0Yes
#intro1,0,0Yes
#other1,0,0No

Use source order only after earlier comparisons tie

If .note { color: red; } is followed by .note { color: green; }, the later green wins in this model because specificity ties. An earlier matching #intro { color: blue; } still outranks a later .note rule. The phrase last rule wins omits the requirement that earlier cascade comparisons tie. Offer separate controls for moving rules and changing selectors. If moving a rule has no effect, inspect whether a prior comparison already decided the winner.

Inheritance does not compete using the parent selector specificity

If a div has color green and its p child has no color declaration, color is inherited. If p { color: red; } directly matches the child, the child becomes red under the fixed conditions. A highly specific selector on the parent does not carry its specificity into a competition with the child rule. Properties such as width are normally not inherited. Copying every parent value into the child would incorrectly share box dimensions. Keep inheritance and initial-value rules property-specific.

Use inheritance rules for properties such as color and font-size without blindly applying the same rule to margin and width. Registering inherited values as powerful child candidates can incorrectly defeat direct child declarations. First resolve declarations for the child; then use inheritance or initial-value rules where appropriate. The teaching view focuses on fixed-pixel font-size, color, margin, width, and display. Diagnose a negative width or an unsupported unit before invalid geometry reaches layout. Value validation and declaration priority are separate checks. A syntactically accepted selector does not make every property value valid, and the most specific invalid value is not a reason to manufacture a numeric size.

Pass computed style information to layout

After choosing declarations, apply relevant inheritance, initial values, and supported value interpretation to produce style data for later stages. Begin with display, color, background, and fixed pixel widths. A declared string is not already a final coordinate. Values such as auto or percentages may require layout context. Keep candidate selection separate from box geometry in the demo. Preserve diagnostics for unsupported or invalid values rather than inventing a coordinate. More properties can be added once their dependencies are explicit.

Exercise: remove child rules and reveal inheritance

Let parent #box have color green, and give its p child class note. Place p { color: red; } before .note { color: blue; }. The child is blue. Remove the class rule and it becomes red; remove the p color too and it inherits green. Add another .note { color: orange; } last and orange wins. For each case, list matched candidates, the winner, and whether inheritance was used. Explain which decision stage changed rather than merely naming the final color.

Add a no-match challenge: apply #other { color:red } and p { color:blue } to p#intro. The answer is blue; #other is excluded before specificity comparison. Next give a paragraph width:120px through p and color:green through .note. Both values should survive property-wise selection. Finally assign display:none and compare with the next layout lesson: style output changes while the DOM node count remains unchanged. Explain results through selector parsing, candidate extraction, value selection, inheritance, and transfer to layout. That sequence is a practical debugging method: inspect the first stage whose output differs, rather than repeatedly moving CSS rules until a picture happens to look right.

Try the interactive model

From CSS rules to painted boxes

Loading demo checkpoint…

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 20

Choose a learning path