cssText property
The cssText property allows you
to set CSS styles in bulk in one line. In
this case, the entire content of the
style
attribute is overwritten.
Syntax
element.style.cssText = 'property 1: value; property 2: value...'
Example
Let's give an element some styles:
<p id="elem"></p>
let elem = document.querySelector('#elem');
elem.style.cssText = 'color: red; font-size: 40px;';
Example
In this example, an element will
initially have styles in the
style
attribute, but the cssText
property will overwrite it:
<p id="elem" style="background: red;"></p>
let elem = document.querySelector('#elem');
elem.style.cssText = 'color: red; font-size: 20px;';
Example
To prevent previous styles from being overwritten, you can do this:
<p id="elem" style="background: green;"></p>
let elem = document.querySelector('#elem');
elem.style.cssText += 'color: red; font-size: 20px;';
See also
-
the
getComputedStylemethod
that gets the value of an element's CSS property