Shadow DOM
Learn how to use Shadow DOM in Aurelia components for style encapsulation and native web component features.
Quick decision guide
Enabling Shadow DOM
Using the @useShadowDOM Decorator
import { customElement, useShadowDOM } from 'aurelia';
@customElement('my-card')
@useShadowDOM()
export class MyCard {
message = 'Hello from Shadow DOM';
}Configuring Shadow DOM Mode
Using the Configuration Object
Styling Shadow DOM Components
Troubleshooting checklist
Component-Local Styles
Using Constructable Stylesheets
Global Shared Styles
Shadow DOM CSS Selectors
The :host Selector
:host SelectorThe :host-context() Selector
:host-context() SelectorThe ::slotted() Selector
::slotted() SelectorThe ::part() Selector
::part() SelectorStyling from Outside: CSS Custom Properties
Using CSS Modules with Shadow DOM
Shadow DOM and Slots
Basic Slot Usage
Named Slots
Fallback Content
Listening to Slot Changes
Constraints and Limitations
Cannot Combine with @containerless
Native Slots Require Shadow DOM
Choosing Between Shadow DOM and Light DOM
Use Shadow DOM When:
Use Light DOM (no Shadow DOM) When:
Practical Examples
Themed Button Component
Card with Multiple Slots
Component with Dynamic Styles
Best Practices
1. Use CSS Custom Properties for Theming
2. Provide Fallback Content for Slots
3. Namespace Your CSS Variables
4. Consider Performance with Constructable Stylesheets
5. Use Open Mode Unless You Have a Reason Not To
6. Document Your CSS Custom Properties
7. Convention-Based CSS Does Not Auto-Inject into Shadow DOM
Additional Resources
Last updated
Was this helpful?