Search code examples
htmlcssghost-blog

Multiple HTML/CSS only sliders on one page not changing independently


I am writing a blog post for a friend of mine, on the ghost platform. This blog post is going to be a very long post about multiple different objects, each requiring their own image slider. I'd like to re-use the same CSS for all 20 or so sliders on the page, only changing the html. I followed a slider tutorial online, that created a html/css only slider, as Ghost doesnt support scripting in their individual blog posts.

Unfortunately, when i click on one thumbnail, the image in all the other sliders disapears, and is only shown in that particular slider, making scrolling down the page a very boring experience.

Anyone able to spot the error? Here is an excerpt using only two sliders. take a look at the result further down:

<style>
* {margin: 0; padding: 0;}
body {background: #ccc;}

.slider{
	width: 640px; 
	position: relative;
	padding-top: 320px; 
	margin: 100px auto;
	box-shadow: 0 10px 20px -5px rgba(0, 0, 0, 0.75);
}

.slider>img{
	position: absolute;
	left: 0; top: 0;
	transition: all 0.2s;
}

.slider input[name='slide_switch'] {
	display: none;
}

.slider label {
	margin: 18px 0 0 18px;
	border: 3px solid #999;
	float: left;
	cursor: pointer;
	transition: all 0.5s;
	opacity: 0.6;
}

.slider label img{
	display: block;
}

.slider input[name='slide_switch']:checked+label {
	border-color: #666;
	opacity: 1;
}
.slider input[name='slide_switch'] ~ img {
	opacity: 0;
	transform: scale(1.1);
}
.slider input[name='slide_switch']:checked+label+img {
	opacity: 1;
	transform: scale(1);
}
</style>
**The xx: **
Insert image
About 
Gallery of the xx

<div class="slider">
	<input type="radio" name="slide_switch" id="id1"/>
	<label for="id1">
		<img src="http://thecodeplayer.com/uploads/media/3yiC6Yq.jpg" width="100"/>
	</label>
	<img src="http://thecodeplayer.com/uploads/media/3yiC6Yq.jpg"/>	
	<input type="radio" name="slide_switch" id="id2"/>
	<label for="id2">
		<img src="http://thecodeplayer.com/uploads/media/40Ly3VB.jpg" width="100"/>
	</label>
	<img src="http://thecodeplayer.com/uploads/media/40Ly3VB.jpg"/>
	<input type="radio" name="slide_switch" id="id3"/>
	<label for="id3">
		<img src="http://thecodeplayer.com/uploads/media/00kih8g.jpg" width="100"/>
	</label>
	<img src="http://thecodeplayer.com/uploads/media/00kih8g.jpg"/>
	<input type="radio" name="slide_switch" id="id4"/>
	<label for="id4">
		<img src="http://thecodeplayer.com/uploads/media/2rT2vdx.jpg" width="100"/>
	</label>
	<img src="http://thecodeplayer.com/uploads/media/2rT2vdx.jpg"/>
	<input type="radio" name="slide_switch" id="id5"/>
	<label for="id5">
		<img src="http://thecodeplayer.com/uploads/media/8k3N3EL.jpg" width="100"/>
	</label>
	<img src="http://thecodeplayer.com/uploads/media/8k3N3EL.jpg"/>
</div>

**The yy**
Insert image
About 
Gallery of the yy

<div class="slider">
	<input type="radio" name="slide_switch" id="id6"/>
	<label for="id6">
		<img src="http://thecodeplayer.com/uploads/media/3yiC6Yq.jpg" width="100"/>
	</label>
	<img src="http://thecodeplayer.com/uploads/media/3yiC6Yq.jpg"/>	
	<input type="radio" name="slide_switch" id="id7"/>
	<label for="id7">
		<img src="http://thecodeplayer.com/uploads/media/40Ly3VB.jpg" width="100"/>
	</label>
	<img src="http://thecodeplayer.com/uploads/media/40Ly3VB.jpg"/>
	<input type="radio" name="slide_switch" id="id8"/>
	<label for="id8">
		<img src="http://thecodeplayer.com/uploads/media/00kih8g.jpg" width="100"/>
	</label>
	<img src="http://thecodeplayer.com/uploads/media/00kih8g.jpg"/>
	<input type="radio" name="slide_switch" id="id9"/>
	<label for="id9">
		<img src="http://thecodeplayer.com/uploads/media/2rT2vdx.jpg" width="100"/>
	</label>
	<img src="http://thecodeplayer.com/uploads/media/2rT2vdx.jpg"/>
	<input type="radio" name="slide_switch" id="id10"/>
	<label for="id10">
		<img src="http://thecodeplayer.com/uploads/media/8k3N3EL.jpg" width="100"/>
	</label>
	<img src="http://thecodeplayer.com/uploads/media/8k3N3EL.jpg"/>
</div>


Solution

  • All of your radio buttons have the same name, so they're all considered a part of the same group. Only one radio button can be active in any one group. Each group should have a different name so they are grouped separately. Doing so will affect your current CSS but changing your attribute selector from name= to name^= or name*= will allow you to give each radio button set a different name. In the example below, we appended _1 to the second set.

    * {
      margin: 0;
      padding: 0;
    }
    
    body {
      background: #ccc;
    }
    
    .slider {
      width: 640px;
      position: relative;
      padding-top: 320px;
      margin: 100px auto;
      box-shadow: 0 10px 20px -5px rgba(0, 0, 0, 0.75);
    }
    
    .slider>img {
      position: absolute;
      left: 0;
      top: 0;
      transition: all 0.2s;
    }
    
    .slider input[name^='slide_switch'] {
      display: none;
    }
    
    .slider label {
      margin: 18px 0 0 18px;
      border: 3px solid #999;
      float: left;
      cursor: pointer;
      transition: all 0.5s;
      opacity: 0.6;
    }
    
    .slider label img {
      display: block;
    }
    
    .slider input[name^='slide_switch']:checked+label {
      border-color: #666;
      opacity: 1;
    }
    
    .slider input[name^='slide_switch']~img {
      opacity: 0;
      transform: scale(1.1);
    }
    
    .slider input[name^='slide_switch']:checked+label+img {
      opacity: 1;
      transform: scale(1);
    }
    <div class="slider">
      <input type="radio" name="slide_switch_1" id="id1" checked>
      <label for="id1">
    		<img src="http://thecodeplayer.com/uploads/media/3yiC6Yq.jpg" width="100">
    	</label>
      <img src="http://thecodeplayer.com/uploads/media/3yiC6Yq.jpg">
      <input type="radio" name="slide_switch_1" id="id2">
      <label for="id2">
    		<img src="http://thecodeplayer.com/uploads/media/40Ly3VB.jpg" width="100">
    	</label>
      <img src="http://thecodeplayer.com/uploads/media/40Ly3VB.jpg">
      <input type="radio" name="slide_switch_1" id="id3">
      <label for="id3">
    		<img src="http://thecodeplayer.com/uploads/media/00kih8g.jpg" width="100">
    	</label>
      <img src="http://thecodeplayer.com/uploads/media/00kih8g.jpg">
      <input type="radio" name="slide_switch_1" id="id4">
      <label for="id4">
    		<img src="http://thecodeplayer.com/uploads/media/2rT2vdx.jpg" width="100">
    	</label>
      <img src="http://thecodeplayer.com/uploads/media/2rT2vdx.jpg">
      <input type="radio" name="slide_switch_1" id="id5">
      <label for="id5">
    		<img src="http://thecodeplayer.com/uploads/media/8k3N3EL.jpg" width="100">
    	</label>
      <img src="http://thecodeplayer.com/uploads/media/8k3N3EL.jpg">
    </div>
    
    <div class="slider">
      <input type="radio" name="slide_switch" id="id6" checked>
      <label for="id6">
    		<img src="http://thecodeplayer.com/uploads/media/3yiC6Yq.jpg" width="100">
    	</label>
      <img src="http://thecodeplayer.com/uploads/media/3yiC6Yq.jpg">
      <input type="radio" name="slide_switch" id="id7">
      <label for="id7">
    		<img src="http://thecodeplayer.com/uploads/media/40Ly3VB.jpg" width="100">
    	</label>
      <img src="http://thecodeplayer.com/uploads/media/40Ly3VB.jpg">
      <input type="radio" name="slide_switch" id="id8">
      <label for="id8">
    		<img src="http://thecodeplayer.com/uploads/media/00kih8g.jpg" width="100">
    	</label>
      <img src="http://thecodeplayer.com/uploads/media/00kih8g.jpg">
      <input type="radio" name="slide_switch" id="id9">
      <label for="id9">
    		<img src="http://thecodeplayer.com/uploads/media/2rT2vdx.jpg" width="100">
    	</label>
      <img src="http://thecodeplayer.com/uploads/media/2rT2vdx.jpg">
      <input type="radio" name="slide_switch" id="id10">
      <label for="id10">
    		<img src="http://thecodeplayer.com/uploads/media/8k3N3EL.jpg" width="100">
    	</label>
      <img src="http://thecodeplayer.com/uploads/media/8k3N3EL.jpg">
    </div>