BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

jQuery css()

By Chaitanya Singh | Filed Under: jQuery

The jQuery css() method is used to get or set style properties of selected elements.

When css() method is used to return style properties:
It returns the css property value of the first matched element.

Syntax:

$(selector).css(property)

When css() method is used to set style properties:
It sets the specified css property values for all the matched elements (selected elements).

Syntax:

$(selector).css(property,value)

Example 1: jQuery css() – get css property value

In this example, we are using css() method to get the css property value of the matched element. We already discussed in the beginning that css() returns the css property value for first matched element.

Here we have three paragraphs. All of them have different background color css property value. When we selected the p elements and used css() method on it. The method returned the value red, which is the background color of first paragraph.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
  alert("Background color of first matched element: " + $("p").css("background-color"));
});
});
</script>
</head>
<body>
<p style="background-color:red">This is the first paragraph.</p>
<p style="background-color:grey">This is the second paragraph.</p>
<p style="background-color:green">This is the third paragraph.</p>
<button>Get the first paragraph background color</button>
</body>
</html>

Output:
Before button click:
jQuery css()
After button click: The alert message shows the color as rgb(255, 0, 0) which is a representation of color red.
jQuery css()

Example 2: jQuery css() – set css property value

In this example, we are using the css() method to set the css property value. When used for setting up value, this method set specified css for all matched elements.

Here we have three paragraphs, just like the above example. We are changing the font color of these paragraphs using css(). In this case css property is “color” and css property value is “green”. In the output screenshot, you can see that font color changed to “green” for all the p elements.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").css("color", "green");
  });
});
</script>
</head>
<body>
<p><b>This is the first paragraph.</b></p>
<p><b>This is the second paragraph.</b></p>
<p><b>This is the third paragraph.</b></p>
<button>Set the font color of all paragraphs</button>
</body>
</html>

Output:
Before button click:
jQuery css() Method
After button click:
jQuery css() Method

Example 3: jQuery css() – set multiple css properties and value pairs

Here, we are using css() to set multiple css properties and value pairs. The css properties in this example are “color” & “font-size” and their values are “green” & “150%” respectively.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").css({"color": "green", "font-size": "150%"});
  });
});
</script>
</head>
<body>
<p><b>This is the first paragraph.</b></p>
<p><b>This is the second paragraph.</b></p>
<p><b>This is the third paragraph.</b></p>
<button>Set the values for multiple css properties</button>
</body>
</html>

Output:
Before button click:
jQuery css() set multiple properties
After button click: As you can see both the font color and font size has been changed to the values set by css() method.
jQuery css() set multiple properties

❮ jQuery val()

jQuery Tutorial

  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events

jQuery Effects

  • jQuery Show & Hide
  • jQuery Fading
  • jQuery Sliding
  • jQuery Animate
  • jQuery stop()
  • jQuery Callback Function
  • jQuery Chaining

jQuery HTML/CSS

  • jQuery html()
  • jQuery addClass()
  • jQuery hasClass()
  • jQuery removeClass()
  • jQuery toggleClass()
  • jQuery after()
  • jQuery before()
  • jQuery append()
  • jQuery appendTo()
  • jQuery clone()
  • jQuery insertBefore()
  • jQuery insertAfter()
  • jQuery attr()
  • jQuery text()
  • jQuery val()
  • jQuery css()
  • jQuery prepend()
  • jQuery remove()
  • jQuery empty()
  • jQuery detach()

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap