2

I have some divs with 2 classess - all of them have ticket class and some of them have visible class:

<div class="ticket hidden">BLA 1</div>
<div class="ticket hidden">BLA 2</div>
<div class="ticket hidden">BLA 3</div>
<div class="ticket visible">BLA 4</div>
<div class="ticket visible">BLA 5</div>
<div class="ticket visible">BLA 6</div>
<div class="ticket visible">BLA 7</div>
<div class="ticket visible">BLA 8</div>

When I'm adressing it with:

.visible:nth-child(5) {
 text-decoration: underline;
}

I get BLA 5, and i want BLA 8

How can I adress specific .visible element?

JSFiddle.

Salman A
  • 262,204
  • 82
  • 430
  • 521
Karol Klepacki
  • 2,070
  • 1
  • 18
  • 30
  • would it be possible to use :last-child? o is it just a coincidence that's it's at the end? – jbutler483 Dec 05 '14 at 13:33
  • 1
    possible duplicate of [nth-child doesn't respond to class](http://stackoverflow.com/questions/5428676/nth-child-doesnt-respond-to-class) – Rahul Desai Dec 05 '14 at 13:33
  • possible duplicate of [CSS :nth-of-type selector breaks on non-matching childs](http://stackoverflow.com/questions/27313984/css-nth-of-type-selector-breaks-on-non-matching-childs) –  Dec 05 '14 at 13:59

3 Answers3

3

:nth-child further restricts the matched elements to those that are nth child of its parent. There is no CSS selector that matches nth element having class x.

For the given markup, you could use the + combinator:

.visible ~ .visible ~ .visible ~ .visible ~ .visible {
  /*
   * matches .visible element with at least 4 previous .visible siblings
   */
  text-decoration: underline;
}
.visible ~ .visible ~ .visible ~ .visible ~ .visible ~ .visible {
  /*
   * matches .visible element with at least 5 previous .visible siblings
   */
  text-decoration: line-through;
}
<div class="ticket hidden">Hidden</div>
<div class="ticket hidden">Hidden</div>
<div class="ticket visible">Visible 1</div>
<div class="ticket visible">Visible 2</div>
<div class="ticket hidden">Hidden</div>
<div class="ticket hidden">Hidden</div>
<div class="ticket visible">Visible 3</div>
<div class="ticket visible">Visible 4</div>
<div class="ticket hidden">Hidden</div>
<div class="ticket hidden">Hidden</div>
<div class="ticket visible">Visible 5</div>
<div class="ticket visible">Visible 6</div>
<div class="ticket hidden">Hidden</div>
<div class="ticket hidden">Hidden</div>
<div class="ticket visible">Visible 7</div>
<div class="ticket visible">Visible 8</div>
Salman A
  • 262,204
  • 82
  • 430
  • 521
1

nth-child() don´t care which class you want to check. It always count all siblings in a DOM element.

Maybe you want to give :nth-of-type a chance: developer.mozilla.org

Maybe the best solution is to use some jQuery and add another class to append the underline style

Der Vampyr
  • 941
  • 1
  • 7
  • 13
0

You can add a div at the beginning of the visible class and give it the nth-child http://jsfiddle.net/7uap9v21/3/

<div class="visible-holder">
<div class="visible ticket">BLA 4</div>
<div class="visible ticket">BLA 5</div>
<div class="visible ticket">BLA 6</div>
<div class="visible ticket">BLA 7</div>
<div class="visible ticket">BLA 8</div>
</div>
Akshay
  • 14,138
  • 5
  • 46
  • 70