Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all css styles for a DOM element (a la Firebug)

For a DOM element, how to I get all styles specified in css for a particular element? Is it a case of iterating over all css style names?

Or is there a more elegant way? How does Firebug do it?

Thanks

like image 649
Richard H Avatar asked Nov 17 '09 13:11

Richard H


People also ask

How do I get CSS style of an element?

The CSS of an element can be obtained using the getComputedStyle element function in JavaScript. It returns a JavaScript object containing CSS properties and their values. This object is indexed and iterable over the property names. The getPropertyValue(property) is used to get the value of a property.

How get all inspect element CSS?

On Chrome (without CSS Scan): On the left side is the HTML DOM tree, and on the right side, the CSS styles of the selected element. Having the right element selected on the HTML DOM tree, right-click on it and choose “Copy” > “Copy styles”. And done, the CSS was copied! That's how you copy CSS from “Inspect element”.

Does CSS manipulate the DOM?

No, CSS does not change the DOM.


2 Answers

You should be able to get it with getComputedStyle:

var css = window.getComputedStyle(element);
for (var i=0; i<css.length; i++) {
    console.log(css[i] +'='+css.getPropertyValue(""+css[i]))
}

However, this method returns computed style meaning that it will perform some computation and convert your values in px. For example if you have a line-height of 1.2 then it will be returned as 57.6px instead of 1.2

like image 195
NoBugs Avatar answered Sep 20 '22 07:09

NoBugs


Preveous solutions mangle the styles as they are modified before expanded. Also you get a lot of default styles.

my solution strips the default styles and keeps the cascading styles through the elements.

Run in console and copy the element you want from the Elements view. (tested in chrome)

function setStyle(theElement) {
    var els = theElement.children;
    for(var i = 0, maxi = els.length; i < maxi; i++)
    {
        setStyle(els[i]);

        var defaultElem = document.createElement(els[i].nodeName)
        var child = document.body.appendChild(defaultElem);
        var defaultsStyles = window.getComputedStyle(defaultElem,null);     

        var computed = window.getComputedStyle(els[i],null).cssText;

        for(var j = 0, maxj = defaultsStyles.length; j < maxj; j++)
        {
            var defaultStyle = defaultsStyles[j] + ": " + defaultsStyles.getPropertyValue(""+defaultsStyles[j]) + ";"
            if(computed.startsWith(defaultStyle)) {
                computed = computed.substring(defaultStyle.length);
            } else {
                computed = computed.replace(" " + defaultStyle, "");
            }
        }

        child.remove();

        els[i].setAttribute("style", computed);
    }
}

setStyle(document.getElementsByTagName("body")[0]);

console.log("DONE");
like image 44
Stephan T. Avatar answered Sep 22 '22 07:09

Stephan T.