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 elementvar youElement = document.querySelector('.youElement');// Setup the styles objectvar styles = {color: 'red',backgroundColor: 'green',};// Add the styles to the elementObject.assign(youElement.style, styles);