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

jQuery css() method

jQuery css() method

jQuery HTML/CSS methods jQuery HTML/CSS Methods

Example

Set all

elements' color property:

$

(

"

button

"

)

.

click

(

function

(

)

{

$

(

"

p

"

)

.

css

(

"

color

"

,

"

red

"

)

;

}

)

;

Try it »


Definition and Usage

The css() method sets or returns one or more style properties for the selected elements.

**When used to return a property:
**This method returns the specified CSS property value of the first matched element.
However, shorthand CSS properties (such as "background" and "border") are not fully supported, and may give different results in different browsers when used to return property values.

When used to set a property:
This method sets the specified CSS property for all matched elements.


Syntax

Return a CSS property value:

$(

selector

).css(

property

)

Set a CSS property and value:

$(

selector

).css(

property,value

)

Set a CSS property and value using a function:

$(

selector

).css(

property,

function

(index,currentvalue)

)

Set multiple properties and values:

$(

selector

).css({

property: value

,

property: value

, ...})

Parameters Description
property Specifies the CSS property name, such as "color", "font-weight", etc.
value Specifies the value of the CSS property, such as "red", "bold", etc.
function(index,currentvalue) Specifies a function that returns the new value of the CSS property.
  • index - Returns the index position of the element in the set.
  • currentvalue - Returns the current value of the CSS property.
  • |

    More examples

    Return a CSS property value
    Returns the specified CSS property value of the first matched element.

    Set a CSS property and value using a function
    How to use a function to change CSS properties for selected elements.

    Set multiple CSS property/value pairs
    How to set multiple CSS properties and values for selected elements.


    jQuery HTML/CSS methods jQuery HTML/CSS Methods