:nth-last-child
åºçº¿
广æ³å¯ç¨
*
èª 2015å¹´7æ èµ·ï¼æ¤ç¹æ§å·²å¨ä¸»æµæµè§å¨ä¸å¾å°æ¯æï¼å¯å¨å¤§å¤æ°è®¾å¤åæµè§å¨çæ¬ä¸æ£å¸¸ä½¿ç¨ã
* æ¤ç¹æ§çæäºé¨åçæ¯æç¨åº¦å¯è½ææä¸åã
:nth-last-child() è¿ä¸ªCSS 伪类 ä»å
å¼èç¹ä¸ä»åå¾åå¹é
å¤äºæäºä½ç½®çå
ç´
/* 卿æå
å¼èç¹ä¸ï¼ä»åå¾å
éæ©ææ 4 çåæ°çèç¹ */
:nth-last-child(4n) {
color: lime;
}
夿³¨ï¼è¿ä¸ªä¼ªç±»å :nth-child åºæ¬ä¸è´ï¼ä½å®æ¯ä»ç»å°¾è®¡æ°ï¼è䏿¯ä»å¼å§è®¡æ°ã
è¯æ³
nth-last-child伪类æ¥åä¸ä¸ªåæ°ï¼ç¨æ¥ä½ä¸ºä¸ä¸ªæ¨¡å¼ï¼ä»åå¾åå¹é
å
ç´ ã
Keyword values
Functional notation
Formal syntax
Error: could not find syntax for this item示ä¾
>éæ©å¨ç¤ºä¾
tr:nth-last-child(odd)ortr:nth-last-child(2n+1)-
表示 HTML 表çåæ°ç奿°è¡:1ã3ã5 çã
tr:nth-last-child(even)ortr:nth-last-child(2n)-
表示 HTML 表çåæ°çå¶æ°è¡:2ã4ã6 çã
:nth-last-child(7)-
è¡¨ç¤ºåæ°ç¬¬ 7 个å ç´ ã
:nth-last-child(5n)-
è¡¨ç¤ºåæ°ç第 5ã10ã15 çå ç´ ã
:nth-last-child(3n+4)-
è¡¨ç¤ºåæ°ç第 4ã7ã10ã13 çå ç´ ã
:nth-last-child(-n+3)-
表示ä¸ç»å å¼èç¹ä¸çæåä¸ä¸ªå ç´ ã
p:nth-last-child(n)orp:nth-last-child(n+1)-
表示ä¸ç»å å¼èç¹ä¸çæ¯ä¸ª
<p>å ç´ ãè¿ä¸ä¸ä¸ªç®åçpéæ©å¨ç¸åã(ç±äºnä» 0 å¼å§ï¼èæåä¸ä¸ªå ç´ ä» 1 å¼å§ï¼nån+1é½ä¼éæ©ç¸åçå ç´ ã) p:nth-last-child(1)orp:nth-last-child(0n+1)-
表示ææå¤äºå å¼èç¹ä¸åæ°ç¬¬ä¸ä½çå ç´
<p>ãè¿ä¸:last-childéæ©å¨ç¸åã
Table example
HTML
<table>
<tbody>
<tr>
<td>First line</td>
</tr>
<tr>
<td>Second line</td>
</tr>
<tr>
<td>Third line</td>
</tr>
<tr>
<td>Fourth line</td>
</tr>
<tr>
<td>Fifth line</td>
</tr>
</tbody>
</table>
CSS
table {
border: 1px solid blue;
}
/* Selects the last three elements */
tr:nth-last-child(-n + 3) {
background-color: pink;
}
/* Selects every element starting from the second to last item */
tr:nth-last-child(n + 2) {
color: blue;
}
/* Select only the last second element */
tr:nth-last-child(2) {
font-weight: 600;
}
ç»æ
Quantity query
æ°éæ¥è¯¢æ ·å¼å
ç´ åå³äºå®ä»¬çæ°éã卿¬ä¾ä¸ï¼å½ç»å®å表ä¸è³å°æä¸ä¸ªå表项æ¶ï¼å表项å为红è²ãè¿æ¯éè¿ç»ånth-last-childå éç¨å
å¼éæ©å¨.çåè½æ¥å®ç°ç
HTML
<h4>A list of four items (styled):</h4>
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ol>
<h4>A list of two items (unstyled):</h4>
<ol>
<li>One</li>
<li>Two</li>
</ol>
CSS
/* If there are at least three list items,
style them all */
li:nth-last-child(n + 3),
li:nth-last-child(n + 3) ~ li {
color: red;
}
Result
è§è
| è§è |
|---|
| Selectors Level 4> # nth-last-child-pseudo> |