13

I need to get list of elements all css attributes. How can I do that ?

newbie
  • 24,286
  • 80
  • 201
  • 301
  • Do you want to get all the css properties of an element? – rahul Sep 24 '09 at 11:38
  • 1
    Yes, I need to get all css attributes that my selected element has. – newbie Sep 24 '09 at 11:41
  • related: http://stackoverflow.com/questions/754607/can-jquery-get-all-css-styles-associated-with-an-element – Aziz Sep 24 '09 at 11:42
  • Also related: http://stackoverflow.com/questions/1004475/jquery-css-plugin-that-returns-computed-style-of-element-to-pseudo-clone-that-ele – gnarf Sep 24 '09 at 11:58
  • Useful answer: `document.defaultView.getComputedStyle(document.body, '')` got from http://stackoverflow.com/questions/2614963/how-do-i-get-all-supported-css-properties-in-webkit – Muhammad Umer Aug 01 '13 at 09:16

4 Answers4

20

Copying the source from SO1004475 - jQuery CSS plugin that returns computed style of element to pseudo clone that element? - Please follow link and upvote there if you find it useful.

It seems ridiculous, but this is probably your best option - makes .css() with no arguments get an object with all this stuff set.

jQuery.fn.css = (function(css2) { 
    return function() {
        if (arguments.length) { return 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]] = css2.call(this, attr[i]);
        }
        return obj;
    };
})(jQuery.fn.css);

Note that this doesn't capture all possible CSS properties, particularly new ones for CSS3. Here is a list of all standard CSS and stable CSS3 properties, and here's one of hyphen-prefixed vendor-specific properties (such as -moz-border-start). If you really want all of them, you can glean them from there.

Community
  • 1
  • 1
gnarf
  • 105,192
  • 25
  • 127
  • 161
16

As of jQuery 1.8, you can do:

$( "#yourElement" ).css( "cssText" );

Which uses the underlying window.getComputedStyle( element ).cssText. That's the simplest way.

Mike Sherov
  • 13,277
  • 8
  • 41
  • 62
  • 2
    @EJTH I'm the author of that code inside jQuery ;-) – Mike Sherov Apr 29 '15 at 16:08
  • Thanks! You helped me solve the problem of moving stuff to a certain container in the dom without any styling getting lost! :-) I use it for some pet project, make a HTML page fall apart with box2d... – EJTH Apr 29 '15 at 20:30
  • WOW! I love this! Simply the best way if you want a dump of every CSS property in existence! – Leviscus Tempris Dec 03 '16 at 21:47
3

For inline styles:

var styles = $("#someelement").attr("style");

From there, you should be able to split this string if you need to loop the styles.

To check individual styles, check the docs:

http://docs.jquery.com/CSS

jbutler483
  • 24,074
  • 9
  • 92
  • 145
ScottE
  • 21,530
  • 18
  • 94
  • 131
  • I need also attributes that are set in css – newbie Sep 24 '09 at 11:43
  • Perhaps explain what you're trying to accomplish in your question. You may be better off checking for individual styles, or using classes and using .hasClass() - gnarf has a link below otherwise – ScottE Sep 24 '09 at 14:00
-4
$("#div1).css("background-color");

will return the background color property of an element with id div1.

css( name )

Sorry, I don't know a way to get all the CSS attributes using jQuery.

rahul
  • 184,426
  • 49
  • 232
  • 263