InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search
  • Home
  • > Tutorials
  • > CSS cookbook

CSS – :nth child, :nth-last-child, :first-child, :last-child pseudo classes examples

By admin on Jan 16, 2016

CSS nth-child, nth-last-child, first-child, last-child pseudo class selectors (:nth-child(), :nth-last-child(), :first-child, :last-child) are pretty powerful css selectors to create very useful styles in html.
css-nth-child-nth-last-child-first-child-last-child
Here are some points to note about these pseudo classes.

  1. These pseudo classes can apply to any element like “div”, li, tr, etc.
  2. In case one uses expression like :nth-child(3n+1), it is calculated for n=0,1,2,3,… and so on. Whatever is the outcome of expression, the style applies to child at that count. e.g. (3n+1) will apply to
    Value of n Applies to which child
    0 1
    1 4
    2 7
    … …
  3. :nth-last-child() is similar to :nth-child() except that it starts backward from last child.
  4. These selectors work in most modern browsers (don’t work in IE 8 or older browsers)

:nth-child() odd/even example

This is probably most frequently use case. To have different style for odd/even child, the following example style can be used.

<style type="text/css">
.oddeven > div:nth-child(odd) {background:lightgreen;}
.oddeven > div:nth-child(even) {background:lightgray;}
</style>
<div class="oddeven">
<div>v1</div>
<div>v2<div>v2.1</div><div>v2.2</div></div>
<div>v3</div>
<div>v4</div>
</div>
refresh done
try it online

First child (:nth-child(1) or :first-child) example

We can use :nth-child(1) or :first-child (without brackets)

<style type="text/css">
.foo > li:nth-child(1) {background:lightgreen;}
.bar > li:first-child {background:lightgray;}
</style>
First list<ul class="foo">
<li>v1</li>
<li>v2</li>
<li>v3</li>
<li>v4</li>
</ul>
Second list<ul class="bar">
<li>v1</li>
<li>v2</li>
<li>v3</li>
<li>v4</li>
</ul>
refresh done
try it online

:nth-child() first 2 children example

We can use :nth-child(-n+2). Here for n=0,1,2,3, … expression (“-n+2”) value will be 2, 1, 0, -1, …. Hence it will match with first and 2nd child. Zero and negative values are ignored.

<style type="text/css">
.foo > li:nth-child(-n+2) {background:lightgreen;}
</style>
<ul class="foo">
<li>v1</li>
<li>v2</li>
<li>v3</li>
<li>v4</li>
</ul>
refresh done
try it online

Last child (:nth-last-child(1) or :last-child) example

We can use :nth-last-child(1) or last-child (without brackets). :nth-last-child() cound from last element.

<style type="text/css">
.foo > li:nth-last-child(1) {background:lightgreen;}
.bar > li:last-child {background:lightgreen;}
</style>
First list<ul class="foo">
<li>v1</li>
<li>v2</li>
<li>v3</li>
<li>v4</li>
</ul>
Second list<ul class="bar">
<li>v1</li>
<li>v2</li>
<li>v3</li>
<li>v4</li>
</ul>
refresh done
try it online

:nth-child() every third child example

We can use nth-child(3n+1) for every third child starting from first.

<style type="text/css">
.foo > li:nth-child(3n+1) {background:lightgreen;}
</style>
<ul class="foo">
<li>v1</li>
<li>v2</li>
<li>v3</li>
<li>v4</li>
<li>v5</li>
</ul>
refresh done
try it online

Suggested posts:

  1. CSS :nth-of-type
  2. CSS ::before and ::after examples
  3. Vim – how to change current character case to uppercase or lowercase
  4. CSS :not – negation pseudo-class
  5. PHP regex – word boundary examples
  6. Css :last-child selector – last child element
  7. CSS :root
  8. Mysql how to dump schema of all databases
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged CSS, CSS cookbook, CSS Pseudo Classes, CSS3
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress