How to use css property and funtion in JQuery

In this article, I will explain use of css property and function in JQuery.
  • 2513

JQuery CSS css() Method

  • css() method sets or returns one or more style properties for the selected elements.

jQuery css({properties}) Method

  • css({properties}) method use to set one or more properties values.

$(selector).css({property:value, property:value, ...})

  • {property:value, property:value, ...} set the name and value of one or more CSS properties. Like {"color":"red","font-weight":"bold"}.

The following example shows how to css({properties}) method in jquery.

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

    $(document).ready(function () {

        $("button").click(function () {

            $("p").css({

                "color": "blue",

                "background-color": "red",

                "font-family": "Microsoft Sans Serif",

                "font-size": "25px",

                "padding": "5px"

            });

        });

    });

</script>

</head>

<body>

<p>First paragraph.</p>

<p>Second paragraph.</p>

<button style="border-left-style:solid">Click for set multiple css color property of all paragraph </button>

</body>

</html>

Output

 css property value pair.jpg

jQuery css(name,function(index,oldvalue)) Method

  • css(name,function()) set the specified CSS property for ALL matched elements.

Sysntax

$(selector).css(name,function(index,oldvalue))

  • name The name of the property to be set.
  • function(index,oldvalue) Specifies a function that returns the new value for the CSS property.

Example

The following example shows how to css(name,function()) method in jquery.

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

    $(document).ready(function () {

        $("button").click(function () {

            $("p").css("color", function () {

                return "blue";

            });

        });

    });

</script>

</head>

 

<body>

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

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

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

</body>

</html>

Output

 css property function.jpg

Further Readings

You may also want to read these related articles here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.