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().
box.style.backgroundColor = "gold"; box.style.setProperty("--size", "120px");
Try it
Syntax
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 is0. - Names. Properties use camelCase (
marginTop), because dashes are not allowed in dot syntax. The CSS propertyfloatiscssFloat. Custom properties must go throughsetProperty()andgetPropertyValue(). - Inline styles are hard to override. Prefer toggling classes with
classListfor state, and usestylefor values that are computed at run time, such as positions, sizes and custom properties. - Reading is limited.
styleshows only what was set inline. For the final value the browser uses, callgetComputedStyle(). - Strict Content Security Policy. Some policies block inline styles written as markup, but changing
element.stylefrom script is generally still allowed. - Assigning the object.
el.style = "color: red"setscssText, but writingel.style.cssTextis 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.
Related
window.getComputedStyle()โ read the final stylesElement.classListโ switch style rules by classElement.getAttribute()andsetAttribute()- Lesson: Styles and classes