0

I have the following pure css slideshow and I'm trying to implement "next/prev" buttons in addition to the thumbnails (both are labels pointing to a <input type="radio">).

The thumbnails are labels with an image inside, so I created more labels for the buttons. The problem is that I can't figure out how to correctly hide them all except for the ones next to the checked input.

I've tried the following code:

.slider input[name='slide_switch'] ~ label.next,
.slider input[name='slide_switch'] ~ label.previous
{
    z-index: -1
}
.slider input[name='slide_switch']:checked ~ label.next,
.slider input[name='slide_switch']:checked ~ label.previous {
    z-index: 1
}

That should target all <label class="next"> and <label class="previous"> in the HTML, but for some reason it's not working, I "can see" more than one button,one upon another, because I gave them opacity: 0.6 and they don't look semi-transparent. However the last two of them show the correct opacity and are working, if I'm in the last picture I can click the previous button and it'll take me to the 4th picture, and if I click next it takes me to the 5th (unfortunately this happens with all pictures).

I'm working it here: https://jsfiddle.net/5v6ajfup/

The HTML code:

<div class="slider">
    <input type="radio" name="slide_switch" id="id1"/>
    <label class="thumbnail" for="id1">
        <img src="http://thecodeplayer.com/uploads/media/3yiC6Yq.jpg" width="100"/>
    </label>
    <img src="http://thecodeplayer.com/uploads/media/3yiC6Yq.jpg"/>
    <label class="next" for="id2">></label>

    <input type="radio" name="slide_switch" id="id2" checked="checked"/>
    <label class="thumbnail" for="id2">
        <img src="http://thecodeplayer.com/uploads/media/40Ly3VB.jpg" width="100"/>
    </label>
    <img src="http://thecodeplayer.com/uploads/media/40Ly3VB.jpg"/>
    <label class="previous" for="id1">></label>
    <label class="next" for="id3">></label>

    <input type="radio" name="slide_switch" id="id3"/>
    <label class="thumbnail" for="id3">
        <img src="http://thecodeplayer.com/uploads/media/00kih8g.jpg" width="100"/>
    </label>
    <img src="http://thecodeplayer.com/uploads/media/00kih8g.jpg"/>
    <label class="previous" for="id2">></label>
    <label class="next" for="id4">></label>

    <input type="radio" name="slide_switch" id="id4"/>
    <label class="thumbnail" for="id4">
        <img src="http://thecodeplayer.com/uploads/media/2rT2vdx.jpg" width="100"/>
    </label>
    <img src="http://thecodeplayer.com/uploads/media/2rT2vdx.jpg"/>
    <label class="next" for="id5">></label>
    <label class="previous" for="id3">></label>

    <input type="radio" name="slide_switch" id="id5"/>
    <label class="thumbnail" for="id5">
        <img src="http://thecodeplayer.com/uploads/media/8k3N3EL.jpg" width="100"/>
    </label>
    <img src="http://thecodeplayer.com/uploads/media/8k3N3EL.jpg"/>
    <label class="previous" for="id4">></label>
</div>

The CSS code:

* { margin: 0; padding: 0 }
body { background: #ccc }
.slider {
    width: 640px;
    position: relative;
    padding-top: 320px;
    margin: 20px auto;
    box-shadow: 0 10px 20px -5px rgba(0, 0, 0, 0.75);
}
.slider > img {
    position: absolute;
    left: 0; top: 0;
    transition: all 0.5s;
    display: block
}
.slider input[name='slide_switch'] {
    display: none;
}
.slider label.next, .slider label.previous {
    position: absolute;
    left: 0; top: 0;
    display: block;
    font-size:3em;
    opacity: 0.6;
    cursor: pointer;
    color: white;
}
.slider label.next { left: 560px; line-height: 320px }
.slider label.previous { left: 50px; line-height: 320px; transform: rotateY(180deg) }
.slider label.thumbnail {
    margin: 18px 0 0 18px;
    border: 3px solid #999;
    float: left;
    cursor: pointer;
    transition: all 0.5s;
    opacity: 0.6;
}
.slider label.thumbnail:hover { opacity: 0.8 }
.slider label.thumbnail img {
    display: block;
}
.slider input[name='slide_switch']:checked + label.thumbnail {
    border-color: #666;
    opacity: 1;
}
.slider input[name='slide_switch'] ~ label.next,
.slider input[name='slide_switch'] ~ label.previous
{
    z-index: -1
}
.slider input[name='slide_switch']:checked ~ label.next,
.slider input[name='slide_switch']:checked ~ label.previous {
    z-index: 1
}
.slider input[name='slide_switch'] ~ img {
    opacity: 0;
    transform: scale(1.1);
}
.slider input[name='slide_switch']:checked + label.thumbnail + img {
    opacity: 1;
    transform: scale(1);
}

(Source)

Chazy Chaz
  • 1,781
  • 3
  • 29
  • 48

2 Answers2

2

This rule

.slider input[name='slide_switch']:checked ~ label.next,
.slider input[name='slide_switch']:checked ~ label.previous {
    z-index: 1
}

will target all label.next/label.previous that comes after an input[name='slide_switch'], and not only the immediate one's up until next input[name='slide_switch'].

You either need to target each input[name='slide_switch'] specific label using their for attribute, or use nth-of-type


Updated based on a comment

Solution 1 - Preferred

Instead of changing the :checked rule above, wrap each group of input/image/thumb/arrow in a div and change this rule's .slider > img selector to slider > div > img

Updated fiddle

Stack snippet

* { margin: 0; padding: 0 }
body { background: #ccc }
.slider {
 width: 640px;
 position: relative;
 padding-top: 320px;
 margin: 20px auto;
 box-shadow: 0 10px 20px -5px rgba(0, 0, 0, 0.75);
}
.slider > div > img {
 position: absolute;
 left: 0; top: 0;
 transition: all 0.5s;
    display: block
}
.slider input[name='slide_switch'] {
 display: none;
}
.slider label.next, .slider label.previous {
 position: absolute;
 left: 0; top: 0;
    display: block;
    font-size:3em;
 opacity: 0.6;
 cursor: pointer;
    color: white;
}
.slider label.next { left: 560px; line-height: 320px }
.slider label.previous { left: 50px; line-height: 320px; transform: rotateY(180deg) }
.slider label.thumbnail {
 margin: 18px 0 0 18px;
 border: 3px solid #999;
 float: left;
 cursor: pointer;
 transition: all 0.5s;
 opacity: 0.6;
}
.slider label.thumbnail:hover { opacity: 0.8 }
.slider label.thumbnail img {
 display: block;
}
.slider input[name='slide_switch']:checked + label.thumbnail {
 border-color: #666;
 opacity: 1;
}
.slider input[name='slide_switch'] ~ label.next,
.slider input[name='slide_switch'] ~ label.previous
{
 z-index: -1
}
.slider input[name='slide_switch']:checked ~ label.next,
.slider input[name='slide_switch']:checked ~ label.previous {
 z-index: 1
}
.slider input[name='slide_switch'] ~ img {
 opacity: 0;
 transform: scale(1.1);
}
.slider input[name='slide_switch']:checked + label.thumbnail + img {
 opacity: 1;
 transform: scale(1);
}
<div class="slider">

  <div>
    <input type="radio" name="slide_switch" id="id1" />
    <label class="thumbnail" for="id1">
      <img src="http://thecodeplayer.com/uploads/media/3yiC6Yq.jpg" width="100" />
    </label>
    <img src="http://thecodeplayer.com/uploads/media/3yiC6Yq.jpg" />
    <label class="next" for="id2">></label>
  </div>

  <div>
    <input type="radio" name="slide_switch" id="id2" checked/>
    <label class="thumbnail" for="id2">
      <img src="http://thecodeplayer.com/uploads/media/40Ly3VB.jpg" width="100" />
    </label>
    <img src="http://thecodeplayer.com/uploads/media/40Ly3VB.jpg" />
    <label class="previous" for="id1">></label>
    <label class="next" for="id3">></label>
  </div>

  <div>
    <input type="radio" name="slide_switch" id="id3" />
    <label class="thumbnail" for="id3">
      <img src="http://thecodeplayer.com/uploads/media/00kih8g.jpg" width="100" />
    </label>
    <img src="http://thecodeplayer.com/uploads/media/00kih8g.jpg" />
    <label class="previous" for="id2">></label>
    <label class="next" for="id4">></label>
  </div>

  <div>
    <input type="radio" name="slide_switch" id="id4" />
    <label class="thumbnail" for="id4">
      <img src="http://thecodeplayer.com/uploads/media/2rT2vdx.jpg" width="100" />
    </label>
    <img src="http://thecodeplayer.com/uploads/media/2rT2vdx.jpg" />
    <label class="next" for="id5">></label>
    <label class="previous" for="id3">></label>
  </div>

  <div>
    <input type="radio" name="slide_switch" id="id5" />
    <label class="thumbnail" for="id5">
      <img src="http://thecodeplayer.com/uploads/media/8k3N3EL.jpg" width="100" />
    </label>
    <img src="http://thecodeplayer.com/uploads/media/8k3N3EL.jpg" />
    <label class="previous" for="id4">></label>
  </div>

</div>

Solution 2

Replace this rule

.slider input[name='slide_switch']:checked ~ label.next,
.slider input[name='slide_switch']:checked ~ label.previous {
    z-index: 1
}

with this

.slider input#id1:checked ~ label[for="id2"],
.slider input#id2:checked ~ label[for="id1"],
.slider input#id2:checked ~ label[for="id3"],
.slider input#id3:checked ~ label[for="id2"],
.slider input#id3:checked ~ label[for="id4"],
.slider input#id4:checked ~ label[for="id3"],
.slider input#id4:checked ~ label[for="id5"],
.slider input#id5:checked ~ label[for="id4"]
{
    z-index: 1;
}
Asons
  • 84,923
  • 12
  • 110
  • 165
  • Yes, I see it now. So using the `for` attribute means that I need the id number and in the case of `nth-of-type` I also need a position. How can I do this then? Counters? – Chazy Chaz Jun 21 '17 at 17:18
  • @ChazyChaz Updated my answer – Asons Jun 21 '17 at 17:44
0

You aren't using the tilde selector correctly. All sibling elements after the left hand of the tilde will actually match.

https://stackoverflow.com/questions/10782054/what-does-the-tilde-squiggle-twiddle-css-selector-mean

So even though your code is setting the z-index to -1, that is getting overwritten in most cases by this (since this rule is more specific):

.slider input[name='slide_switch']:checked ~ label.next,
.slider input[name='slide_switch']:checked ~ label.previous {
    z-index: 1;
}
Jennifer Goncalves
  • 1,442
  • 1
  • 12
  • 29
  • Oh right I see it now... So not only the ones I need are getting overwritten, also every one after the checked... So, how would I show only the two first labels? – Chazy Chaz Jun 21 '17 at 17:16