How to select all children of an element except the last child using CSS?
When designing and developing web applications, sometimes we need to select all the child elements of an element except the last element. To select all the children of an element except the last child, use :not and :last-child pseudo classes.
Syntax:
element:not(:last-child) {
// CSS Style
}
Example 1: This example creates a navigation menu separated by right border except the last element.
<!DOCTYPE html> <html> <head> <title> Example to select all children except the last </title> <!-- CSS style to create nav bar --> <style> nav { margin: 30px; } nav a { text-transform: capitalize; text-decoration: none; color: rgba(60, 60, 60); font-family: sans-serif; padding: 10px 10px; margin-top: 30px; width: 150px; text-align: center; display: inline-block; } nav a:not(:last-child) { border-right: 5px solid greenyellow; } </style> </head> <body> <nav> <a href="#">Home</a> <a href="#">About</a> <a href="#">Blog</a> <a href="#">Geeksforgeeks</a> <a href="#">Articles</a> <a href="#">Contact Me</a> </nav> </body> </html> |
Output:

Example 2: This example creates a navigation menu and uses some CSS property except the last element.
<!DOCTYPE html> <html> <head> <title> Example to select all children except the last </title> <!-- CSS style to create nav bar --> <style> nav { margin: 30px; } nav a { text-transform: capitalize; text-decoration: none; color: rgba(60, 60, 60); font-family: sans-serif; padding: 10px 10px; margin-top: 30px; width: 150px; text-align: center; display: inline-block; border: 2px solid black; border-radius: 5px; } nav a:not(:last-child) { background-color:greenyellow; } </style> </head> <body> <nav> <a href="#">Home</a> <a href="#">About</a> <a href="#">Blog</a> <a href="#">Geeksforgeeks</a> <a href="#">Articles</a> <a href="#">Contact Me</a> </nav> </body> </html> |
Output:

Recommended Posts:
- How to select all child elements recursively using CSS?
- How to remove parent element except its child element using jQuery ?
- How to check if an element has any children in JavaScript ?
- How to get the child element of a parent using JavaScript ?
- How to Check if an element is a child of a parent using JavaScript?
- How to set the value of a select box element using JavaScript?
- How to select an element by name with jQuery?
- How to add options to a select element using jQuery?
- How to set the default value for an HTML <select> element?
- How to select a random element from array in JavaScript ?
- How to remove options from select element using jQuery ?
- How to select first element in the drop-down list using jQuery ?
- PHP | SimpleXMLElement children() Function
- HTML | DOM children Property
- What is the difference between children and childNodes in JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



