jQuery css()

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

jQuery css()

The jQuery CSS() method is used to get (return)or set style properties or values for selected elements. It facilitates you to get one or more style properties.

jQuery CSS() method provides two ways:

1) Return a CSS property

It is used to get the value of a specified CSS property.

Syntax:

  1. css("propertyname");  

Let's take an example to demonstrate this property.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
  5. <script>  
  6. $(document).ready(function(){  
  7.     $("button").click(function(){  
  8.         alert("Background color = " + $("p").css("background-color"));  
  9.     });  
  10. });  
  11. </script>  
  12. </head>  
  13. <body>  
  14. <h2>This is a heading</h2>  
  15. <p style="background-color:#ff0000">The background-color of this paragraph is red.</p>  
  16. <p style="background-color:#00ff00">The background-color of this paragraph is green.</p>  
  17. <p style="background-color:#0000ff">The background-color of this paragraph is blue.</p>  
  18. <button>Click here to get the background-color of first matched element</button>  
  19. </body>  
  20. </html>   

Test it Now

Output:

This is a heading

The background-color of this paragraph is red.

The background-color of this paragraph is green.

The background-color of this paragraph is blue.

Click here to get the background-color of first matched element

Note: The above example returns the background-color value of the first matched element.

2) Set a CSS property

This property is used to set a specific value for all matched element.

Syntax:

  1. css("propertyname","value");     
  2. <!DOCTYPE html>  
  3. <html>  
  4. <head>  
  5. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
  6. <script>  
  7. $(document).ready(function(){  
  8.     $("button").click(function(){  
  9.         $("p").css("background-color", "violet");  
  10.     });  
  11. });  
  12. </script>  
  13. </head>  
  14. <body>  
  15. <p style="background-color:#ff0000">The background-color of this paragraph is red.</</p>  
  16. <p style="background-color:#00ff00">The background-color of this paragraph is green.</</p>  
  17. <p style="background-color:#0000ff">The background-color of this paragraph is blue.</</p>  
  18. <p>This paragraph has no background-color. </p>  
  19. <button>Click here to set a specific background-color of all matched element</button>  
  20. </body>  
  21. </html>  

Test it Now

Output:

The background-color of this paragraph is red.

The background-color of this paragraph is green.

The background-color of this paragraph is blue.

Click here to set a specific background-color of all matched element

3) Set multiple CSS properties

It is just an extension of Set CSS property. It facilitates you to add multiple property values together.

Syntax:

  1. css({"propertyname":"value","propertyname":"value",...});    

Let's take an example to demonstrate this property. In this example we add two properties background-color and font-size for all element.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
  5. <script>  
  6. $(document).ready(function(){  
  7.     $("button").click(function(){  
  8.         $("p").css({"background-color": "yellow", "font-size": "200%"});  
  9.     });  
  10. });  
  11. </script>  
  12. </head>  
  13. <body>  
  14. <h2>This is a heading</h2>  
  15. <p style="background-color:#ff0000">The background-color of this paragraph is red.</p>  
  16. <p style="background-color:#00ff00">The background-color of this paragraph is green.</p>  
  17. <p style="background-color:#0000ff">The background-color of this paragraph is blue.</p>  
  18. <p>This paragraph has no background-color.</p>  
  19. <button>Click here to set multiple styles for all selected elements.</button>  
  20. </body>  
  21. </html>  

Test it Now

Output:

The background-color of this paragraph is red.

The background-color of this paragraph is green.

The background-color of this paragraph is blue.

Click here to set multiple styles for all selected elements