MDN wants to learn about developers like you: https://qsurvey.mozilla.com/s3/MDN-survey

Our volunteers haven't translated this article into Bahasa Indonesia yet. Join us and help get the job done!
You can also read the article in English (US).

The :nth-last-child() CSS pseudo-class matches one or more elements based on their position among a group of siblings, counting from the end.

Note n and n+1 both start at the same element counting backwards from the end.

/* Selects every fourth element
   among any group of siblings,
   counting backwards from the last one */
:nth-last-child(4n) {
  color: lime;
}

Note: This pseudo-class is essentially the same as :nth-child, except it counts items backwards from the end, not forwards from the beginning.

Syntax

The nth-last-child pseudo-class is specified with a single argument, which represents the pattern for matching elements, counting from the end.

Keyword values

odd
Represents elements whose numeric position in a series of siblings is odd: 1, 3, 5, etc., counting from the end.
even
Represents elements whose numeric position in a series of siblings is even: 2, 4, 6, etc., counting from the end.

Functional notation

<An+B>
Represents elements whose numeric position in a series of siblings matches the pattern An+B, for every positive integer or zero value of n. The index of the first element, counting from the end, is 1. The values A and B must both be <integer>s.

Formal syntax

:nth-last-child( <nth> [ of <selector># ]? )

where
<nth> = <an-plus-b> | even | odd

Examples

Example selectors

tr:nth-last-child(odd) or tr:nth-last-child(2n+1)
Represents the odd rows of an HTML table: 1, 3, 5, etc., counting from the end.
tr:nth-last-child(even) or tr:nth-last-child(2n)
Represents the even rows of an HTML table: 2, 4, 6, etc., counting from the end.
:nth-last-child(7)
Represents the seventh element, counting from the end.
:nth-last-child(5n)
Represents elements 5, 10, 15, etc., counting from the end.
:nth-last-child(3n+4)
Represents elements 4, 7, 10, 13, etc., counting from the end.
:nth-last-child(-n+3)
Represents the last three elements among a group of siblings.
p:nth-last-child(n)
Represents every <p> element among a group of siblings. This is the same as a simple p selector.
p:nth-last-child(1) or p:nth-last-child(0n+1)
Represents every <p> that is the first element among a group of siblings, counting from the end. This is the same as the :last-child selector.

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;
}

/* Grabs every element starting from the last second element */
tr:nth-last-child(n+2) {
  color: blue;
}

/* Select only the last second element */
tr:nth-last-child(2) {
  font-weight: 600;
}

Result

 

Corner case 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

tr:nth-last-child(n) {
  background-color: lightgray;
}

tr:nth-last-child(n+1){
  font-weight: 600;
}

Result

 

Specifications

Specification Status Comment
Selectors Level 4
The definition of ':nth-last-child' in that specification.
Working Draft Matching elements are not required to have a parent.
Selectors Level 3
The definition of ':nth-last-child' in that specification.
Recommendation Initial definition.

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support4 Yes3.5993.2
Matches elements with no parent57 ?52 ?44 ?
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes Yes Yes4103.2 ?
Matches elements with no parent5757 ?5244 ? ?

See also

Tag Dokumen dan Kontributor

 Terakhir diperbarui oleh: delete12345,