Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html> <head> <style> table { border: 1px solid #8EBF41; border-spacing: 10px; font-family: sans-serif; } table tr { background-color: #cccccc; } /* Selects the last three elements */ tr:nth-last-child(-n+3) { background-color: #8EBF41; } table tr td { padding: 10px; } /* Selects every element starting from the second to last item */ tr:nth-last-child(n+2) { color: #ffffff; } /* Select only the last second element */ tr:nth-last-child(2) { font-weight: 900; } </style> </head> <body> <h2>:nth-last-child selector example</h2> <table> <tbody> <tr> <td>First row</td> </tr> <tr> <td>Second row</td> </tr> <tr> <td>Third row</td> </tr> <tr> <td>Fourth row</td> </tr> <tr> <td>Fifth row</td> </tr> </tbody> </table> </body> </html>