Dzhuneyt
Dzhuneyt

Reputation: 8701

How to target all elements besides the first with nth-child?

How do I target all paragraphs inside a given DIV beside the first one using the :nth-child operator?

:nth-child(/* select all but the first one */) {
     color: green;
}
<div>
    <p>Example 1</p>
    <p>Example 2</p>
    <p>Example 3</p>
    <p>Example 4</p>
    <p>Example 5</p>
    <p>Example 6</p>
    <p>Example 7</p>
</div>

Upvotes: 6

Views: 7466

Answers (3)

Dmytro Zarezenko
Dmytro Zarezenko

Reputation: 10686

You can use the following formula:

:nth-child(n+1)

or for some browsers:

:nth-child(n+2)

W3Schools says:

Using a formula (an + b). Description: a represents a cycle size, n is a counter (starts at 0), and b is an offset value.

Link

Or you can use separate :first-child CSS declaration for this first element.

Upvotes: 13

xkeshav
xkeshav

Reputation: 54016

use

p:nth-child(n+2) {
    color: green;   
}

working DEMO

Reference

Upvotes: 6

Clyde Lobo
Clyde Lobo

Reputation: 9174

Try

div > p:nth-child(n+2)

Demo at http://jsfiddle.net/Q6FDq/

Upvotes: 4

Related Questions