I’m looking to get the used css values of all DOM elements on a page. When I say "used values" I’m referring to the definition as specified in the W3C specification:
6.1.3 Used values
Computed values are processed as far as possible without formatting the document. Some values, however, can only be determined when the document is being laid out. For example, if the width of an element is set to be a certain percentage of its containing block, the width cannot be determined until the width of the containing block has been determined. The used value is the result of taking the computed value and resolving any remaining dependencies into an absolute value.
These should be the final values computed with respect to the actual page layout. Mozilla’s docs claim that you can use window.getComputedStyle to get the used values, but this does not make sense to me because computed values are different from used values (and I want used values). Even if these are the used values, I’m not sure if this only works in Firefox or not. Is there a way to reliably get used values in all browsers?
Note: The world has moved on since the question was asked and answered. There are now more layers of values than there used to be: declared, cascaded, specified, computed, resolved, used, and actual.
getComputedStylereturns resolved values (which are either computed or used depending on the property). Here are the layers:From CSS Cascading and Inheritance Level 4:
Then, the CSS Object Model defines resolved values:
…which is followed by a list of properties (specific ones and categories) saying whether the resolved value is the computed or used value.
With that backdrop:
getComputedStyleworks on all major modern browsers. Earlier versions of IE provide a near-equivalent in the form ofcurrentStyle.getComputedStylereturns resolved values, which for any given property is either the computed value or the used value, with the CSSOM spec defining clearly what properties get returned with which kind of value under which circumstances. I don’t see anything in CSSC&I4 or CSSOM defining a way to access used values in cases where the resolved value isn’t the used value, or a way to access actual values, and gsnedders says they have checked with the working group and confirmed there isn’t a way to get used values, at least not yet.Resolved values are probably good enough for what you need. For instance, the following example shows
207.5pxor similar , not50%. That’s the resolved value, which is also the used value in this particular case (because I usedwidthon an element where thedisplayisn’tnoneorcontents), but possibly not the actual value, depending on whether subpixel rendering is feasible and appropriate in this case.