使用nth-child选择多个子元素

use*_*202 7 html css css-selectors

div div p:nth-child(1 to 5)
Run Code Online (Sandbox Code Playgroud)

如何用nth-child选择多个数字,所以我得到子元素1到5而不必写:

div div p:nth-child(1),
div div p:nth-child(2),
div div p:nth-child(3),
div div p:nth-child(4),
div div p:nth-child(5) {

}
Run Code Online (Sandbox Code Playgroud)

所以看起来应该是这样的:

div div p:nth-child(1 to 5)
Run Code Online (Sandbox Code Playgroud)

jon*_*ode 6

div div p:nth-child(-n+5){

}
Run Code Online (Sandbox Code Playgroud)

这将选择前5个孩子.


pot*_*hin 5

div div p:nth-child(1),
div div p:nth-child(2),
div div p:nth-child(3),
div div p:nth-child(4),
div div p:nth-child(5){

}
Run Code Online (Sandbox Code Playgroud)

要么

div div p:nth-child(-n+5){

}
Run Code Online (Sandbox Code Playgroud)


Ban*_*ana 5

div div p:nth-child(n+1):nth-child(-n+5){

}
Run Code Online (Sandbox Code Playgroud)

将选择元素1到5


Hug*_*hes 5

我已经附加了一个JSFiddle的链接,它应该按照你的要求做,但看起来应该是这样的:

li:nth-child(-n+5){
    color: red;
}
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/p2aBc/1/