Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท DOM: Document and Elements

HTMLElement.style

Reference for HTMLElement.style: set and read inline CSS from JavaScript with camelCase properties, setProperty(), custom properties and cssText.

The style property returns a CSSStyleDeclaration that represents the element's inline style, the contents of its style="..." attribute. Setting a property on it writes a declaration into that attribute, which wins over rules in style sheets (unless they use !important).

Only inline styles appear here. Reading el.style.color will not tell you the colour that comes from a style sheet; for that, use getComputedStyle().

JavaScript
box.style.backgroundColor = "gold";
box.style.setProperty("--size", "120px");

Try it

Setting, reading and removing
Custom properties as a bridge

Syntax

JavaScript
element.style.propertyName = value;       // camelCase: backgroundColor
element.style.setProperty(name, value, priority)   // kebab-case: "background-color"
element.style.getPropertyValue(name)
element.style.removeProperty(name)
element.style.cssText = "color: red; margin: 0";
Member Description
style.camelCaseName Reads or writes one property, for example style.fontSize. Reading returns "" when the inline style does not set it.
setProperty(name, value, priority) Sets a property by its CSS name. priority can be "important". Works for custom properties (--name).
getPropertyValue(name) Returns the inline value as a string, or "".
removeProperty(name) Removes the declaration and returns the old value (or "").
cssText The whole inline style as one string. Assigning replaces all inline declarations.
length, item(index) Number of set properties, and the name at a position.

Assigning an empty string (el.style.color = "") removes that inline declaration.

Usage notes

  • Invalid values are ignored silently. The declaration is not set, and no error is thrown. A number without a unit (style.width = 200) is invalid; use "200px". The one exception is 0.
  • Names. Properties use camelCase (marginTop), because dashes are not allowed in dot syntax. The CSS property float is cssFloat. Custom properties must go through setProperty() and getPropertyValue().
  • Inline styles are hard to override. Prefer toggling classes with classList for state, and use style for values that are computed at run time, such as positions, sizes and custom properties.
  • Reading is limited. style shows only what was set inline. For the final value the browser uses, call getComputedStyle().
  • Strict Content Security Policy. Some policies block inline styles written as markup, but changing element.style from script is generally still allowed.
  • Assigning the object. el.style = "color: red" sets cssText, but writing el.style.cssText is more explicit.
  • Units and shorthands. Shorthands such as style.margin = "4px 8px" work. Reading a shorthand back may return the value normalized by the browser.

Browser support

Supported in every browser. Setting custom properties with setProperty() works in all current browsers.