Style resolution and invalidation
Definition and purpose
Style resolution selects applicable declarations, resolves the cascade and produces style values. A DOM or stylesheet change can invalidate previously resolved styles, causing the engine to reconsider affected elements.
Ancestor class mutation
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } .panel #a { color: blue; } .panel.active #a { color: green; } </style> </head> <body> <div class="panel" id="p"> <p id="a">Status</p> </div> <button id="toggle" type="button" onclick="document.querySelector('#p').classList.toggle('active')">Toggle</button> </body></html>Click Toggle; green indicates the new matching rule.
Syntax and mental model
Changing an ancestor class can affect descendants matched by a selector, or inherited values. A relational selector can also depend on descendants. Engines track dependencies and schedule work differently; a selector matching a large region does not imply every node is always recalculated. Observe state before and after a mutation, then record a performance trace in the browser tooling to investigate actual recalculation scope.
Inherited mutation
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } .panel { color: navy; } .panel.active { color: green; } </style> </head> <body> <div class="panel" id="p"> <p id="a">Status</p> </div> <button id="toggle" type="button" onclick="document.querySelector('#p').classList.toggle('active')">Toggle</button> </body></html>The paragraph inherits the panel color; toggling changes inherited style.
Common mistakes
Do not infer exact invalidation costs from selector length alone. Measure under representative document sizes and changes.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Resolved style and geometry APIs. Next lesson: Layout, paint, raster and compositing.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Style resolution and invalidation — knowledge check
Five questions on style resolution and invalidation.
What is style invalidation?
Can an ancestor class change affect descendants?
Which can depend on descendant state?
How should recalculation cost be investigated?
What can style changes trigger?