1

I'm currently trying to do it by changing my :root variables for the color scheme of the page with a function changeBackgrondColor() and assigning to my fontAwesome moon "button". However, when i click at the moon, the page changes all of the colors for a sec and then comeback to the original form.

var r = document.querySelector(':root');
var rootStyles = getComputedStyle(r);
function changeBackgroundColor(){
    r.style.setProperty('--clr-1', 'blue');
    r.style.setProperty('--clr-2', 'blue');
    r.style.setProperty('--clr-3', 'blue');
    r.style.setProperty('--clr-4', 'blue');
}
1
  • Can you convert your code into a snippet? Commented Feb 23, 2024 at 18:02

1 Answer 1

1

Usually you set class theme-dark to body element and use CSS veriables for colors:

.theme-dark{
  --body-bg:#333;
  --color:#aaa;
  --input-bg: #555;
}
body{
  background-color: var(--body-bg);
  color: var(--color);
}

input, button{
  color: var(--color);
  background-color: var(--input-bg);
}
<input><button onclick="document.body.classList.toggle('theme-dark')">Toggle dark theme</button>

Sign up to request clarification or add additional context in comments.

2 Comments

i could use the same variables i declared at :root and assign at this .theme-dark class with different colors then it changes the page color when i click at the button?
@JoãoMarcelo yes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.