docs.rodeo

MDN Web Docs mirror

CSSStyleDeclaration: getPropertyValue() method

{{ APIRef("CSSOM") }} 

The CSSStyleDeclaration.getPropertyValue() method interface returns a string containing the value of a specified CSS property.

Syntax

getPropertyValue(property)

Parameters

Return value

A string containing the value of the property. If not set, returns the empty string.

The property value is dynamically computed, not what was originally specified in the declaration. The serialization happens in the following way:

In essence, the property value is canonicalized, ensuring that two property values with the same rendering effect compare equal even when they are declared differently.

Examples

The following JavaScript code queries the value of the margin property in a CSS selector rule:

const declaration = document.styleSheets[0].cssRules[0].style;
const value = declaration.getPropertyValue("margin"); // "1px 2px"

The returned string might differ from the value specified in the style specification of the element. For instance this styling:

<style>
  p#blueish {
    color: hsl(250 90 50);
  }
</style>
<script>
  const declaration = document.styleSheets[0].cssRules[0].style;
  const value = declaration.getPropertyValue("color");
</script>

Will set a value rgb(51, 13, 242);. This is important when comparing styles by string.

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

In this article

View on MDN