This site runs best with JavaScript enabled.

Adding multiple styles in JS

Kevin Beck

July 31, 2019


Using vanilla JS you need to add styles to a DOM element one at a time with the Element.style property. One way you can add multiple styles is by using an object.assign method on the element.style property:

// Get the element
var youElement = document.querySelector('.youElement');
// Setup the styles object
var styles = {
color: 'red',
backgroundColor: 'green',
};
// Add the styles to the element
Object.assign(youElement.style, styles);

Source