Skip to main content

Overview

Lightning Web Components typically render in Shadow DOM, which provides style encapsulation and DOM isolation. However, you can opt into Light DOM rendering for specific use cases where Shadow DOM restrictions are too limiting.
Light DOM is available in API version 58.0 and later. Use it when you need direct DOM access from parent components or better integration with global styles.

Shadow DOM vs Light DOM

Shadow DOM (Default)

  • Encapsulated styles: CSS defined in the component only affects that component
  • Isolated DOM: Parent components cannot query child component internals
  • Standard behavior: Default for all Lightning Web Components

Light DOM

  • Shared styles: Component inherits styles from parent and global CSS
  • Accessible DOM: Parent components can query child component elements
  • Legacy compatibility: Better integration with older Salesforce features

Enabling Light DOM

Set the renderMode static property to 'light':
Source: force-app/main/default/lwc/lightDomQueryChild/lightDomQueryChild.js:3-11 Update the template with the lwc:render-mode directive:
Source: force-app/main/default/lwc/lightDomQueryChild/lightDomQueryChild.html:1-11

Querying Light DOM Elements

From Within the Component

Use this.querySelector() instead of this.template.querySelector():

From Parent Components

Parent components can directly access elements inside Light DOM children:
Source: force-app/main/default/lwc/lightDomQuery/lightDomQuery.js:4-8
In Shadow DOM, parent components cannot query elements inside child components. Light DOM removes this restriction.

Key Differences

DOM Queries

Shadow DOM:
Light DOM:

Event Handling

Shadow DOM:
  • Events bubble through shadow boundaries
  • event.target may be retargeted
Light DOM:
  • Standard DOM event propagation
  • event.target is the actual element

Styling

Shadow DOM:
Light DOM:

Use Cases for Light DOM

1. Third-Party Library Integration

Some libraries need direct DOM access:

2. Global Style Application

Apply Salesforce Lightning Design System (SLDS) utilities:

3. Accessibility Testing Tools

Some accessibility testing tools work better with Light DOM:

4. Parent-Child DOM Manipulation

When parent needs direct control over child elements:

Limitations

Light DOM has important limitations compared to Shadow DOM. Consider these carefully before using Light DOM.
  • No style encapsulation: Component styles can conflict with parent/global styles
  • No slot support: <slot> elements don’t work in Light DOM
  • Security considerations: More vulnerable to XSS if not careful with DOM manipulation
  • Performance: Shadow DOM can have better performance in some scenarios

Migration Considerations

Converting from Shadow DOM to Light DOM

  1. Add static renderMode = 'light'; to the class
  2. Add lwc:render-mode="light" to the template
  3. Replace this.template.querySelector() with this.querySelector()
  4. Replace this.template.querySelectorAll() with this.querySelectorAll()
  5. Remove <slot> usage (not supported in Light DOM)
  6. Review CSS for potential conflicts with global styles

Example

Before (Shadow DOM):
After (Light DOM):

Best Practices

  • Default to Shadow DOM: Only use Light DOM when you have a specific need
  • Namespace CSS classes: Prevent style conflicts by using unique class names
  • Document the choice: Comment why Light DOM is needed for future maintainers
  • Test thoroughly: Ensure no style conflicts with parent components
  • Validate accessibility: Light DOM can affect screen reader behavior

When NOT to Use Light DOM

  • Reusable components: Shadow DOM provides better encapsulation
  • Style isolation needed: Components that should look the same everywhere
  • Using slots: Slots only work in Shadow DOM
  • Security-sensitive: Shadow DOM provides an extra layer of isolation