How can I use a not:first-child selector?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How can I use a not:first-child selector?



I have div tag containing several ul tags.


div


ul



If I trying set CSS properties only for the first ul tag, and this code works:


ul


div ul:first-child
background-color: #900;



When I want set CSS properties for each ul tag except the first, I tried this:


ul


div ul:not:first-child
background-color: #900;



also this:


div ul:not(:first-child)
background-color: #900;



and this:


div ul:first-child:after
background-color: #900;



But to no effect. How must I write in CSS: "each element, except the first"?




6 Answers
6



One of the versions you posted actually works for all modern browsers (where CSS selectors level 3 are supported):


div ul:not(:first-child)
background-color: #900;



If you need to support legacy browsers, or if you are hindered by the :not selector's limitation (it only accepts a simple selector as an argument) then you can use another technique:


:not



Define a rule that has greater scope than what you intend and then "revoke" it conditionally, limiting its scope to what you do intend:


div ul
background-color: #900; /* applies to every ul */


div ul:first-child
background-color: transparent; /* limits the scope of the previous rule */



When limiting the scope use the default value for each CSS attribute that you are setting.





do you know exactly which versions this isn't supported on?
– Simon_Weaver
Jul 27 '14 at 19:48





@Simon_Weaver: In practice everything but IE < 9 supports it. A great resource to get this kind of information is caniuse.com.
– Jon
Jul 29 '14 at 9:15






Hmm.. watch out for the the restrictions of the first solution: caniuse.com/#feat=css-not-sel-list The second one sounded more reasonable for me. Thanks!
– iorrah
Jun 22 at 13:17



This CSS2 solution ("any ul after another ul") works, too, and is supported by more browsers.


ul


ul


div ul + ul
background-color: #900;



Unlike :not and :nth-sibling, the adjacent sibling selector is supported by IE7+.


:not


:nth-sibling



If you have JavaScript changes these properties after the page loads, you should look at some known bugs in the IE7 and IE8 implementations of this. See this link.



For any static web page, this should work perfectly.





The '+' selector doesn't seem to work in IE7.
– Leven
Sep 11 '13 at 13:30





@Leven: That quirksmode link says it should work in IE7, but only statically. If your JS places another element in front of it, it might not be updated correctly. Is that the issue you saw?
– Alex Quinn
Sep 18 '13 at 2:18





might be worth noting that in a sequence of elements like: ul ul div ul (where these elements are siblings), only the second ul will be selected, since the last one does not have a ul previous to itself
– Brian H.
Dec 13 '16 at 12:08


ul ul div ul


ul



Well since :not is not accepted by IE6~8, I would suggest you this:


:not


div ul:nth-child(n+2)
background-color: #900;



So you pick every ul in its parent element except the first one.


ul



Refer to Chris Coyer's "Useful :nth-child Recipes" article for more nth-child examples.


nth-child


div li~li
color: red;



Supports IE7



not(:first-child) does not seem to work anymore. At least with the more recent versions of Chrome and Firefox.


not(:first-child)



Instead, try this:


ul:not(:first-of-type)





They both work, but they have different meaning. ul:not(:first-child) means literally "any ul element that is not first child of its parent", so it won't match even the 1st ul if it's preceded by another element (p, heading etc.). On the contrary, ul:not(:first-of-type) means "any ul element except the 1st ul in the container". You are right that OP probably needed the latter behavior, but your explanation is rather misleading.
– Ilya Streltsyn
Aug 21 '17 at 16:44



ul:not(:first-child)


ul


ul


p


ul:not(:first-of-type)


ul


ul



I didn't have luck with some of the above,



This was the only one that actually worked for me



ul:not(:first-of-type)


ul:not(:first-of-type)



This worked for me when I was trying to have the first button displayed on the page not be effected by a margin-left option.



this was the option I tried first but it didn't work



ul:not(:first-child)


ul:not(:first-child)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard