Styling hooks (CSS custom properties) allow you to customize the appearance of Lightning base components and create themeable components without breaking encapsulation.
What are Styling Hooks?
Styling hooks are CSS custom properties (CSS variables) that components expose to allow external customization. They follow the pattern:
Lightning base components expose styling hooks prefixed with:
--slds-c-* - Component-specific styling hooks
--slds-g-* - Global design tokens
Basic Example
Here’s how to customize a button’s appearance:
Complete Styling Example
The stylingHooks component demonstrates comprehensive customization:
Template: lwc/stylingHooks/stylingHooks.html:1
Styles: lwc/stylingHooks/stylingHooks.css:1
Scoped Styling Hooks
You can target specific instances of base components:
Example: lwc/stylingHooks/stylingHooks.css:54
In the template, apply the class to customize a specific instance:
Use CSS classes to create variant styles for base components, allowing different instances to have different appearances.
Custom CSS Properties
Define your own CSS custom properties for reuse:
Then reference them in other properties:
Define a color palette using custom properties at the :host level, then reference them throughout your component. This makes theme changes easier.
Common Styling Hooks
Card Component
Toggle Component
Creating Themeable Components
Expose styling hooks in your own components:
Parent components can then customize:
Always provide fallback values in var() to ensure your component works even when custom properties aren’t set.
Best Practices
- Use
:host to define styling hooks at the component level
- Create a consistent naming convention for custom properties
- Provide sensible default values in
var() functions
- Document available styling hooks for component consumers
- Group related hooks together in your CSS
- Use design tokens as values for styling hooks when possible
- Test your components with different hook values