Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to select all list items except for the first and last using CSS only?

I have an HTML list, from which I need to select all but the first and last element.

Right now I'm trying like this:

.inputList li:not(:first-child):not(:last-child) {
    // stuff
    }

Not really working... is there a better way, that also works on IE?

Thanks for inputs!

like image 716
frequent Avatar asked Mar 21 '12 10:03

frequent


2 Answers

You can specify a declaration for all child elements, then override it for first and last children:

.example > LI {background: green; }

.example > LI:first-child,
.example > LI:last-child {background: none; }
like image 87
Marat Tanalin Avatar answered Oct 05 '22 12:10

Marat Tanalin


Write like this:

.inputList li:first-child, .inputList li:last-child{
  color:red;
}

Note :first-child work till IE7 & above. :last-child work till IE9 & above.

like image 33
sandeep Avatar answered Oct 05 '22 12:10

sandeep