本文介紹了帶有 mod(或模)運算符的 nth-child的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
是否可以將 nth-child
與模一起使用?我知道你可以指定一個公式,比如
Is it possible to use the nth-child
with modulo? I know you can specify a formula, such as
nth-child(4n+2)
但我似乎無法找到是否有模運算符.我已經嘗試了以下示例,但似乎都不起作用:
But I can't seem to find if there's a modulo operator. I've tried the following examples below, and none seem to work:
nth-child(n%7)
nth-child(n % 7)
nth-child(n mod 7)
推薦答案
不,:nth-child()
只支持加減法和系數乘法.
我猜你正在嘗試獲取前 6 個元素(因為任何正整數 n
的 n mod 7
只會給你 0
到 6
).為此,您可以改用以下公式:
I gather you're trying to pick up the first 6 elements (as n mod 7
for any positive integer n
only gives you 0
to 6
). For that, you can use this formula instead:
:nth-child(-n+6)
通過否定n
,元素計數從零開始倒數,所以這些元素會被選中:
By negating n
, element counting is done backwards starting from zero, so these elements will be selected:
0 + 6 = 6
-1 + 6 = 5
-2 + 6 = 4
-3 + 6 = 3
-4 + 6 = 2
-5 + 6 = 1
...
jsFiddle 演示
這篇關于帶有 mod(或模)運算符的 nth-child的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!