mshwf
mshwf

Reputation: 7469

How can I exclude last two child elements with CSS selector?

I want to draw bottom-border under some div elements inside their parent but except the last two, knowing that number of child element is changeable:

<div id="parent">
   <div>
One
   </div>
   <div>
Two
   </div>
   <div>
Three
   </div>
   <div>
Four
   </div>
</div>

Is it possible to select all child elements except the last two?

Upvotes: 4

Views: 5626

Answers (2)

Quentin
Quentin

Reputation: 944546

:nth-last-child should do the job (combined with :not).

#parent >:not(:nth-last-child(-n+2)) {
  border-bottom: solid black 1px;
}
<div id="parent">
  <div>
    One
  </div>
  <div>
    Two
  </div>
  <div>
    Three
  </div>
  <div>
    Four
  </div>
  <div>
    Five
  </div>
  <div>
    Six
  </div>
  <div>
    Seven
  </div>
</div>

Upvotes: 9

dsgriffin
dsgriffin

Reputation: 68626

In this case, something like

#parent > div:not(:nth-last-of-type(-n+2)) {
  border-bottom: 1px solid red;
}

JSFiddle

Upvotes: 7

Related Questions