CSSStyleDeclaration Object
The CSSStyleDeclaration object represents a collection of CSS property-value pairs.
It is used in API :
HTMLElement.style
window.getComputedStyle( )
Properties
Methods
getPropertyValue(property)
getPropertyPriority(property)
removeProperty(property)
setProperty(property, value, priority)
item(index)
Style Property
The style property is used to get or set the inline style of an element. While getting, it returns a CSSStyleDeclaration object that contains a list of all styles properties for that element with values assigned for the attributes that are defined in the element’s inline style attribute.
Style Property represents only the CSS declarations set in the element’s inline style attribute, not those that come from style rules elsewhere, such as style rules in the <head> section, or external style sheets or browser default.
Syntax:-
Return
element.style.property
Set
element.style.property = “value”
A style declaration is reset by setting it to null or an empty string, e.g. element.style.property = null
Inline Style Manipulation
The most direct way to modify CSS value with JavaScript is through the style property that corresponding to the inline style sheet specification for a particular HTML element. To perform a manipulation of the CSS with a JavaScript DOM interface, you would access the style object of the element.
Style Property
Example: –
Dynamic Style Manipulation
As we know The most direct way to modify CSS value with JavaScript is through the style property that corresponding to the inline style sheet specification for a particular HTML element, This has performance considerations since you need to change a single property at a time. We might manipulate style rules using CSS class Selector. The element’s class attribute is represented as className property in DOM.
DOMTokenList
The DOMTokenList interface represents a set of space-separated tokens. Such a set is returned by Element.classList, HTMLLinkElement.relList, HTMLAnchorElement.relList, HTMLAreaElement.relList, HTMLIframeElement.sandbox, or HTMLOutputElement.htmlFor. etc. It is indexed beginning with 0 as with JavaScript Array objects. DOMTokenList is always case-sensitive.
Properties
length
value
Methods
item()
add()
remove()
replace()
contains()
toggle()
value()
classList Property
The Element.classList is a read-only property that returns a live DOMTokenList collection of the class attributes of the element.
Syntax: –
var elementClasses = elementNodeReference.classList;
Computed Style
The computed style is the style actually used in displaying the element, after “stylings” from multiple sources have been applied.
Style sources can include: internal style sheets, external style sheets, inherited styles, and browser default styles.
Style Property represents only the CSS declarations set in the element’s inline style attribute, not those that come from style rules elsewhere, such as style rules in the section, or external style sheets or browser default. To get the values of all CSS properties for an element you should use window.getComputedStyle( ) instead.
The returned style is a live CSSStyleDeclaration object ( The CSSStyleDeclaration object represents a collection of CSS property-value pairs.), which updates itself automatically when the element’s style is changed.
Syntax:-
window.getComputedStyle(element, pseudoElement)
Element – Element for which to get the computed style.
pseudoElement – A string specifying the pseudo-element to match. Must be omitted (or null) for regular elements.
Ex: –
var elm = document.getElementById(“myid”);
var allCSSProp = window.getComputedStyle(elm)
Tagged : css / Javascript / Manipulation / methods / objects / properties