Window.getComputedStyle()

  • Revision slug: DOM/window.getComputedStyle
  • Revision title: window.getComputedStyle
  • Revision id: 53589
  • Created:
  • Creator: Michael Ryan
  • Is current revision? No
  • Comment null, not empty string; 1 words added, 3 words removed

Revision Content

{{ DomRef() }}

Summary

Returns computed style of the element. Computed style represents the final computed values of all CSS properties for the element.

Syntax

var style = window.getComputedStyle(element, pseudoElt);
  • element is an element.
  • pseudoElt is a string specifying the pseudo-element to match. Should be null for regular elements.
  • style is a CSSStyleDeclaration object.

Example

var element = document.getElementById(“elemId”);
var style = document.defaultView.getComputedStyle(element, pseudoElt);
//practical example:
<html>
<head>
<style>
#elem_container{
  position: absolute;
  left: 100Px;
  top: 200px;
  height:100px ;
}
</style>
</head>
<body>
<div id="elem_container">dummy</div>
<div id="output"></div>  
<script>
function getTheStyle(){
var elem=document.getElementById("elem_container");
var theCSSprop=document.defaultView.getComputedStyle(elem,null).getPropertyValue("height");
document.getElementById("output").innerHTML=theCSSprop;
}
getTheStyle()
</script>
</body>
</html>


Description

The returned object is of the same type that the object returned from the element's style property, however the two objects have different purpose. The object returned from getComputedStyle is read-only and can be used to inspect the element's style (including those set by a <style> element or an external stylesheet). The elt.style object should be used to set styles on a specific element.

The first argument must be an Element, not a Node (as in a #text Node).

Specification

DOM Level 2 Style: getComputedStyle

{{ languages( { "ja": "ja/DOM/window.getComputedStyle" } ) }}

Revision Source

<p>{{ DomRef() }}</p>
<h3 name="Summary">Summary</h3>
<p>Returns computed style of the element. Computed style represents the final <a class="external" href="http://www.w3.org/TR/1998/REC-CSS2-19980512/cascade.html#computed-value">computed</a> values of all CSS properties for the element.</p>
<h3 name="Syntax">Syntax</h3>
<pre class="eval">var <em>style</em> = window.getComputedStyle(<em>element</em>, <em>pseudoElt</em>);
</pre>
<ul> <li><code>element</code> is an <a href="/en/DOM/element" title="en/DOM/element">element</a>.</li> <li><code>pseudoElt</code> is a string specifying the pseudo-element to match. Should be <code>null</code> for regular elements.</li> <li><code>style</code> is a <a class="external" href="http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSview-getComputedStyle"><code>CSSStyleDeclaration</code></a> object.</li>
</ul><h3 name="Example">Example</h3>
<pre class="eval">var element = document.getElementById(“elemId”);
var <em>style</em> = document.defaultView.getComputedStyle(<em>element</em>, <em>pseudoElt</em>);
</pre>
<pre class="eval">//practical example:
&lt;html&gt;
&lt;head&gt;
&lt;style&gt;
#elem_container{
  position: absolute;
  left: 100Px;
  top: 200px;
  height:100px ;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="elem_container"&gt;dummy&lt;/div&gt;
&lt;div id="output"&gt;&lt;/div&gt;  
&lt;script&gt;
function getTheStyle(){
var elem=document.getElementById("elem_container");
var theCSSprop=document.defaultView.getComputedStyle(elem,null).getPropertyValue("height");
document.getElementById("output").innerHTML=theCSSprop;
}
getTheStyle()
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;


</pre>
<h3 name="Description">Description</h3>
<p>The returned object is of the same type that the object returned from the element's <a href="/en/DOM/element.style" title="en/DOM/element.style">style</a> property, however the two objects have different purpose. The object returned from <code>getComputedStyle</code> is read-only and can be used to inspect the element's style (including those set by a &lt;style&gt; element or an external stylesheet). The <code>elt.style</code> object should be used to set styles on a specific element.</p>
<p>The first argument must be an Element, not a Node (as in a #text Node).</p>
<h3 name="Specification">Specification</h3>
<p><a class="external" href="http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSview-getComputedStyle">DOM Level 2 Style: getComputedStyle</a></p>
<p>{{ languages( { "ja": "ja/DOM/window.getComputedStyle" } ) }}</p>
Revert to this revision