jQuery css() Method

The jQuery css() method can be used to return or set the CSS properties for the selected elements. You can use this method to add single and multiple CSS properties.

jQuery css() Method

Syntax of css() Method

The syntax of this method is given below:-

Get or Return CSS Property
$(selector).css(“propertyname”);
Set CSS Property
$(selector).css(“propertyname”, “value”)
Set Multiple CSS properties
$(selector).css({“propertyname1″:”value1”, “propertyname2″:”value2″,…”propertynameN”:”valueN”})

Description of the Parameters

The description of the parameters are given below.

Parameter Name Description
selector Specify the selector for selecting the element and return and set the CSS property. It can be done by using the tag name, class name, or id of the elements to select.
propertyname It is the name of the CSS property you want to select or return you want to add to the element. It can be single or multiple with spaces.
value An index returns the position of the selected element.

jQuery css() Method Get or Return CSS Property of an Element

If you want to get the value of CSS property, you can use this method as given in the example below. The below example contains an added style to the paragraph element. The method returns the value of the CSS property ‘border’ in the alert box.

Example

Test it Live

Output


This is the paragraph.

You have to click the button given above to return the value of specified CSS property. It displays an alert box showing the value of the CSS property border.

Set CSS Property Using jQuery css() Method

The jQuery css() method also sets the CSS property of the selected element. You have to specify the CSS property with its value to add to the element.

Example

Test it Live

Output


This is the paragraph.

Click the button given above to add the CSS property border with its value. You can find the added border after you click the button given above.

How to Set Multiple CSS Property of the Selected Element

In addition to all these, you can also set multiple CSS properties for the selected element. See the example given below to specify the multiple CSS properties and add to the element.

Example

Test it Live

Output


This is the paragraph.

When you click the button above, it adds three CSS properties to the selected element. See the output showing the added CSS using the jQuery css() method.

You may also like to read

I hope you like this tutorial on jQuery css() method. If you have any queries regarding the tutorial, please comment below.

References