HTML DOM Element style
Examples
Change the color of "myH1":
Get the value of a "myP"s top-border:
Description
The style
property returns the values of an element's style attribute.
The style
property returns a CSSStyleDeclaration
object.
The CSSStyleDeclaration
object contains all inline styles properties for the element.
It does not contain any style properties set in the <head> section
or in any external style sheets.
Note 1
You cannot set a style like this:
Note 2
JavaScript syntax is slightly different from CSS syntax:
backgroundColor / background-color.
See our Full Style Object Reference.
Note 3
Use this style property instead of the The setAttribute() Method, to prevent overwriting other properties in the style attribute.
See Also:
Syntax
Return a style property:
Set a style property:
Return Value
For example:
element.style.borderBottom = "2px solid red"
Return Value
Browser Support
element.style
is a DOM Level 2 (2001) feature.
It is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |