Example

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.


Return CSS Property Value

Returns the specified CSS property value of the FIRST matched element.

Note: When used to return a value, shorthand CSS properties (like "background" and "border") are not supported.

Syntax

$(selector).css(name)
Try it yourself »

Parameter Description
name Required. Specifies the name of a CSS property. This parameter can contain any CSS property. Like "color"


Set CSS Property

Sets the specified CSS property for ALL matched elements.

Syntax

$(selector).css(name,value)
Try it yourself »

Parameter Description
name Required. Specifies the name of a CSS property. This parameter can contain any CSS property. Like "color"
value Optional. Specifies the value of the CSS property. This parameter can contain any CSS property value. Like "red".

If an empty string value is set, it removes the specified property from the element.


Set CSS Property Using a Function

Using a function to set the specified CSS property for ALL matched elements.

Syntax

$(selector).css(name,function(index,oldvalue))
Try it yourself »

Parameter Description
name Required. Specifies the name of a CSS property. This parameter can contain any CSS property. Like "color"
function(index,oldvalue) Specifies a function that returns the new value for the CSS property.
  • index - Optional. Receives the index position of the selector
  • oldvalue - Optional. Receives the current value of the CSS property


Set Multiple CSS Property/Value Pairs

Set one or more CSS property/value pairs for the selected elements.

Syntax

$(selector).css({property:value, property:value, ...})
Try it yourself »

Parameter Description
{property:value, property:value, ...} Required. Specifies the name and value of one or more CSS properties. Like {"color":"red","font-weight":"bold"}