CSS Tutorial

CSS Style

CSS Layout Style

CSS Float Style

CSS Reference

CSS :nth-last-child(n) Selector


What is :nth-last-child(n) Selector?

CSS :nth-last-child(n) selector matches only nth last child element of an parent element.

Browser Compatibility

  • Google Chrome 2+
  • Mozilla Firefox 3+
  • Internet Explorer 9+
  • Opera 9.5+
  • Safari 4+

Note: Here details of browser compatibility with version number may be this is bug and not supported. But recommended to always use latest Web browser.

Syntax

General syntax of :nth-last-child selector,

:nth-last-child(n) {
	property: value;
	property: value;
	...
}

/* nth number item in list from last */
li:nth-last-child(n) {
	property: value;
	property: value;
	...
}

/* select odd item in list from last */
li:nth-last-child(odd) {
	property: value;
	property: value;
	...
}

/* select even item in list from last */
li:nth-child(even) {
	property: value;
	property: value;
	...
}

/* every second item in list from last*/
li:nth-child(2n+1) {
	property: value;
	property: value;
	...
}

/* every third item in list from last*/
li:nth-child(3n+1) {
	property: value;
	property: value;
	...
}

Example:

CSS li:nth-last-child(2) selector select only second last <li> element whose are child of an <ul> element.

<style type="text/css">
li:nth-last-child(2) {
	background:#ff0000;
}
</style>
</head>
<body>
    <ul>
        <li>This is first line</li>
        <li>This is second line</li>
        <li>This is third line</li>
        <li>This is forth line</li>
        <li>This is fifth line</li>
    </ul>
</body>

Run it...   »

Example Result:

Example:

CSS li:nth-child(3n+1) selector select every third <li> element from last and whose are child of an <ul> element.

<style type="text/css">
li:nth-child(3n+1) {
	background:#ff0000;
}
</style>
</head>
<body>
    <ul>
        <li>This is first line</li>
        <li>This is second line</li>
        <li>This is third line</li>
        <li>This is forth line</li>
        <li>This is fifth line</li>
    </ul>
</body>

Run it...   »

Example Result: