Overview
Converts a colors object to CSS custom properties and injects them into the document by creating or updating a<style> tag. This is the recommended method for applying dynamic colors to your application’s UI.
Signature
Parameters
The colors object containing color key-value pairs. Typically from
DynamicColor.colors() response (e.g., colors.light, colors.dark, or colors.dayNight).The prefix to use for CSS variable names. The method will append a hyphen and the kebab-cased color key name.
Returns
Returns
true after successfully inserting or updating the style tag in the DOM.Behavior
- First call: Creates a new
<style>element with classdynamicColorCssVarsColorsand appends it to<head> - Subsequent calls: Updates the existing style element’s content with new CSS variables
- CSS variables are wrapped in
:root { ... }selector for global availability
Example
With Tinted Surface Colors
With Custom Prefix
Implementation
The method creates a:root CSS rule with all color variables:
Complete Usage Pattern
The style tag uses class
dynamicColorCssVarsColors for tracking. If you need to apply both colors and palette, use this method alongside paletteToDom() - they use different class names and won’t conflict.Use Cases
- Apply Material Design 3 dynamic colors to your entire app
- Update theme colors when user switches between light/dark mode
- Respond to system wallpaper color changes on Android 12+
- Create adaptive UI that follows platform design guidelines
See Also
colorsToCssVars()- Generate CSS string without applying to DOMpaletteToDom()- Apply palette colors to DOMDynamicColor.tintSurfaceColors()- Add elevation tints to surface colorsDynamicColor.colors()- Get the colors object