How to change CSS with JavaScript?

To change CSS with JavaScript, use the style property on an HTML element.

For example, if this is your HTML element with some text in it:

<div id="my-div">Hello World!</div>

Then you can change its CSS with this JavaScript:

document.getElementById('my-div').style.backgroundColor = 'red';
document.getElementById('my-div').style.color = 'blue';
document.getElementById('my-div').style.fontFamily = 'Verdana';

Learn more about different style properties from the CSS Properties Reference.