In this article, you will learn how to set background color of HTML element using css properties in JavaScript.
Using the css background-color property, we can set the background color of an HTML element.
Syntax :
element{background-color:color-name}
Basically, we will create a class with the property of background-color and set its value to red.
.new-class{background-color:red}
And using the javascript setAttribute() method, we will add the new-class to the targeted element.
document.querySelector('div').setAttribute('class', 'new-class');
Try it Yourself »
Comments (0)