Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu September 26, 2022 . #css . #HowTo

How to set background color of HTML element using css properties in JavaScript

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 »
save
listen
AI Answer
Write Your Answer
loading
back
View All