How to apply css property to a child element using JQuery? Last Updated : 23 Sep, 2021 Comments Improve Suggest changes Like Article Like Report The task is to apply CSS property to a child element using jQuery. To do that, first we select the child element with the help of children() method in jQuery and then apply CSS property to it with the help of css() method in jQuery. Syntax: // Note : children method selects the direct child to parent $(selector).children("selector").css("property-name","value") Example 1 HTML <! DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title> How to apply CSS property to a child element using JQuery? </title> <!-- Link of JQuery cdn --> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> </head> <body> <div class="parent"> <h2 class="child-1">i am child-1.</h2> <p class="child-2">i am child-2.</p> </div> <button>Apply css</button> <script> $("button").click(function () { // select div element which is the parent // select first child(h2) and apply one // or more css property at a time $("div").children("h2").css({ "backgroundColor": "black", "color": "white" }); // apply one property at a time, use // property name just like css and // then select second child element(p) $("div").children("p").css("background-color", "red"); $("div").children("p").css("color", "white"); }); </script> </body> </html> Output: Before Clicking the Button: After Clicking the Button: We can also apply CSS property to grand-child and any descendants to parent with the help of find() method in jQuery. Syntax: $(selector).find("descendants-name").css("property-name","value"); Example 2: HTML <! DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title> How to apply CSS property to a child element using JQuery? </title> <!-- Link of JQuery cdn --> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> </head> <body> <div class="parent"> <h2 class="child-1">i am child-1.</h2> <p class="child-2">i am child-2. <span>i am grand-child-1</span> </p> </div> <button>Apply css</button> <script> $("button").click(function () { // select div element which is the parent // select first child(h2) and apply one // or more css property at a time $("div").find("h2").css({ "backgroundColor": "black", "color": "white" }); // Apply one property at a time, use // property name just like css // and then select second child element(p) $("div").find("p").css("background-color", "red"); $("div").find("p").css("color", "white"); // Apply on grand-child $("div").find("span").css({ "backgroundColor": "black", "color": "white" }); }); </script> </body> </html> Output: Before clicking the button: After clicking the button: Comment More infoAdvertise with us Next Article How to apply css property to a child element using JQuery? N nikhilchhipa9 Follow Improve Article Tags : Web Technologies HTML CSS JQuery CSS-Misc HTML-Misc jQuery-Misc +3 More Similar Reads How to add and remove CSS classes to an element using jQuery ? In this article, we will see how to add or remove the CSS classes to an element using jQuery. To add the CSS classes to an element we use addClass() method, and to remove the CSS classes we use removeClass() method. Syntax: The syntax for adding CSS classes to an element: $('selector').addClass(clas 2 min read How to apply CSS in even childs of parent node using jQuery ? In this article, we will see how to set styles in even the child of the parent element using jQuery. To set the style on the even child of the parent, we use jQuery :nth-child(even) selector. The :nth-child(even) selector is used to select every element that is the even child of its parent element. 1 min read How to add CSS properties to an element dynamically using jQuery ? In this article, we will see how to add some CSS properties dynamically using jQuery. To add CSS properties dynamically, we use css() method. The css() method is used to change the style property of the selected element. The css() method can be used in different ways. This method can also be used to 1 min read How to apply styles on an element using jQuery ? In this article, we will learn how one can apply a style on an element using jQuery. Suppose you are building a website or game where you need the user interface to be interactive. In such cases styles can play a huge role there in this post we are going to learn how can we change styles when certai 3 min read How to apply CSS in a particular div element using jQuery ? In this article, we will set the CSS of a div element using jQuery. We will use the CSS property to set the CSS.Approach: We will create a div element with some text and hyperlinks. Then we will use jQuery to apply CSS to the div element. jQuery comes with the function .css() where we can get and se 2 min read Like