


How Can I Dynamically Change CSS Variables with JavaScript for Theme Customization?
Dynamic Theme Customization with CSS Variables and JavaScript
Enhancing the user experience through customization is a common goal for web developers. One effective approach involves leveraging CSS variables and JavaScript to effortlessly alter a website's theme.
However, when attempting to implement such a system, one may encounter challenges in modifying the variables defined in the :root scope of the CSS using JavaScript. To address this issue, let's explore a solution that effectively resolves it.
Customizing CSS Variables with JavaScript
The key to modifying CSS variables programmatically lies in utilizing the setProperty() method of the document.documentElement style object. By passing in the variable name and the desired value, you can dynamically alter the variable's value.
document.documentElement.style.setProperty('--your-variable', '#YOURCOLOR');
To illustrate, consider the following example:
function setPrimaryColor(color) { document.documentElement.style.setProperty('--primary-color', color); }
In this function, the primary-color CSS variable is set to the provided color argument. This allows you to seamlessly update the color scheme of your website by changing a single CSS variable.
Implementing a Theme Switch
With the ability to alter CSS variables using JavaScript, you can now implement a robust theme switch system. Consider the following code:
const theme = localStorage.getItem('theme'); function setTheme(newTheme) { localStorage.setItem('theme', newTheme); // Apply the appropriate styles based on the selected theme switch (newTheme) { case 'light': document.documentElement.style.setProperty('--primary-color', '#FFFFFF'); document.documentElement.style.setProperty('--secondary-color', '#000000'); break; case 'dark': document.documentElement.style.setProperty('--primary-color', '#000000'); document.documentElement.style.setProperty('--secondary-color', '#FFFFFF'); break; } } // Initialize the theme based on the stored preference if (theme) { setTheme(theme); } else { setTheme('light'); }
This code persists the user's preferred theme using localStorage and applies the corresponding styles when the theme is changed.
By leveraging the setProperty() method and implementing a theme switch system, you can empower users to customize the look and feel of your website with ease. This approach enables you to create dynamic web experiences that adapt to the user's preferences.
The above is the detailed content of How Can I Dynamically Change CSS Variables with JavaScript for Theme Customization?. For more information, please follow other related articles on the PHP Chinese website!

In this post, Blackle Mori shows you a few of the hacks found while trying to push the limits of Cohost’s HTML support. Use these if you dare, lest you too get labelled a CSS criminal.

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!
