Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS :not(:last-child):after selector

I have a list of elements, which are styled like this:

ul {      list-style-type: none;      text-align: center;  }    li {      display: inline;  }    li:not(:last-child):after {      content:' |';  }
<ul>      <li>One</li>      <li>Two</li>      <li>Three</li>      <li>Four</li>      <li>Five</li>  </ul>

Outputs One | Two | Three | Four | Five | instead of One | Two | Three | Four | Five

Anyone know how to CSS select all but the last element?

You can see the definition of the :not() selector here

like image 344
Matt Clarkson Avatar asked Mar 27 '11 14:03

Matt Clarkson


People also ask

How do I use except last child in CSS?

There is a:not selector in css3. Use :not() with :last-child inside to select all children except last one.

How do I select all except last child?

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.

How do I get the last element in CSS?

If you need to get the last . hr element out of the document, regardless of what it's inside, then you can't do that in CSS. In CSS, you can only select the first, last, or nth element at one particular level of the hierarchy, you can't select the last element of some type regardless of what it's nested in.


2 Answers

If it's a problem with the not selector, you can set all of them and override the last one

li:after {   content: ' |'; } li:last-child:after {   content: ''; } 

or if you can use before, no need for last-child

li+li:before {   content: '| '; } 
like image 98
imma Avatar answered Oct 13 '22 15:10

imma


Every things seems correct. You might want to use the following css selector instead of what you used.

ul > li:not(:last-child):after 
like image 42
Vivek Avatar answered Oct 13 '22 13:10

Vivek