Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSSStyleDeclaration setProperty() method

CSSStyleDeclaration setProperty() method

CSS CSSStyleDeclaration CSS CSSStyleDeclaration

Example

Set a new CSS property:

var

declaration

=

document

.

styleSheets

[

0

]

.

cssRules

[

0

]

.

style

;

var

setprop

=

declaration

.

setProperty

(

"

background-color

"

,

"

yellow

"

)

;

Try it »


Definition and usage

The setProperty() method is used to set a new CSS property and can also modify an existing property in the CSS declaration block.


Browser support

Method
setProperty() Yes 9.0 Yes Yes Yes

Syntax



object.setProperty(propertyname, value, priority)


Attribute values

Parameters Description
propertyname Required. A string representing the property to create or modify.
value Optional. The new property value.
priority Optional. A string specifying whether the property's priority should be set to important.
 可以是下面三个值:       

 "important"       

 undefined       

 "" |


Technical Details

DOM version: CSS Object Model
Return value: undefined

More examples

Example

Set a new CSS property and use the "important" priority:

var

declaration

=

document

.

styleSheets

[

0

]

.

cssRules

[

0

]

.

style

;

var

setprop

=

declaration

.

setProperty

(

"

background-color

"

,

"

yellow

"

,

"

important

"

)

;

Try it »

CSS CSSStyleDeclaration CSS CSSStyleDeclaration