1

I was wondering if you can activate some CSS through an HTML button press or scrolling to a certain point. It's preferred if it's through JS.

3
  • Yes, that is possible. Commented Dec 10, 2021 at 15:56
  • Yes, I know that but how to do it? Commented Dec 10, 2021 at 15:57
  • We can't help you if you don't include a working snippet. Commented Dec 10, 2021 at 15:59

2 Answers 2

1

You can use a wrapper class:

const button = document.querySelector("button") button.addEventListener("click", () => { document.body.classList.add("red") })
.red { color: red; }
<h1>Stackoverflow is nice!</h1> <p>Great website!</p> <button>Change CSS</button>

  • Create a button which listens for a user action.
  • When the button is clicked add a class to the body element.
  • Use this class to apply different css rules.

You can build a dark theme with that for example.

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

Comments

0

As previously said by @Lain, it is indeed possible. something like someElement.classList.add() or someElement.classlist.toggle() MDN Web API

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.