3

How can I select the first element with class "red" (First) in this construction?

HTML:

<div class='container'>
    <p>Zero</p>
    <p class="red">First</p>
    <p class="red">Second</p>
</div>

http://jsfiddle.net/ek9Ch/

inser
  • 1,190
  • 8
  • 17

3 Answers3

1

try this..

.container .red:nth-child(2)
{
color: red;
}
Vegeta
  • 1,319
  • 7
  • 17
  • 1
    Ok. Works for this situation. But if I have a lot of p elements without class before first "red"? – inser Feb 07 '14 at 11:59
  • Let's me explain why this answer won: it did what I need - select required element using ONE css expression – inser Feb 10 '14 at 08:44
1

Ok, not very pretty but does the job:

.container p + p.red {
    color: red;
}

.container p + p.red ~ p {
    color:black; /*reverting back*/
}

fiddle: http://jsfiddle.net/Varinder/ek9Ch/2/

Varinder
  • 2,634
  • 1
  • 17
  • 20
0
.container p[class=red]:nth-child(2)
        {
            color: red;
        }
Kaptan
  • 336
  • 2
  • 11