/html-dom /Basic
GitHub 4932★

Set CSS style for an element

Set a CSS style

Setting the style via the style property:

ele.style.backgroundColor = 'red';
ele.style['backgroundColor'] = 'red';
ele.style['background-color'] = 'red';

Multiple styles can be set at the same time by overwriting or updating the cssText property:

// Add new style
el.style.cssText += 'background-color: red; color: white';

// Ignore previous styles
el.style.cssText = 'background-color: red; color: white';

Remove a CSS style

ele.style.removeProperty('background-color');

// Does NOT work
ele.style.removeProperty('backgroundColor');

See also

Follow me on and to get more useful contents.