Skip to content
tezvyn:
Menu

Shadow DOM encapsulation vs CSS Modules

Source: interviewHardHow cards are made

Summary

Understanding real browser encapsulation versus build-time name scoping.

Key points

Shadow DOM creates a separate tree blocking selectors both ways, CSS Modules only rename classes, and global styles need explicit bridges.

What's really being asked

The interviewer checks whether you understand the difference between a real DOM and CSS boundary enforced by the browser and a convention that only rewrites names at build time, plus the practical cost of true encapsulation.

The full answer

Shadow DOM attaches a shadow root to a host element, creating a separate subtree. Styles defined inside that root cannot leak out, and selectors written outside cannot reach in. The boundary is enforced at runtime by the browser. A few things still cross it deliberately: inherited CSS properties such as color and font, CSS custom properties, and explicit hooks like the ::part pseudo-element and ::slotted for projected content. By contrast, CSS Modules is a build step that hashes class names to make them unique, so collisions are avoided, but there is no real boundary; global stylesheets, resets, and descendant selectors still apply because the elements live in the normal light DOM.

The mistakes people make

Claiming CSS Modules gives the same isolation as Shadow DOM is wrong; it only prevents name clashes. Forgetting that Shadow DOM blocks your own global design tokens is a common trap. Assuming a global reset or utility framework will style shadow content also fails.

What usually comes next

How do design tokens reach into shadow trees? Via custom properties, which pierce the boundary. How do you let consumers theme parts? Expose ::part. How do slots interact with styling? How does form participation work for custom elements?

A concrete example

A web component date picker uses Shadow DOM, so the app Tailwind utilities and global reset have no effect inside it, which keeps it consistent everywhere but means you cannot restyle its internals with normal classes. To theme it you define custom properties like a primary color variable that the component reads, and you expose ::part(button) so consumers can adjust the trigger without breaking encapsulation.

Interview question

Which mechanism can intentionally cross the Shadow DOM boundary to let an app theme an encapsulated web component?

  • a.A global stylesheet using descendant class selectors
  • b.A global CSS reset applied at the document root
  • c.A CSS Modules hashed class name from the host app
  • d.CSS custom properties read inside the shadow treeCorrect
Why?

Custom properties (and ::part) deliberately pierce the boundary, so a component can read app-provided tokens. Global selectors, resets, and CSS Modules class names stay in the light DOM and cannot reach shadow content.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles