Is there a cross-browser method of getting the used css values of all properties of all elements?

前端 未结 2 806
孤城傲影
孤城傲影 2021-01-12 17:57

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 specificat

相关标签:
2条回答
  • 2021-01-12 18:31

    You could use jQuery or another preferred library in most cases.

    For instance, your question title has font-size:100% applied to it which can be retrieved w/firebug. But with jQuery api you can retrieve the value used like so:

    $('#question-header .question-hyperlink').css('font-size');//run in console
    //or enter this in the url bar
    //javascript:alert($('#question-header .question-hyperlink').css('font-size'));
    //returns "23.06px"
    

    NB the library is included on this page, but it would be fairly trivial to create a bookmarklet that includes jQuery and polls the necessary properties.

    0 讨论(0)
  • 2021-01-12 18:33

    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. getComputedStyle returns resolved values (which are either computed or used depending on the property). Here are the layers:

    From CSS Cascading and Inheritance Level 4:

    Once a user agent has parsed a document and constructed a document tree, it must assign, to every element in the tree, and correspondingly to every box in the formatting structure, a value to every property that applies to the target media type.

    The final value of a CSS property for a given element or box is the result of a multi-step calculation:

    1. First, all the declared values applied to an element are collected, for each property on each element. There may be zero or many declared values applied to the element.
    2. Cascading yields the cascaded value. There is at most one cascaded value per property per element.
    3. Defaulting yields the specified value. Every element has exactly one specified value per property.
    4. Resolving value dependencies yields the computed value. Every element has exactly one computed value per property.
    5. Formatting the document yields the used value. An element only has a used value for a given property if that property applies to the element.
    6. Finally, the used value is transformed to the actual value based on constraints of the display environment. As with the used value, there may or may not be an actual value for a given property on an element.

    Then, the CSS Object Model defines resolved values:

    getComputedStyle() was historically defined to return the "computed value" of an element or pseudo-element. However, the concept of "computed value" changed between revisions of CSS while the implementation of getComputedStyle() had to remain the same for compatibility with deployed scripts. To address this issue this specification introduces the concept of a resolved value.

    The resolved value for a given longhand property can be determined as follows:

    ...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:

    getComputedStyle works on all major modern browsers. Earlier versions of IE provide a near-equivalent in the form of currentStyle.

    getComputedStyle returns 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.5px or similar , not 50%. That's the resolved value, which is also the used value in this particular case (because I used width on an element where the display isn't none or contents), but possibly not the actual value, depending on whether subpixel rendering is feasible and appropriate in this case.

    (function() {
      var target = document.getElementById("target");
      var style = window.getComputedStyle(target);
      display("computed width = " + style.width);
      
      function display(msg) {
        var p = document.createElement('p');
        p.innerHTML = String(msg);
        document.body.appendChild(p);
      }
    })();
    <div id="target" style="display: inline-block; width: 50%">x</div>

    0 讨论(0)
提交回复
热议问题