W3Schools.com

jQuery CSS css() Method

jQuery CSS Methods jQuery CSS Methods

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"}


jQuery CSS Methods jQuery CSS Methods


WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Best Hosting Coupons
UK Reseller Hosting
Cloud Hosting
Top Web Hosting
$3.98 Unlimited Hosting
Premium Website Design
WEB BUILDING
XML Editor - Free Trial!
FREE Website BUILDER
Best Website Templates Top CSS Templates
CREATE HTML Websites
EASY WEBSITE BUILDER
W3SCHOOLS EXAMS
Get Certified in:
HTML, CSS, JavaScript, XML, PHP, and ASP
W3SCHOOLS BOOKS
New Books:
HTML, CSS
JavaScript, and Ajax
STATISTICS
Browser Statistics
Browser OS
Browser Display
SHARE THIS PAGE