Change the color of "myH1":
document.getElementById("myH1").style.color = "red";
Try it Yourself »
Get the value of a "myP"s top-border:
let value = document.getElementById("myP").style.borderTop;
Try it Yourself »
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.
You cannot set a style like this:
element.style = "color:red";
JavaScript syntax is slightly different from CSS syntax:
backgroundColor / background-color.
See our Full Style Object Reference.
Use this style property instead of the The setAttribute() Method, to prevent overwriting other properties in the style attribute.
Return a style property:
element.style.
property
Set a style property:
element.style.
property =
value
Type | Description |
value | The value of the specified property. For example: element.style.borderBottom = "2px solid red" |
Type | Description |
Object | The element's CSSStyleDeclaration object. |
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 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!