0Pricing
CSS Academy · Lesson

Updating Variables with JavaScript

Dynamically update CSS custom properties from JavaScript using style.setProperty().

Why Update CSS Variables from JS?

CSS custom properties are live in the browser. Changing them via JavaScript immediately updates all elements using that property — enabling dynamic themes, real-time sliders, and state-driven styles without toggling many classes.

Getting a Custom Property Value

Read a custom property value using getComputedStyle():

const root = document.documentElement;
const value = getComputedStyle(root)
  .getPropertyValue('--primary-color').trim();
console.log(value); // '#3b82f6'

All lessons in this course

  1. Declaring and Using Custom Properties
  2. Fallback Values and var()
  3. Scoping Variables to Components
  4. Updating Variables with JavaScript
← Back to CSS Academy