Make Your CSS Dynamic with CSS Custom Properties

PHOTO EMBED

Tue May 09 2023 16:48:38 GMT+0000 (Coordinated Universal Time)

Saved by @mindplumber #javascript #css

Changing the Value of a Custom Property Using JavaScript
I’ve been mentioning throughout this whole article that variables can be updated using JavaScript, so let’s get into that.

Say you have a light theme and want to switch it to a dark theme, assuming you have some CSS like the following:

```css
:root {
  --text-color: black;
  --background-color: white;
}

body {
  color: var(--text-color);
  background: var(--background-color);
}
```

You can update the `--text-color` and `--background-color` custom properties by doing the following:

```js
var bodyStyles = document.body.style;
bodyStyles.setProperty('--text-color', 'white');
bodyStyles.setProperty('--background-color', 'black');
```
content_copyCOPY

https://www.toptal.com/front-end/dynamic-css-with-custom-properties