jQuery css()Method

jQuery HTML/CSS 方法 jQuery HTML/CSS Methods

Examples

Set the color property of all <p> elements:

$("button").click(function(){ $("p").css("color","red"); });

Try it yourself »

Definition and Usage

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

When used to return properties:
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, when used to return property values, have different results in different browsers.

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


Syntax

Return the CSS property value:

$(selector).css(property)

Set CSS property and value:

$(selector).css(property,value)

Set 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 the CSS property value
Returns the specified CSS property value of the first matched element.

Set 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 方法 jQuery HTML/CSS Methods

Other Extensions