You would have seen very heavy JavaScript based sliders. These JavaScript based sliders make the webpage slower and also don’t work if user has disabled JavaScript interpretation in browser. One solution to this problem is don’t use those sliders, but how would you implement a slider without JavaScript? This article is the answer to that question. I will show a proper working modal of a slider which has been made without JavaScript.
View Demo
index.html
<html>
<head>
<title>QNimate Slider</title>
<link href="style.css" type="text/css" rel="stylesheet">
</head>
<body>
<div class="slider-holder">
<span id="slider-image-1"></span>
<span id="slider-image-2"></span>
<span id="slider-image-3"></span>
<div class="image-holder">
<img src="1.jpg" class="slider-image" />
<img src="2.jpg" class="slider-image" />
<img src="3.jpg" class="slider-image" />
</div>
<div class="button-holder">
<a href="#slider-image-1" class="slider-change"></a>
<a href="#slider-image-2" class="slider-change"></a>
<a href="#slider-image-3" class="slider-change"></a>
</div>
</div>
</body>
</html>
style.css
{
width: 800px;
height: 400px;
background-color: yellow;
margin-left: auto;
margin-right: auto;
margin-top: 0px;
text-align: center;
overflow: hidden;
}
.image-holder
{
width: 2400px;
background-color: red;
height: 400px;
clear: both;
position: relative;
-webkit-transition: left 2s;
-moz-transition: left 2s;
-o-transition: left 2s;
transition: left 2s;
}
.slider-image
{
float: left;
margin: 0px;
padding: 0px;
position: relative;
}
#slider-image-1:target ~ .image-holder
{
left: 0px;
}
#slider-image-2:target ~ .image-holder
{
left: -800px;
}
#slider-image-3:target ~ .image-holder
{
left: -1600px;
}
.button-holder
{
position: relative;
top: -20px;
}
.slider-change
{
display: inline-block;
height: 10px;
width: 10px;
border-radius: 5px;
background-color: brown;
}
We have three .jpg images each of width 800px and height 400px. We put them left floated on the image.holder div. Now we move the div left and right according to the anchor clicked. This is a good working method and the coding style is also not dirty.
Conclusion
I showed how to create a awesome slider using HTML and CSS only. This consumes less browser memory and computation power. It also works if JavaScript is disabled. Thanks for reading.