Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • CSS Tutorial
  • CSS Exercises
  • CSS Interview Questions
  • CSS Selectors
  • CSS Properties
  • CSS Functions
  • CSS Examples
  • CSS Cheat Sheet
  • CSS Templates
  • CSS Frameworks
  • Bootstrap
  • Tailwind
  • CSS Formatter
Open In App
Next Article:
How to select "last child" with a specific class using CSS ?
Next article icon

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:

 

Next Article
How to select "last child" with a specific class using CSS ?

G

geetanjali16
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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences