JavaScriptEasy
How do you manipulate CSS styles using JavaScript?
By FrontendPro Editorial Team Updated 8/8/2026
#JavaScript
Answer
You can manipulate CSS styles using JavaScript by accessing the style property of an HTML element. For example, to change the background color of a div element with the id myDiv, you can use:
js
document.getElementById("myDiv").style.backgroundColor = "blue";
You can also add, remove, or toggle CSS classes using the classList property:
js
document.getElementById("myDiv").classList.add("newClass");
document.getElementById("myDiv").classList.remove("oldClass");
document.getElementById("myDiv").classList.toggle("toggleClass");
