Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the opposite of the CSS :last-of-type selector?

Tags:

css

The CSS selector :last-of-type selects the last element of the type. Is there a way for me to select everything which comes before the last element of the type?

like image 831
Kirk Ouimet Avatar asked Jul 23 '20 17:07

Kirk Ouimet


2 Answers

You can use :not(:last-of-type)

div span {
  display: inline-block;
  width: 20px;
  height: 20px;
  background: #000;
}

div span:not(:last-of-type) {
  background: red;
}
<div>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
</div>
like image 156
doğukan Avatar answered Sep 29 '22 19:09

doğukan


I saw the :not operator in another answer and then tried this and it worked well. Not sure if there is a performance difference.

:nth-last-child(n+2)

like image 45
Kirk Ouimet Avatar answered Sep 29 '22 19:09

Kirk Ouimet