jQuery Get and Set CSS Properties


The css() function of jQuery is used to retrieve the calculated value of a CSS property or to set one or more CSS values for the specified components.

This approach provides a simple way to apply styles directly to HTML components that haven't been or can't readily be defined in a stylesheet (i.e. inline styles).


Get a CSS Property Value

Simply giving the property name as an argument to the css() function will return the calculated value of an element's CSS property. The fundamental syntax is as follows: 

Syntax

When a <div> element is clicked, the following example will fetch and show the calculated value of its CSS background-color property.

Example

Preview

Set a Single CSS Property and Value

For establishing a single CSS property for the elements, the css() function can take a property name and value as distinct arguments. The fundamental syntax is as follows:

Syntax

When the button is clicked, the CSS background-color property of the <div> elements is set to the color value blue.

Example

Preview

Set Multiple CSS Properties and Values

The css() function can also be used to set numerous CSS properties. The fundamental syntax for setting multiple properties for elements is as follows: 

Syntax

The following example will simultaneously set the background-color and padding CSS properties for the chosen items.

Example

Preview