Adding CSS styles
CSS rules can be applied like any other property; note though that the properties are camel-cased in JavaScript:
myElement.style.marginLeft = '2em'
If we want certain values, we can obtain these via the.style
property. However, this will only give us styles that have been explicitly applied. To get the computed values, we can use,.window.getComputedStyle()
. It takes the element and returns a CSSStyleDeclaration containing all styles from the element itself as well as those inherited from its parents:
window.getComputedStyle(myElement).getPropertyValue('margin-left')