0

In a simple pager I'm building I need to apply specific style to the current page if the current page is the first or the last page. And I don't know how to use the :first-of-type/ :last-of-type selector in addition to the normal class selector.

   <div id="myPages">
      <div class="something-else"></div>
      <div class="page-number current-page">1</div>
      <div class="page-number">2</div>
      <div class="page-number">3</div>
      <div class="another-one"></div>
  </div>

I'd like something like #myPages .page-number:first-of-type.current-page but it does not work because of the something-else and another-one div.

In my jsFiddle for example, I'd like the '1' to be blue.

Thanks for your help.

[edit] I've updated the fiddle, I think it's the best way to get my idea :)

Valentin Coudert
  • 1,759
  • 3
  • 19
  • 44

1 Answers1

-1

UPDATE: As the post author has changed their question, I've updated my answer:

#explain {
  margin-bottom: 20px;
}

#myPages .something-else+.current-page {
  color: blue;
}

#myPages div:nth-last-child(2) {
  color: red;
}
<div id="explain">
  Please note:<br> - there will be only one 'page-number' with 'current-page' in my app<br> - I don't know in advance how many pages there will be (so nth-child cannot work)
</div>
<div id="myPages">
  <div class="something-else"></div>
  <div class="page-number current-page">1 - Should be blue because it is the first page-number <strong>and</strong> it has the current-page class</div>
  <div class="page-number">2</div>
  <div class="page-number current-page">3 - Nothing should happen</div>
  <div class="page-number current-page">4 - Should be red because it is the last page-number <strong>and</strong> it has the current-page class</div>
  <div class="another-one"></div>
</div>
reinierkors
  • 507
  • 6
  • 19
  • I don't think this does what he wants, as your selectors aren't selecting nth-of-items-with-this-class, but items with this class which are also the nth child of their parent, regardless of class. Hence 1 being colored blue instead of 2. – Nathan Arthur Jun 06 '17 at 14:08
  • I don't know the number of page in advance, and I don't where the `current-page` will be – Valentin Coudert Jun 06 '17 at 14:09
  • @ValentinCoudert, I've updated my answer. Is this what you're looking for? – reinierkors Jun 06 '17 at 14:22