Table of Contents
Method 1 using:last-child() selector:
grammar
Example
Method 2: Use :nth-last-child() selector
How to use JavaScript
in conclusion
Home Web Front-end CSS Tutorial How to select the 'last child' of a specific category using CSS?

How to select the 'last child' of a specific category using CSS?

Aug 29, 2023 am 11:13 AM

如何使用 CSS 选择特定类别的“最后一个孩子”?

CSS or Cascading Style Sheets is an important part of modern web development, allowing web developers to add styles to their websites and create visually appealing designs. Sometimes we want to select the last child element of a specific class using CSS, but how to achieve this? There are different ways to select the last child element.

In this article, we will see how to select the last child element with a specific class using CSS.

Method 1 using:last-child() selector:

:The last-child() selector method is the first method available in CSS. This selector is used to select the last child element of its parent element. The process of selecting the last child element is very simple. To select the last child element with a specific class, we use the :last-child selector in combination with the class selector.

Check out the syntax below to learn how to use the last-child selector to select the last element.

grammar

In this syntax, let's say we have a list of items with class name "item" and we want to style the last item in the list differently, in order to do this we can use The following CSS code-

.item:last-child {
   /* Add your CSS styles here */
}
Copy after login

The above syntax will select the last child element with the "item" class and apply the specified style to it.

Example

In this example, we use the :last-child() selector to select the last div element with the class name "item" and add CSS styles to it, such as background color green and padding 10px.

<html>
<head>
   <title>Program to select the last child using :last-child() selector</title>
   <style>  
      /* Using the :last-child Selector */
      .item:last-child {
         background-color: green;
         padding: 10px;
      }
   </style>
</head>
<body>
   <h2>Method: Using the :last-child Selector</h2>
   <div class="item">First Item</div>
   <div class="item">Second Item</div>
   <div class="item">Third Item</div>
   <div class="item">Fourth or Last Item</div>
</body>
</html>
Copy after login

Method 2: Use :nth-last-child() selector

This is the second way to select the last child element with a specific class in CSS. The :nth-last-child selector is used to select elements based on their position relative to the end of the parent element.

The work of this selector is very simple. To select the last child of a specific class with the help of :nth-last-child selector, we have to combine it with the class selector and set the value to 1. See the following syntax for more details on how nth-last is used - the child selector is used to select the last element.

grammar

In this syntax, assuming we have a series of div elements with the class name "div-box", and now to style the last div element in this series, we can use the following CSS code:

.div-box:nth-last-child(1) {
   /* Add your CSS styles here */
}
Copy after login

The above syntax will select the last child element with the class name "div-box" and apply the specified style to it.

Example

The above syntax will select the last child element with the class name "div-box" and apply the specified style to it.

<html>
<head>
   <title>Program to select the last child using :nth-last-child() selector</title>
   <style>
      /* Using the :last-child Selector */
      .item:nth-last-child(1) {
         background-color: lightblue;
         padding: 10px;
      }
   </style>
</head>
<body>
   <h2>Method: Using the :nth-last-child Selector</h2>
   <div class="item">First Item</div>
   <div class="item">Second Item</div>
   <div class="item">Third Item</div>
   <div class="item">Fourth or Last Item</div>
</body>
</html>
Copy after login

How to use JavaScript

In this approach we use JavaScript to select the last child with a specific class. While CSS is the preferred method for styling web pages, JavaScript on the other hand can also be used to perform Document Object Model operations and dynamically select HTML elements.

In JavaScript, the querySelector() method is used with the class selector and the :last-child selector to select the last child with a specific class.

The process of selecting the last child using JavaScript is also very simple. To select the last child of a specific class with the help of javascript, we have to combine it with the :last-child-selector of the class and CSS. See the following syntax for more details on how to select the last element using javascript.

grammar

In this syntax, we have a series of div elements with "div" class, and we want to add "add-color" class to the last div in the list, we can use the following JavaScript code -

ar lastDiv = document.querySelector('.div:last-child');
lastDiv.classList.add(add-color);
Copy after login

The above syntax will select the last child element with the "div" class and add the "add-color" class to it.

Example

In this example, we define a list of div elements with the class name "item", and then we also add some CSS styles. Now to select the last child element we define a class called "highlight" and twitch will style the last div element.

Here, we use the querySelector() method in JavaScript, which selects the last child element with "class" as "item". Then, we also added the "highlight" class to the selected element using the classList.add() method.

<html>
<head>
   <title>Program to select the last child using JavaScript</title>
   <style>
      /* Styling for divs */
      .item {
         background-color: #f1f1f1;
         padding: 10px;
         margin-bottom: 10px;
      }
      /* Styling for last div */
      .highlight { background-color: yellow;}
   </style>
</head>
<body>
   <h2>Method: Using JavaScript</h2>
   <div class="item">First Item</div>
   <div class="item">Second Item</div>
   <div class="item">Third Item</div>
   <div class="item">Fourth or Last Item</div>
   <script>
   
      // Selecting the last div element with class "item"
      var lastDiv = document.querySelector('.item:last-child');
      
      // Adding the "highlight" class to the last div element to change its background color
      lastDiv.classList.add('highlight');
   </script>
</body>
</html>
Copy after login

in conclusion

Selecting the last child element with a specific class using CSS is a task that can be achieved in different ways. The :last-child() selector and the :nth-last-child() selector are two methods that can be used to accomplish this task.

:The last-child() selector is used to select the last child element of its parent element. To select the last child element with a specific class, we use the :last-child selector in conjunction with the class selector. On the other hand, the :nth-last-child() selector is used to select elements based on their position relative to the end of the parent element. To select the last child element of a specific class using the :nth-last-child selector, we have to combine it with the class selector and set the value to 1.

In summary, both methods are effective for selecting the last child element with a specific class, and which method to use depends on the specific needs of the web developer. By using these selectors, web developers can enhance the appearance and functionality of their websites to make them more attractive to users.

The above is the detailed content of How to select the 'last child' of a specific category using CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

While You Weren't Looking, CSS Gradients Got Better While You Weren't Looking, CSS Gradients Got Better Apr 11, 2025 am 09:16 AM

One thing that caught my eye on the list of features for Lea Verou&#039;s conic-gradient() polyfill was the last item:

A Comparison of Static Form Providers A Comparison of Static Form Providers Apr 16, 2025 am 11:20 AM

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

How to Build Vue Components in a WordPress Theme How to Build Vue Components in a WordPress Theme Apr 11, 2025 am 11:03 AM

The inline-template directive allows us to build rich Vue components as a progressive enhancement over existing WordPress markup.

The Three Types of Code The Three Types of Code Apr 11, 2025 pm 12:02 PM

Every time I start a new project, I organize the code I’m looking at into three types, or categories if you like. And I think these types can be applied to

PHP is A-OK for Templating PHP is A-OK for Templating Apr 11, 2025 am 11:04 AM

PHP templating often gets a bad rap for facilitating subpar code — but that doesn&#039;t have to be the case. Let’s look at how PHP projects can enforce a basic

See all articles