jQuery css() Method Last Updated : 07 Jul, 2023 Comments Improve Suggest changes Like Article Like Report The css() method in jQuery is used to change the style property of the selected element. This method can be used in different ways. The css() method can be used to get/return the present value of the property for the selected element. Syntax: $(selector).css(property)or $(selector).css(property, value)or $(selector).css(property, function(index, currentvalue))or $(selector).css({property:value, property:value, ...})Return value: It will return the value of the property for the selected element. Example 1: In this example, we will use css() method to get the text color of the paragraph element. HTML <!DOCTYPE html> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <script> $(document).ready(function () { // Here selecting button element $("button").click(function () { // When the button is clicked, css() method // will return the value using alert method alert($("p").css("color")); }); }); </script> </head> <body> <button> Click here and it will return the color value of p element </button> <p style="color:green"> Welcome to gfg! </p> </body> </html> Output: Example 2: In this example, we will use css() method to change the CSS style of the selected element. HTML <!DOCTYPE html> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <script> $(document).ready(function () { // Selecting button element $("button").click(function () { // When the button is clicked css() method // will change the color of paragraph $("p").css("color", "green"); }); }); </script> </head> <body> <button> Click here and it will change the color of paragraph element </button> <p style="border: 2px solid green; color:red;padding:5px"> Wecome to gfg!. </p> </body> </html> Output: Example 3: In this example, we will use css() method to add styles using the function. HTML <!DOCTYPE html> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <script> $(document).ready(function () { $("button").click(function () { $("p").css("padding", function (h) { return h + 30; }); }); }); </script> </head> <body> <button> Click here and the padding will change. </button> <p style="border: 2px solid green; color: green; padding: 5px;"> Welcome to gfg!. </p> </body> </html> Output: Example 4: In this example, we will use css() method to apply multiple CSS properties simultaneously. HTML <!DOCTYPE html> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <script> $("button").click(function () { // Applying more than one property at a time // Note: property name written in camelCase $("p").css({ "backgroundColor": "green", "color": "white", "fontSize": "20px" }); }); </script> </head> <body> <p style="border: 2px solid green; color:green;padding:5px;"> Welcome to gfg!. </p> <button>Apply css</button> </body> </html> Output: Comment More infoAdvertise with us Next Article jQuery detach() Method K kundankumarjha Follow Improve Article Tags : Web Technologies JQuery jQuery-Methods Similar Reads jQuery addClass() Method The addClass() method is an inbuilt method in jQuery that is used to add more properties to each selected element. It can also be used to change the property of the selected element. This method can be used in two different ways: 1) By adding a Class name directly: Here, the Class name can be used d 2 min read jQuery after() Method The after() method is an inbuilt function in jQuery which is used to insert content, specified by the parameter for each selected element in the set of matched elements. Syntax: $(selector).after(A);Parameter: It accepts a parameter "A" which is either a content or function passed to the method. Ret 1 min read jQuery append() Method The append() method in jQuery is used to insert some content at the end of the selected elements. Syntax: $(selector).append( content, function(index, html) ) Parameters: This method accepts two parameters as mentioned above and described below: content: It is a required parameter and is used to spe 2 min read jQuery appendTo() Method The appendTo() method is an inbuilt method in jQuery that is used to insert HTML element at the end of the selected element. Syntax: $(content).appendTo(selector)Here the element content specifies the content to be inserted. Parameters: It accepts a parameters "selector" that specifies the elements 1 min read jQuery attr() Method The attr() method in jQuery is used to set or return the attributes and values of the selected elements. Syntax: To return the value of an attribute:$(selector).attr(attribute)To set the attribute and value:$(selector).attr(attribute, value)To set attribute and value using a function:$(selector).at 3 min read jQuery before() Method The before() Method in jQuery is used to add the content before the selected element. Syntax: $(selector).before( content, function(index) )Parameters: This method accepts two parameters as mentioned above and described below: content: This parameter holds the content to be inserted before the eleme 2 min read jQuery clone() Method The clone() method is an inbuilt method in jQuery that is used to make a copy of selected elements including its child nodes, text, and attributes. Syntax: $(selector).clone(true|false)Parameters: This method accepts an optional parameter that could be either true or false specifying whether the eve 2 min read jQuery css() Method The css() method in jQuery is used to change the style property of the selected element. This method can be used in different ways. The css() method can be used to get/return the present value of the property for the selected element. Syntax: $(selector).css(property)or $(selector).css(property, val 2 min read jQuery detach() Method The detach() is an inbuilt method in jQuery that removes the selected elements from the DOM tree including all text and child nodes but it keeps the data and the events. Document Object Model (DOM) is a World Wide Web Consortium standard. This defines accessing elements in the DOM tree. Syntax: $(se 1 min read jQuery empty() Method The empty() method is an inbuilt method in jQuery that is used to remove all child nodes and their content for the selected elements. Syntax: $(selector).empty()Parameter: This method does not accept any parameter. Return Value: This method returns the selected element with the specified changes mad 1 min read Like