Getcomputedstyle (Or) $.Css(Map) <-- to Get Every Style Declaration

getComputedStyle (or) $.css(map) -- to get every style declaration

is there any way to get a key/value output of all styling of a dom element?

Yes, but don't expect the exact handling of values (units etc.) to be the same cross-browser.

var styles= [];

// The DOM Level 2 CSS way
//
if ('getComputedStyle' in window) {
var cs= getComputedStyle(element, '');
if (cs.length!==0)
for (var i= 0; i<cs.length; i++)
styles.push([cs.item(i), cs.getPropertyValue(cs.item(i))]);

// Opera workaround. Opera doesn't support `item`/`length`
// on CSSStyleDeclaration.
//
else
for (var k in cs)
if (cs.hasOwnProperty(k))
styles.push([k, cs[k]]);

// The IE way
//
} else if ('currentStyle' in element) {
var cs= element.currentStyle;
for (var k in cs)
styles.push([k, cs[k]]);
}

Getting all the style properties using JQuery. Basically I want an equivalent of getComputedStyle() and I dont want to handle cross-browser stuff

Well, there is a soultion for a similiar problem offered by Keith Bentrup: jQuery CSS plugin that returns computed style of element to pseudo clone that element?

He used a list of attributes from Firebug and created a computed style object with jQuery to be able to clone styles from one object to another:

jQuery.fn.css2 = jQuery.fn.css;
jQuery.fn.css = function() {
if (arguments.length) return jQuery.fn.css2.apply(this, arguments);
var attr = ['font-family','font-size','font-weight','font-style','color',
'text-transform','text-decoration','letter-spacing','word-spacing',
'line-height','text-align','vertical-align','direction','background-color',
'background-image','background-repeat','background-position',
'background-attachment','opacity','width','height','top','right','bottom',
'left','margin-top','margin-right','margin-bottom','margin-left',
'padding-top','padding-right','padding-bottom','padding-left',
'border-top-width','border-right-width','border-bottom-width',
'border-left-width','border-top-color','border-right-color',
'border-bottom-color','border-left-color','border-top-style',
'border-right-style','border-bottom-style','border-left-style','position',
'display','visibility','z-index','overflow-x','overflow-y','white-space',
'clip','float','clear','cursor','list-style-image','list-style-position',
'list-style-type','marker-offset'];
var len = attr.length, obj = {};
for (var i = 0; i < len; i++)
obj[attr[i]] = jQuery.fn.css2.call(this, attr[i]);
return obj;
}

This seems to do exactly what you are looking for.

There are also a couple of plugins for that:

  1. http://github.com/peol/jquery-computed-style
  2. http://www.jupiterit.com/news/get-multiple-computed-styles-fast-with-the-curstyles-jquery-plugin

getComputedStyle() returns nothing, but getComputedStyle().getPropertyValue() returns as expected

From MDN Documentation:

The Window.getComputedStyle() method returns an object containing the values of all CSS properties of an element, after applying active stylesheets and resolving any basic computation those values may contain. Individual CSS property values are accessed through APIs provided by the object, or by indexing with CSS property names.

It's stated that you need to use API functions, such as getPropertyValue() to get the value of it.

Ref: https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle

If you want to print all of the CSS styles from a specific element you may just iterate all the attributes like this:

function dumpCSSText(element){
var s = '';
var o = getComputedStyle(element);
for(var i = 0; i < o.length; i++){
s+=o[i] + ': ' + o.getPropertyValue(o[i])+';\n';
}
return s;
}

var e = document.querySelector('.card');
console.log(dumpCSSText(e));
.card {
color: #ff0;
font-size: 3rem;
font-weight: 600;
border: 3px solid blueviolet;
background-color: greenyellow;
}
<div class="card"></div>

how to get all the calculated styles of an element with jQuery?

after further research using Majid's answer, I found out that Jquery's ".css" method return computed style as well, and is better because it accounts for browser difference
http://api.jquery.com/css/

Get the computed style and omit defaults

there you go,
i did this by adding a new dummy DOM element, to know which styles are default for any element.

/**
* IE does not have `getComputedStyle`
*/

window.getComputedStyle = window.getComputedStyle || function( element ) {
return element.currentStyle;
}

/**
* get computed style for an element, excluding any default styles
*
* @param {DOM} element
* @return {object} difference
*/

function getStylesWithoutDefaults( element ) {

// creating an empty dummy object to compare with
var dummy = document.createElement( 'element-' + ( new Date().getTime() ) );
document.body.appendChild( dummy );

// getting computed styles for both elements
var defaultStyles = getComputedStyle( dummy );
var elementStyles = getComputedStyle( element );

// calculating the difference
var diff = {};
for( var key in elementStyles ) {
if(elementStyles.hasOwnProperty(key)
&& defaultStyles[ key ] !== elementStyles[ key ] )
{
diff[ key ] = elementStyles[ key ];
}
}

// clear dom
dummy.remove();

return diff;
}

/**
* usage
*/

console.log( getStylesWithoutDefaults( document.getElementById( 'bar' ) ) );

Notes:

  • the result will have some extra properties, not only those you've mentioned.

demo - console should be opened



Related Topics



Leave a reply



Submit