styleMap directive sets inline CSS properties on an element using a StyleInfo object. Property names can be camelCase JavaScript identifiers or dash-case CSS property names. Setting a property’s value to null or undefined removes it.
Import
Signature
Types
Parameters
An object mapping CSS property names to their values. Keys with
null or undefined values are removed from the element’s inline style. Accepts camelCase JavaScript names (e.g., backgroundColor) or dash-case CSS names (e.g., background-color) and CSS custom properties (e.g., --my-color).Return type
DirectiveResult — an opaque value consumed by lit-html’s template engine.
Property name handling
| Key format | Example | Resolved as |
|---|---|---|
| camelCase | backgroundColor | background-color via property assignment |
| dash-case | background-color | background-color via setProperty() |
| CSS custom property | --my-size | --my-size via setProperty() |
| vendor-prefixed camelCase | webkitAppearance | -webkit-appearance |
!important, append !important to the value string:
Constraints
Example
display to null when _size is 0 removes the property from the inline style entirely.
When to use
- You need to dynamically set multiple CSS properties based on component state or properties.
- You want to manage CSS custom properties reactively.
- You prefer a declarative object over building style strings manually.
When not to use
- Outside of a
styleattribute —styleMapwill throw if placed elsewhere. - When you need to apply class-based styles — use
classMapinstead.