乐闻世界logo
搜索文章和话题

How can i use a notfirst child selector

3个答案

1
2
3

The :not(:first-child) selector in CSS combines the pseudo-class selector :not() with :first-child. Its primary purpose is to select elements that are not the first child of their parent.

Here is an example using the :not(:first-child) selector:

Assume we have the following HTML structure:

html
<ul> <li>First list item</li> <li>Second list item</li> <li>Third list item</li> </ul>

We want to select all list items except the first one and apply styles to them. We can achieve this in CSS by:

css
li:not(:first-child) { color: red; }

This rule sets the text color to red for the second and third list items, while the first list item retains its default color.

The advantage of using the :not(:first-child) selector is that we can directly specify the elements we do not want to select (in this case, the first child), without having to set styles for the other elements individually. This approach enhances the readability and maintainability of our code.

2024年6月29日 12:07 回复

:not(:first-child) doesn't seem to be working anymore. At least in the latest versions of Chrome and Firefox.

Instead, try this:

css
ul:not(:first-of-type) {}
2024年6月29日 12:07 回复

Since IE6-8 does not support :not, I recommend the following:

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

Therefore, you select all elements except the first one within the parent element.

For more examples, see Chris Coyer's article 'Useful :nth-child Recipes' at [http://css-tricks.com/useful-nth-child-recipies/].

2024年6月29日 12:07 回复

你的答案