Colore JS

Search documentation

Find a function or jump to a section.
v1.4.4

setCssVariableValue

Legacy documentation for Colore JS 1.4.4.View v2 docs

The setCssVariableValue function sets the value of a specified CSS variable on a given element or the document.

Syntax

setCssVariableValue(element: HTMLElement | null, variableName: string, value: string): void;

Parameters

  • element (HTMLElement | null): The element on which to set the CSS variable value. If null, the CSS variable will be set on the document.
  • variableName (string): The name of the CSS variable (e.g., --my-variable).
  • value (string): The value to set for the CSS variable.

Throws

  • Error: Throws an error if the CSS variable name does not start with "--".

Example

import { setCssVariableValue } from 'colore-js';

const element = document.querySelector('.my-element');
setCssVariableValue(element, '--my-variable', 'blue');

// Passing null sets the variable on the document root.
setCssVariableValue(null, '--global-variable', 'red');

Usage

The setCssVariableValue function is useful for dynamically updating CSS variables defined in your stylesheets. This can be particularly helpful when you need to change these values within your JavaScript code to affect styling dynamically.