1

I checked this topic about CSS variables; Creating CSS Global Variables : Stylesheet theme management

I define global vars,

:root {
--MytextColor: blue;
--MyBackColor: black;
}

and I m able to modify values using javascript with this line,

document.documentElement.style.setProperty('--MyBackColor', 'yellow');

that's fine, but when I refresh the page they return back to original values, is it possible to keep these variables in the memory, or cookies or etc. until we clear browser data (such as full refresh Ctrl+F5).

The reason I was trying that is to make light mode, dark mode using same css class only with css values.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ali CAKIL
  • 383
  • 5
  • 21
  • 2
    Explore [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) for this. – Lakshya Thakur May 10 '21 at 13:53
  • not possible without javascript. you would need to store the data in `localStorage` and then always copy them to the css vars – Lux May 10 '21 at 13:55

3 Answers3

1

A web page is always reloaded from scratch when it is refreshed. If you need to keep the state over page refreshs, you need to store the value somewhere. You can do this by using a cookie, the indexed DB or just the simple local storage.

Here is an example:

const color = localStorage.getItem('color-mode');
if (color !== null) {
  document.documentElement.style.setProperty('--MyBackColor', color);
}

Set the value to the local storage by using:

localStorage.setItem('color-mode', 'yellow');
ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87
1

Use window.localStorage to store the property value when changed and apply it on script execution, if it's been saved.

The localStorage read-only property of the window interface allows you to access a Storage object for the Document's origin; the stored data is saved across browser sessions. https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

if(localStorage.getItem("--MyBackColor") != null)
  document.documentElement.style.setProperty("--MyBackColor", localStorage.getItem("--MyBackColor"));

document.getElementById("refresh").addEventListener("click", (event) => {
  window.location.reload();
});

document.getElementById("yellow").addEventListener("click", (event) => {
  document.documentElement.style.setProperty("--MyBackColor", "yellow");
  localStorage.setItem("--MyBackColor", "yellow");
});

document.getElementById("white").addEventListener("click", (event) => {
  document.documentElement.style.setProperty("--MyBackColor", "white");
  localStorage.setItem("--MyBackColor", "white");
});
:root {
  --MyBackColor: white;
}

body {
  background: var(--MyBackColor);
}
hi

<button id="white">White</button>
<button id="yellow">Yellow</button>

<button id="refresh">Refresh</button>
Nora
  • 612
  • 3
  • 11
0

You should try to save the variable names in localStorage. https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

Then you would take whatever you want from the localStorage or it will fall back to a default value

Abregre
  • 486
  • 4
  • 11