CSSStyleDeclaration
Summary
CSSStyleDeclaration
represents a collection of CSS property-value pairs. It is used in a few APIs:
HTMLElement.style
- to manipulate the style of a single element (<elem style="...">);- (TODO: reword) is an interface to the declaration block returned by the
style
property of acssRule
in a stylesheet, when the rule is a CSSStyleRule. CSSStyleDeclaration
is also a read-only interface to the result of window.getComputedStyle().
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), ornull
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 aDOMException
NOT_SUPPORTED_ERR.
Example
JavaScript
Copy Code
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
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