<ol>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
</ol>
ol > :nth-last-child(2) { /* Selects the second child element of <ol>, counting from the end */
    background-color: yellow;
}
Rendered Output
/* Positive integers: select the nth child from the end */
:nth-last-child(1)       /* Selects the first child element from the end */
li:nth-last-child(1)     /* Selects the first <li> element from the end among its siblings */

:nth-last-child(7)       /* Selects the seventh child element from the end */
li:nth-last-child(7)     /* Selects the seventh <li> element from the end among its siblings */

/* Keywords: even / odd */
:nth-last-child(odd)     /* Selects all odd-numbered children counting from the end (1, 3, 5, ...) */
li:nth-last-child(even)  /* Selects all even-numbered <li> elements counting from the end (2, 4, 6, ...) */

/* Formulas: An+B */
:nth-last-child(2n)      /* Selects every 2nd child counting from the end (2, 4, 6, ...) */
li:nth-last-child(2n)    /* Selects every 2nd <li> element counting from the end */

:nth-last-child(3n)      /* Selects every 3rd child counting from the end (3, 6, 9, ...) */
li:nth-last-child(3n)    /* Selects every 3rd <li> element counting from the end */

:nth-last-child(3n+1)    /* Selects every 3rd child plus 1, counting from the end (1, 4, 7, ...) */
:nth-last-child(3n-1)    /* Selects every 3rd child minus 1, counting from the end (2, 5, 8, ...) */
:nth-last-child(index | odd | even | an+b) {
    /* ... */
}
<ol>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
</ol>
li:nth-last-child(2) { /* Non-negative integer index from the end */
    border: 3px solid blue;
}
Rendered Output
<ol>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
</ol>
li:nth-last-child(even) {
    background-color: yellowgreen;
}
Rendered Output
:nth-last-child(5n)
/*
 * 0 [=5×0], 5 [=5×1], 10 [=5×2], 15 [=5×3], ... positions from the end.
 * Indexing starts at 1 from the last child, so the 0th element does not exist. 
 * Therefore, elements at positions 5, 10, 15, ... counting from the end are selected.
 * In summary, 5n selects every 5th element from the end.
*/

:nth-last-child(5n-1)
/*
 * -1 [=5×0-1], 4 [=5×1-1], 9 [=5×2-1], 14 [=5×3-1], ... positions from the end.
 * Indexing starts at 1 from the last child, so the -1st element does not exist. 
 * Therefore, elements at positions 4, 9, 14, ... from the end are selected.
 * In summary, 5n-1 selects one element before each multiple of 5 from the end.
*/

:nth-last-child(5n+1)
/*
 * 1 [=5×0+1], 6 [=5×1+1], 11 [=5×2+1], 16 [=5×3+1], ... positions from the end.
 * Indexing starts at 1 from the last child. 
 * Therefore, elements at positions 1, 6, 11, 16, ... from the end are selected.
 * In summary, 5n+1 selects one element after each multiple of 5 from the end.
*/

:nth-last-child(n)
/*
 * 0 [0], 1 [1], 2 [2], 3 [3], ... positions from the end.
 * Indexing starts at 1 from the last child, so the 0th element does not exist.
 * Therefore, 1, 2, 3, ... elements counting from the end are selected.
 * In summary, n selects all child elements counting from the end.
*/
<ol>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
</ol>   
ol {
    margin: 0;
    padding: 0;
    list-style: none;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
    gap: 7px;
}
ol li {
    aspect-ratio: 1/0.5;
    border: 1px solid red;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 10px;
}
ol li:nth-last-child(5n-1) {/* Selects elements one before every multiple of 5, counting from the end */
    background-color: yellowgreen;
}
Rendered Output

Last updated: December 19, 2019

See more details on caniuse.com.