Shadow DOM and custom states
Definition and purpose
Shadow DOM separates a component tree from the surrounding document selector scope. Ordinary page selectors do not target internal shadow nodes, although inherited properties can still cross the host boundary. Components can deliberately expose styling hooks.
Exposed part versus private node
<!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; } demo-card::part(title) { color: navy; } #secret { color: red; } </style> </head> <body> <demo-card id="host"> <span slot="label" id="label">Label</span> </demo-card> <script> customElements.define("demo-card", class extends HTMLElement { constructor(){super();this.attachInternals().states.add("ready");this.attachShadow({mode:"open"}).innerHTML=``;} }); </script> </body></html>The public part is navy; the internal private node stays black.
Syntax and mental model
Inside shadow CSS, :host matches the host and ::slotted(span) targets directly assigned span elements, not arbitrary descendants. Outside, demo-card::part(title) targets an internal node with part="title". :state(ready) matches a custom element whose ElementInternals.states contains ready. The supplied JavaScript defines a local component and state; learners need only edit the external CSS.
Custom state and slot
<!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; } demo-card:state(ready) { background: white; } </style> </head> <body> <demo-card id="host"> <span slot="label" id="label">Label</span> </demo-card> <script> customElements.define("demo-card", class extends HTMLElement { constructor(){super();this.attachInternals().states.add("ready");this.attachShadow({mode:"open"}).innerHTML=``;} }); </script> </body></html>The supplied custom state and shadow rules style the host and the assigned label.
Common mistakes
Do not treat an open shadow root as permission for page CSS to pierce it; open is a JavaScript access mode. Expose a small documented set of parts instead of relying on internal IDs.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Filtered nth-child and selector parsing. Next lesson: Highlights and specialized states.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Shadow DOM and custom states — knowledge check
Five questions on shadow dom and custom states.
What enables external styling of an internal part?
Where is :host used to match the host?
What does ::slotted(span) target?
What supplies a custom state?
Can inherited color cross a shadow host boundary?