Skip to content

Instantly share code, notes, and snippets.

@jubianchi
Created October 19, 2011 05:44
Show Gist options
  • Save jubianchi/1297545 to your computer and use it in GitHub Desktop.
Save jubianchi/1297545 to your computer and use it in GitHub Desktop.
Get the computed CSS styles of an element
(function(){
var sel = prompt('Selector...'),
elt = $(sel),
gcs = ('getComputedStyle' in window ? getComputedStyle : document.defaultView.getComputedStyle),
cs = gcs(elt.get(0), null),
css = [],
scss = ''
space = '    ';
if(window.console) console.group(sel);
scss = sel +'{<br/>';
if(cs.length !== 0) {
for(var i = 0, val; i < cs.length; i++) {
val = cs.getPropertyCSSValue(cs[i]);
if(val.cssValueType in [0,2,3]) {
scss = scss + space + cs[i] + ': ' + cs.getPropertyValue(cs[i]) + ';<br/>';
if(window.console) {
console.log(val.cssValueType + '-' + cs[i] + ' : ' + cs.getPropertyValue(cs[i]));
console.log(typeof cs);
}
}
}
} else {
}
scss = scss + '}';
if(window.console) console.groupEnd();
var over = $('<div/>').css({
position: 'absolute',
top: 0, left: 0,
backgroundColor: 'rgba(0,0,0,0.6)',
width: $(document).width(),
height: $(document).height()
}).appendTo($('body')),
body = $('<div><h1>' + sel + '</h1><pre>' + scss + '</pre></div>').css({
position: 'fixed',
padding: 10,
top: ($(window).height() / 2) - 250,
left: ($(window).width() / 2) - ($(window).width() / 4),
backgroundColor: '#fff',
color: '#000',
width: '50%',
height: 500,
overflow: 'auto',
borderRadius: 5
}).appendTo(over);
$('<a href="#">Close</a>').bind('click', function() {
over.fadeOut(function() {
over.remove();
});
}).css({
position: 'absolute',
top: 10,
right: 10
}).appendTo($('h1', body));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment