Skip to content

Function: injectStyle() ​

ts
function injectStyle(
   id, 
   css, 
   options?): () => void;

Inject or update a <style> element in <head> identified by id. Calling again with the same id replaces the CSS instead of creating a new element.

Parameters ​

ParameterTypeDescription
idstringThe id attribute of the <style> element.
cssstringThe CSS string to inject.
options?InjectStyleOptionsOptional configuration for the <style> element.

Returns ​

() => void

Examples ​

ts
injectStyle('theme-root', ':root { --color-primary: #007aff; }')
// update later — same element, new content
injectStyle('theme-root', ':root { --color-primary: #ff3b30; }')
ts
const dispose = injectStyle('print-style', 'body { color: black; }', { media: 'print' })
dispose() // removes the style element

Since ​

1.3.0

Released under the MIT License.