liveBook · Manning

Shady CSS

As explained in Web Components in Action

ShadyCSS is a CSS compatibility technique or polyfill library for approximating Shadow DOM style scoping where native CSS encapsulation is unavailable.

ShadyCSS is a polyfill for approximating Shadow DOM CSS scoping when native encapsulation is unavailable. It rewrites template markup and CSS with unique selectors, helping prevent component styles from leaking into the surrounding application. Unlike many drop-in polyfills, it requires developers to structure markup and styles in templates and accommodate its workflow. section 10.3 section 12.1

Using ShadyCSS involves several compatibility decisions:

  • Place component markup and CSS in a <template> so the polyfill can rewrite them. section 12.1
  • Avoid relying on :host when native Shadow DOM is absent; select the component element instead. section 12.1
  • Prefer classes over IDs for internal elements, because document-wide ID references can collide without Shadow DOM. section 12.1
  • Treat the polyfill as an option rather than a complete substitute: it rewrites selectors but does not provide native protection from outside CSS. section 12.1

Switching selectors for a slider

A component can render CSS according to its environment. With Shadow DOM, use :host; without it, use the component name: :host { ... } becomes wcia-slider { ... }, and :host .thumb becomes wcia-slider .thumb. A render function can choose the selector form while using the same HTML template.

How Web Components in Action covers it

How deep it goes
Explains template-based selector rewriting and manual selector alternatives, but stops short of native CSS encapsulation.
Examples
Uses slider and color-picker components to demonstrate conditional selectors, class-based internals, and modal host styling.
What it assumes
Assumes familiarity with Web Components, Shadow DOM selectors, templates, and component lifecycle rendering.
What you can do after
Operate compatibility styling by templating component CSS, switching selectors, and avoiding global ID and selector collisions.
How it connects
Connects Shadow DOM styling with Web Components, ShadyDOM, HTML templates, Polymer, LitElement, and lit-html.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Components in Action. The section links point to the parts of the book it draws on.