css() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:css

Description

The css() method sets or gets one or more style properties for the selected elements.

Parameter

ParameterDescription
property CSS property name, like "color", "font-weight", etc.
valuevalue of the CSS property, like "red", "bold", etc.
function(index,currentvalue) a function that returns the new value for the CSS property
  • index - Returns the index position of the element in the set
  • currentvalue - Returns the current value of the CSS property

The following code shows how to Set the color property of all <p> elements:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").css("color", "red");
    });/*from   w  w w  . j  a  v a 2  s . c  o  m*/
});
</script>
</head>
<body>

<button>Set the color property of all p elements</button>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

</body>
</html>

Related Tutorials