W3cubDocs

/DOM

CSSStyleDeclaration

The CSSStyleDeclaration API represents an object that is a CSS declaration block, that exposes style information and various style-related methods and properties.

A CSSStyleDeclaration object can be exposed using three different APIs:

  • Via HTMLElement.style which deals with the inline styles of a single element (<elem style="...">);
  • Via the CSSStyleSheet API (e.g. document.styleSheets[0].cssRules[0].style returns a CSSStyleDeclaration object on the first CSS rule in the document's first stylesheet).
  • Via window.getComputedStyle() which exposes the CSSStyleDeclaration object as a read-only interface.

Attributes

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

Methods

CSSStyleDeclaration.getPropertyPriority()
Returns the optional priority, "important".
CSSStyleDeclaration.getPropertyValue()
Returns the property value given a property name.
CSSStyleDeclaration.item()
Returns a property name.
CSSStyleDeclaration.removeProperty()
Removes a property from the CSS declaration block.
CSSStyleDeclaration.setProperty()
Modifies an existing CSS property or creates a new CSS property in the declaration block.
CSSStyleDeclaration.getPropertyCSSValue()
Only supported via getComputedStyle in Firefox. Returns the property value as a CSSPrimitiveValue or null for shorthand properties.

Example

var styleObj = document.styleSheets[0].cssRules[0].style;
console.log(styleObj.cssText);

for (var i = styleObj.length; i--;) {
  var nameString = styleObj[i];
  styleObj.removeProperty(nameString);
}

console.log(styleObj.cssText);

Specifications

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support 1 ? 1 ? Yes ?
cssText Yes ? Yes ? Yes ?
length Yes Yes Yes Yes Yes Yes
item Yes Yes Yes Yes Yes Yes
getPropertyValue Yes Yes Yes Yes Yes Yes
getPropertyCSSValue ? — 41
? — 41
See bug 331608.
No ? — 62
? — 62
Only returns a result if called on the result of getComputedStyle().
No 15 — 28
15 — 28
See bug 331608.
Yes
getPropertyPriority Yes Yes Yes Yes Yes Yes
setProperty Yes Yes Yes Yes Yes Yes
removeProperty Yes Yes Yes Yes Yes Yes
parentRule Yes Yes Yes Yes Yes Yes
cssFloat Yes ? No ? Yes ?
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support Yes Yes ? 4 Yes ? ?
cssText Yes Yes ? Yes Yes ? ?
length Yes Yes Yes Yes Yes Yes ?
item Yes Yes Yes Yes Yes Yes ?
getPropertyValue Yes Yes Yes Yes Yes Yes ?
getPropertyCSSValue Yes ? — 41
? — 41
See bug 331608.
No ? — 62 15 — 28
15 — 28
See bug 331608.
Yes ?
getPropertyPriority Yes Yes Yes Yes Yes Yes ?
setProperty Yes Yes Yes Yes Yes Yes ?
removeProperty Yes Yes Yes Yes Yes Yes ?
parentRule Yes Yes Yes Yes Yes Yes ?
cssFloat Yes Yes ? No Yes ? ?

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration