<style type="text/css">
.thumbnail {
width:100px;
height:20px;
margin:10px;
background-color:orange;
}
/* all the magic happens here*/
.thumbnail:nth-last-child(-n+4) {
background-color:red;
}
</style>
<div class="thumbnail"></div>
<div class="thumbnail"></div>
<div class="thumbnail"></div>
<div class="thumbnail"></div>
<div class="thumbnail"></div>
<div class="thumbnail"></div>
Style all elements except the last four
-
13-07-2023 - |
문제
So I'd like to apply a style to all elements with the class thumbnail, except the last four.
This is my code so far.
.thumbnail:nth-child(){...}
Something goes in the nth-child() function but I can't figure out what it is.
해결책
다른 팁
You can as well select the fith from last one and style any behind it : DEMO
.thumbnail:nth-last-child(5)~.thumbnail {
background-color:red;
}
.thumbnail:not(:nth-last-child(-n + 4)) {
/* .. */
}
But if you're trying to use :nth-child()
to style children with a certain class, it won't work.
The :nth-child()
pseudo class only styles children within a parent. It knows nothing about an elements class attribute.
.thumbnail:nth-last-child(-n+4) { background-color:red; }
its working 100%
제휴하지 않습니다 StackOverflow