Window getComputedStyle()Method

Window 对象参考手册Window Object


Definition and Usage

The getComputedStyle() method is used to obtain the CSS styles of a specified element.

The styles obtained are the element's final rendered styles in the browser.

Syntax

window.getComputedStyle(element, pseudoElement)

Parameter Description:

  • element: Required. The element whose styles are to be obtained.
  • pseudoElement: Optional. A pseudo-element. When not querying a pseudo-element, it can be omitted or passed as null.

Return Value

The returned object is an object of type CSSStyleDeclaration.

Browser Support

The numbers in the table indicate the first browser version number that supports this method.

Method
getComputedStyle() 11.0 9.0 4.0 5 11.5

Example

Example

Get the background color value of a DIV:

function myFunction(){ var elem = document.getElementById("test"); var theCSSprop = window.getComputedStyle(elem, null).getPropertyValue("background-color"); document.getElementById("demo").innerHTML = theCSSprop; }

Try it Yourself »

Window 对象参考手册Window Object

Other Extensions