Skip to content

Function: setCssVariable()

ts
function setCssVariable(
   key, 
   val, 
   ele?): void;

Sets a CSS variable on the specified element or the root element.

Parameters

ParameterTypeDescription
keystringThe name of the CSS variable to set.
valstringThe value to set the CSS variable to.
ele?HTMLElementThe element to set the CSS variable on. If not specified, defaults to the root element.

Returns

void

Example

ts
// Set a CSS variable on the root element
setCssVariable('--main-bg-color', '#ffffff');

// Set a CSS variable on a specific element
const element = document.querySelector('.my-element');
setCssVariable('--main-bg-color', '#ffffff', element)

Since

1.0.0

Released under the MIT License.