How can I select all children of an element except the last child?

Css SelectorsCss

Css Selectors Problem Overview


How would I select all but the last child using CSS3 selectors?

For example, to get only the last child would be div:nth-last-child(1).

Css Selectors Solutions


Solution 1 - Css Selectors

You can use the negation pseudo-class :not() against the :last-child pseudo-class. Being introduced CSS Selectors Level 3, it doesn't work in IE8 or below:

:not(:last-child) { /* styles */ }

Solution 2 - Css Selectors

Make it simple:

You can apply your style to all the div and re-initialize the last one with :last-child:

for example in CSS:

.yourclass{
    border: 1px solid blue;
}
.yourclass:last-child{
    border: 0;
}

or in SCSS:

.yourclass{
    border: 1px solid rgba(255, 255, 255, 1);
    &:last-child{
        border: 0;
    }
}
  • easy to read/remember
  • fast to execute
  • browser compatible (IE9+ since it's still CSS3)

Solution 3 - Css Selectors

Nick Craver's solution works but you can also use this:

:nth-last-child(n+2) { /* Your code here */ }

Chris Coyier of CSS Tricks made a nice :nth tester for this.

Solution 4 - Css Selectors

When IE9 comes, it will be easier. A lot of the time though, you can switch the problem to one requiring :first-child and style the opposite side of the element (IE7+).

Solution 5 - Css Selectors

Using nick craver's solution with selectivizr allows for a cross browser solution (IE6+)

Solution 6 - Css Selectors

.nav-menu li:not(:last-child){
    // write some style here
}

this code should apply the style to all

  • except the last child

  • Solution 7 - Css Selectors

    to find elements from last, use

    <style>
    ul li:nth-last-of-type(3n){ color:#a94442}  /**/
    </style>
    

    Solution 8 - Css Selectors

    Nick Craver's solution gave me what I needed but to make it explicit for those using CSS-in-JS:

    const styles = {
      yourClass: {
        /* Styles for all elements with this class */
        '&:not(:last-child)': {
          /* Styles for all EXCEPT the last element with this class */
        },
      },
    };
    

    Solution 9 - Css Selectors

    There is a:not selector in css3. Use :not() with :last-child inside to select all children except last one. For example, to select all li in ul except last li, use following code.

    ul li:not(:last-child){   }
    

    Solution 10 - Css Selectors

    If you're using it within the nesting of the parent then the easiest way is:

    &:not(:last-child){
      ....
    }
    

    Example:

    .row {  //parent
     ...
     ...
     ...
     &:not(:last-child){
      ....
     }
    }
    

    Attributions

    All content for this solution is sourced from the original question on Stackoverflow.

    The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

    Content TypeOriginal AuthorOriginal Content on Stackoverflow
    QuestionRyanScottLewisView Question on Stackoverflow
    Solution 1 - Css SelectorsNick CraverView Answer on Stackoverflow
    Solution 2 - Css SelectorsSebastien HorinView Answer on Stackoverflow
    Solution 3 - Css SelectorsRobin BView Answer on Stackoverflow
    Solution 4 - Css SelectorsNicholas WilsonView Answer on Stackoverflow
    Solution 5 - Css SelectorsdelphiView Answer on Stackoverflow
    Solution 6 - Css SelectorsRyanView Answer on Stackoverflow
    Solution 7 - Css SelectorsAvinash MalhotraView Answer on Stackoverflow
    Solution 8 - Css SelectorsmaddRobotView Answer on Stackoverflow
    Solution 9 - Css SelectorsKrish MalhotraView Answer on Stackoverflow
    Solution 10 - Css SelectorsRavi WrayView Answer on Stackoverflow