CSSStyleDeclaration

Summary

CSSStyleDeclaration represents a collection of CSS property-value pairs. It is used in a few APIs:

Attributes

CSSStyleDeclaration.cssText
Textual representation of the declaration block. Setting this attribute changes the style.
CSSStyleDeclaration.length
The number of properties. See the item method below.
CSSStyleDeclaration.parentRule
The containing CssRule.

Methods

CSSStyleDeclaration.getPropertyPriority()
Returns the optional priority, "important". Example: priString= styleObj.getPropertyPriority('color')
CSSStyleDeclaration.getPropertyValue()
Returns the property value. Example: valString= styleObj.getPropertyValue('color')
CSSStyleDeclaration.item()
Returns a property name. Example: nameString= styleObj.item(0) Alternative: nameString= styleObj[0]
CSSStyleDeclaration.removeProperty()
Returns the value deleted. Example: valString= styleObj.removeProperty('color')
CSSStyleDeclaration.setProperty()
No return. Example: styleObj.setProperty('color', 'red', 'important')
CSSStyleDeclaration.getPropertyCSSValue()
Only supported via getComputedStyle. Returns an ROCSSPrimitiveValue in Firefox (CSSPrimitiveValue, in others, which implements CSSValue), or null for Shorthand properties. Example: cssString= window.getComputedStyle(elem, null).getPropertyCSSValue('color').cssText;
Note: Gecko 1.9 returns null unless using getComputedStyle().
Note: This method may be deprecated by the W3C , and it is not present in the latest CSSOM draft. It is not supported by IE and even though the function exists in Opera, calling it throws a DOMException NOT_SUPPORTED_ERR.

Example

JavaScript
var styleObj= document.styleSheets[0].cssRules[0].style;
alert(styleObj.cssText);
for (var i = styleObj.length-1; i >= 0; i--) {
   var nameString = styleObj[i];
   styleObj.removeProperty(nameString);
}
alert(styleObj.cssText);

Notes

The declaration block is that part of the style rule that appears within the braces and that actually provides the style definitions (for the selector, the part that comes before the braces).

See also

Specification

CSSOM: CSSStyleDeclaration

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/cssstyledeclaration

API CSSOM Interface NeedsCompatTable NeedsMarkupWork NeedsSpecTable Reference Référence