liveBook · Manning

Shadow DOM

As explained in CSS in Depth, Second Edition

Shadow DOM is a web platform mechanism that creates an isolated DOM subtree with encapsulated structure and styles.

Shadow DOM creates a strongly isolated DOM subtree for web components. The outer page cannot style its contents directly; the component must provide its own styles through HTML and JavaScript. section 9.3.2

Choose Shadow DOM when a component must remain visually self-contained:

  • Package a reusable widget whose appearance must remain identical across multiple websites. section 9.3.2
  • Prefer Shadow DOM over CSS scope when strong separation from the page cascade matters; use scope when the page should influence module styling. section 9.3.2

Embedded social-media widget

A widget embedded in a blog can use Shadow DOM to keep its structure and styles independent from the host page, preventing the page stylesheet from styling it directly. section 9.3.2

How CSS in Depth, Second Edition covers it

How deep it goes
Contrasts isolation and cascade behavior with CSS scope but stops short of implementation because JavaScript-driven details are outside treatment.
Examples
Uses an embedded social-media widget as an example of a reusable component requiring identical appearance across websites.
What you can do after
Evaluate whether strong component isolation or cooperative page-wide styling better fits a widget or module.
How it connects
Connects web components, JavaScript, CSS scope, styles, inheritance, and the cascade.

Also explained in

Compare how 2 books explain Shadow DOM →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.