How to select all children of an element except the last child using CSS?


CSS is a commonly used to style web pages. While developing a web app, we sometimes require to apply different styles to different elements. And this common requirement is to select all children of an element except for the last child! Applying CSS makes it possible to select all children of an element except the last child.

In this article, we’ll see how to select all children of an element except the last child in CSS with different approaches. We will be discussing each approach in detail with its syntax, and examples that will help you understand how they work.

Out first approach to select all child except the last one involves using the :not() selector, which allows you to select all elements that do not match a specific selector. The second approach involves using the :nth-last-child() selector, which selects all elements that are the nth child from the end of the parent element. In this method, we specify the n+2 argument that helps in selecting all children of an element except for the last child in HTML.

Both approaches are very useful when developing the web application and they may help in achieving the desired UI of our web pages. Using the selectors discussed above can help us know how these selectors work, and we can create customized styles for our web pages that are both functional and UI.

Approach 1: Using the :not selector

The − not selector is a powerful CSS selector that allows you to select all elements except the ones that match a certain condition. To select all children of an element except the last child, we can use the :not selector in combination with the :last-child selector.

To use the − not() selector to select all children of an element except for the last child, we use it with the :last-child selector. The :last-child selector helps in selecting the last child of an element. By using this combination of approaches to the two selectors, we can select all the children of an element except for the last child.

Below is the syntax of how to use the :not() selector to select all the children of a parent element except for the last child −

Syntax

In the below syntax, .parent is the selector for the parent element, > selects all of its direct children, * selects all of the children, and :not(:last-child) excludes the last child from the selection.

.parent > *:not(:last-child) {
  /* styles for all children except the last one */
}

Example

In this example, the CSS selector .parent > *:not(:last-child) selects all the immediate children of the .parent class element (i.e., h1, h2, p, and span elements), except for the last child i.e., element. Using CSS, we have applied the ‘color’ property to the first three children of the .parent element (i.e., the h1, h2, and p elements) while the fourth child i.e., element will not be affected.

Let's now see the code on how to apply the CSS code to an HTML page −

<!DOCTYPE html>
<html >
   <head>
      <style>
         .parent > *:not(:last-child) {
            color: green;
         }
      </style>
   </head>
   <body>
      <div class="parent">
         <h1>First Child</h1>
         <h2>Second Child</h2>
         <p>Third Child</p>
         <span>Fourth Child</span>
      </div>
   </body>
</html>

Approach 2: Using the:nth-last-child() selector

The − nth-last-child() selector is another useful tool in CSS that allows you to select elements based on their position in the list of children of an element. We can use it to select all children except for the last one by selecting all but the last child using :nth-last-child(n+2).

To select all children of an element except for the last child using the :nth-last-child() selector, we can specify the n+2 argument. The defined argument selects all elements of the HTML using CSS except for the last child, which is the first child from the end.

Below is the syntax of how to use the :nth-last-child() selector to select all the children of a parent element except for the last child −

Syntax

In the below syntax, .parent is the selector for the parent element, > selects all of its direct children, * selects all of the children, and :nth-last-child(n+2) selects all but the last child.

.parent > *:nth-last-child(n+2) {
   /* styles for all children except the last one */
}

Example

In this example, the CSS selector .parent > *:nth-last-child(n+2) selects all its immediate children of the .parent class element, except for the last child. Here, all the button elements inside the .parent element will be selected, but not the last button element.

Several CSS properties have been applied to the first three buttons but the CSS styles will not be added to the last child.

Let's now see the code on how to apply the CSS code to an HTML page −

<!DOCTYPE html>
<html >
   <head>
      <style>
         .parent > *:nth-last-child(n+2) {
            background-color: green;
            padding: 10px;
            color: white;
            border-radius: 10px;
            width: 10em;
            text-decoration: none;
            font-family: sans-serif;
            border: none;
         }
      </style>
   </head>
   <body>
      <div class="parent">
      <button>First</button>
      <button>Second</button>
      <button>Third</button>
      <button>Fourth</button>
   </div>
   </body>
</html>

Conclusion

In this article, we saw how to select all children of an element except for the last child with different approaches. With the help of CSS selectors, selecting the children is a hassle-free process as it provides multiple selectors that can be used to perform this task such as the :not() selector and the :nth-last-child() selector. Both the examples discussed above demonstrate that how CSS is useful in applying the different styles to all children of an element except for the last one. Both of these selectors help us in applying different CSS styles to different HTML elements easily and not only this, but it also helps in creating dynamic websites with ease.

Updated on: 13-Apr-2023

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements