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?
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.
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.
W3Schools lists 228 of them.
The all shorthand CSS property resets all of an element's properties except unicode-bidi , direction , and CSS Custom Properties.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With