Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get access to all css style properties?

I want to get access to all CSS properties (not only for a specific selector or element but all) through JavaScript.

I want to iterate through all properties of the .style collection.

How can I do this?

like image 774
Gaurav Avatar asked Apr 28 '12 05:04

Gaurav


People also ask

How do I get all the CSS properties 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.

Do I need to know all CSS properties?

You don't need to commit to memorizing every CSS Property and Value, as there are good places to look them up. There are some fundamental things, however, which will make CSS much easier for you to use. This article aims to guide you along your path of learning CSS.

How many total CSS properties are there?

W3Schools lists 228 of them.

Which of the following when called returns all the CSS properties of an element?

The all shorthand CSS property resets all of an element's properties except unicode-bidi , direction , and CSS Custom Properties.


2 Answers

you can use CSSStyleDeclaration object. A CSSStyleDeclaration object makes CSS style attributes available through JavaScript properties. The names of these JavaScript properties correspond closely to the CSS attribute names.

this object has two additional properties:

cssText
The textual representation of a set of style attributes and their values. The text is formatted as in a CSS stylesheet, minus the element selector and the curly braces that surround the attributes and values.

length
The number of attribute/value pairs contained in this CSSStyleDeclaration. A CSSStyleDeclaration object is also an array-like object whose elements are the names of the CSS style attributes that are declared.

https://developer.mozilla.org/en/DOM/CSSStyleDeclaration
https://developer.mozilla.org/en/DOM/CSS

like image 160
undefined Avatar answered Oct 25 '22 21:10

undefined


You can see how to access style sheets from javascript here: http://www.quirksmode.org/dom/changess.html.

It's different in IE vs. other browsers and it's a bit of a pain to get all the cross browser stuff right.

var theRules = new Array();
if (document.styleSheets[1].cssRules)
    theRules = document.styleSheets[1].cssRules
else if (document.styleSheets[1].rules)
    theRules = document.styleSheets[1].rules

Often, you can just predefine several CSS rules and then add or remove classes from various objects in order to change the applied styles.

like image 35
jfriend00 Avatar answered Oct 25 '22 21:10

jfriend00