简体   繁体   中英

How to use :not and :first-child pseudo selectors simultaneously

I want to select the first li without class="test" using :not and :first-child pseudoclasses. In this example I try to set color blue to <li>Third</li> :

 ul li { color:red; } ul li:not(.test):first-child { color:blue; } 
 <ul> <li class="test">First</li> <li class="test">Second</li> <li>Third</li> <li>Fourth</li> </ul> 

Why not do the below, which uses a combination of rules to filter the item you need (you can combine rules #1 and #3).

This has the added advantage of it not mattering what the index is of the first item with the class .test

 ul li { /* <-- select all list items */ color: red; } li:not(.test) { /* <-- select all list which arent test */ color: blue; } ul li:not(.test) ~ li { /* <-- select all list items which follow an item without test */ color: red; } 
 <ul> <li class="test">First</li> <li class="test">Second</li> <li>Third</li> <li>Fourth</li> </ul> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM