How to select "last child" with a specific class using CSS ? Last Updated : 08 May, 2023 Comments Improve Suggest changes Like Article Like Report In this article, we will learn to select the "last-child" with a specific class name in CSS. The last-child selector allows us to select the last element inside a set of siblings' elements within its defining element. the last-child selector is a pseudo-class that chooses the final member of the siblings in the block that contains it. Approach: The CSS :last-child selector is used to target the last child element of its parent for styling. Syntax: :last-child { // CSS property } Example 1: The following example shows an HTML div with four paragraphs. The first two paragraphs show a "grey" background-color and the last two paragraph shows a "yellow" background-color. HTML <!DOCTYPE html> <html> <head> <style> .greyClass { background-color: grey; } .yellowClass { background-color: yellow; } </style> </head> <body> <h2 style="color:green;"> GeeksforGeeks </h2> <b>Last child </b> <div id="divID"> <p class="greyClass"> This paragraph 1 is within greyClass class. </p> <p class="greyClass"> This paragraph 2 is within greyClass class. </p> <p class="yellowClass"> This paragraph 3 is within yellowClass class. </p> <p class="yellowClass"> This paragraph 4 is last child and within yellowClass class </p> </div> </body> </html> Output: Example 2: The following code shows selecting the last child with a specific class. Refer to the output which shows the last child with a "blue" color instead of having the color of "yellowClass" class. HTML <!DOCTYPE html> <html> <head> <style> .greyClass { background-color: grey; } .yellowClass { background-color: yellow; } .yellowClass:last-child { background-color: blue; } </style> </head> <body> <h2 style="color:green;"> GeeksforGeeks </h2> <b>Last child with class name</b> <div id="divID"> <p class="greyClass"> This paragraph 1 is within greyClass class. </p> <p class="greyClass"> This paragraph 2 is within greyClass class. </p> <p class="yellowClass"> This paragraph 3 is within yellowClass class. </p> <p class="yellowClass"> T his paragraph 4 is last child and within yellowClass class </p> </div> </body> </html> Output: Example 3: The following code shows another way of selecting the last child of the HTML div. HTML <!DOCTYPE html> <html> <head> <style> .greyClass { background-color: grey; } .yellowClass { background-color: yellow; } #paraID.yellowClass:last-child { background-color: blue; } </style> </head> <body> <h2 style="color:green;"> GeeksforGeeks </h2> <b>Last child with class name</b> <div id="divID"> <div id="greyDiv"> <p class="greyClass"> This paragraph 1 is within greyClass class. </p> <p class="greyClass"> This paragraph 2 is within greyClass class. </p> </div> <div id="yellowDiv"> <p class="yellowClass"> This paragraph 3 is within yellowClass class. </p> <p id="paraID" class="yellowClass"> This paragraph 4 is last child and within yellowClass class. </p> </div> </div> </body> </html> Output: Comment More infoAdvertise with us Next Article How to select "last child" with a specific class using CSS ? G geetanjali16 Follow Improve Article Tags : Web Technologies CSS CSS-Properties CSS-Questions Similar Reads How to find all children with a specified class using jQuery ? There are lots of javascript libraries like - anime.js, screenfull.js, moment.js, etc. JQuery is also a part of javascript libraries. It is used to simplify the code. It is a lightweight and feature-rich library. In this article, we will learn how to find all children with a specified class of each 2 min read How to get specific number of child elements using CSS? The :nth-child() CSS pseudo-class is used to style elements based on their position in a parent element's child list. This selector allows you to target elements in a specific position or match them based on patterns such as odd or even positions, or by using a linear equation.Syntax:element:nth-chi 2 min read How to use a not:first-child Selector in CSS? The :not(:first-child) selector in CSS targets all elements that are not the first child within their parent. It allows you to style elements except the first one, making it useful for applying styles to subsequent items in a list or layout.Syntax:not( element ) { // CSS property } Example: In this 2 min read How to Select all Child Elements Recursively using CSS? Here are three methods to achieve to Select all Child Elements Recursively using CSS:1. Using the Universal Selector (*)The universal selector applies styles to all child elements within a parent recursively.HTML<html> <head> <style> .parent * { color: blue; font-weight: bold; } 3 min read How to Select All Children of an Element Except Last Child using CSS? When designing and developing web applications, there are scenarios where you may want to apply styles to all child elements of a parent except the last one. For instance, you might want to add a border between items in a navigation menu but not include a border after the last item. This can be achi 2 min read Like