Introduction
In this article, we will learn how to create a simple image slider using HTML, CSS, and JavaScript only. We are not using any external frameworks/plugins for the slider.
Within seconds, a developer thinks of using an existing jQuery plugin or something else. While using such plugins, sometimes there is a chance of code conflicts between the plugin libraries and the existing libraries used in the application, which takes time to get fixed. In real-time scenarios, there may be a requirement to put an image slider on the application web page.
If the slider requirement is simple and short, building your slider using HTML and JavaScript can be one of the best ways to achieve it. This will take less time to implement and give no conflicts/errors.
Let's start making it.
Step 1
Create a folder named "images" in the project path and put all the images required for the slider. Make sure that all the images are the same size (width*height). Otherwise, the slider will misbehave while navigating between slides.
Step 2
Add the below code in the body section of the HTML page.
<body>
<div class="slidercontainer">
<div class="showSlide fade">
<img src="images/img1.jpg" />
<div class="content">Lorem ipsum dolor sit amet</div>
</div>
<div class="showSlide fade">
<img src="images/img2.jpg"/>
<div class="content">Lorem ipsum dolor sit amet</div>
</div>
<div class="showSlide fade">
<img src="images/img3.jpg"/>
<div class="content">Lorem ipsum dolor sit amet</div>
</div>
<div class="showSlide fade">
<img src="images/img4.jpg"/>
<div class="content">Lorem ipsum dolor sit amet</div>
</div>
<!-- Navigation arrows -->
<a class="left" onclick="nextSlide(-1)">?</a>
<a class="right" onclick="nextSlide(1)">?</a>
</div>
</body>
Here, <div class= "slidercontainer"> is the main container for the slider, and <div class= "showSlide fade"> is the slider images section that is repeating.
Step 3
Write the JavaScript code. Considering it a small example, I am writing the code in the same HTML page using <script type= "text/javascript"></script>.
You can create an external JS file in the project path and refer it to the HTML page if required.
<script type="text/javascript">
var slide_index = 1;
displaySlides(slide_index);
function nextSlide(n) {
displaySlides(slide_index += n);
}
function currentSlide(n) {
displaySlides(slide_index = n);
}
function displaySlides(n) {
var i;
var slides = document.getElementsByClassName("showSlide");
if (n > slides.length) { slide_index = 1 }
if (n < 1) { slide_index = slides.length }
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slide_index - 1].style.display = "block";
}
</script>
All the above functions are user-defined. We only write some logic to read the slides and show.
Step 4
Now, it's time to apply CSS to showcase the images in a proper position with some styles. Below is the final code,
HTML, JavaScript, CSS Code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>My Slider</title>
<style type="text/css">
body {
margin: 0;
background: #e6e6e6;
}
.showSlide {
display: none
}
.showSlide img {
width: 100%;
}
.slidercontainer {
max-width: 1000px;
position: relative;
margin: auto;
}
.left, .right {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -22px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
}
.right {
right: 0;
border-radius: 3px 0 0 3px;
}
.left:hover, .right:hover {
background-color: rgba(115, 115, 115, 0.8);
}
.content {
color: #eff5d4;
font-size: 30px;
padding: 8px 12px;
position: absolute;
top: 10px;
width: 100%;
text-align: center;
}
.active {
background-color: #717171;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}
@keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}
</style>
</head>
<body>
<div class="slidercontainer">
<div class="showSlide fade">
<img src="images/img1.jpg" />
<div class="content">Slide1 heading</div>
</div>
<div class="showSlide fade">
<img src="images/img2.jpg" />
<div class="content">Slide2 heading</div>
</div>
<div class="showSlide fade">
<img src="images/img3.jpg" />
<div class="content">Slide3 heading</div>
</div>
<div class="showSlide fade">
<img src="images/img4.jpg" />
<div class="content">Slide4 heading</div>
</div>
<!-- Navigation arrows -->
<a class="left" onclick="nextSlide(-1)">?</a>
<a class="right" onclick="nextSlide(1)">?</a>
</div>
<script type="text/javascript">
var slide_index = 1;
displaySlides(slide_index);
function nextSlide(n) {
displaySlides(slide_index += n);
}
function currentSlide(n) {
displaySlides(slide_index = n);
}
function displaySlides(n) {
var i;
var slides = document.getElementsByClassName("showSlide");
if (n > slides.length) { slide_index = 1 }
if (n < 1) { slide_index = slides.length }
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slide_index - 1].style.display = "block";
}
</script>
</body>
</html>
In the above code, I haven't included any libraries, not even jQuery.
Step 5
Now, let's run the HTML page in the browser and see the output. The slider should work correctly.

Thanks for reading this article. I hope it helps.
Peter AitkenPosted Jul 11, 2023, 3:15 PM
Very useful, thanks. Is it possible to have the images advance automatically? Can I add transition effects such as a fade?
Sagar BeheraPosted May 9, 2022, 2:53 PM
It was very helpful for me. Thank you
Pierre LombardPosted Sep 8, 2020, 5:35 AM
Rajitha, just one bug... if I embed this code in more than one place on one page, then the second slider does not display the images, and the arrows on the first slider stop working. Any ideas on how to get this great slider to work for multiple implementations on one page?
Alis BePosted Jun 29, 2020, 7:14 PM
Is this slider accessible? I'm having trouble with sliders that refuse to work with voiceover or talkback on mobile and that refuse to read aloud the new slider position as indicated. Would love to see someone's codepen for it. Thanks.
Ćh SufyanPosted Apr 11, 2020, 3:26 AM
Good work dear. Very informative article
Dharmendra Kumar PanditPosted Jul 28, 2019, 5:35 AM
Thank you . very helpful article working fine.
indra senPosted Nov 29, 2018, 8:58 AM
Can i control the number of images which can be used in the slider? Like the number of images sliding should be dynamically controlled by me.
Sanwar RanwaPosted Feb 1, 2018, 1:10 AM
Very helpfull.............
Phani KumarPosted Dec 8, 2017, 12:37 AM
I want to add slow motion to the images when i click it..can you also help me on it?
MdMaidul IslamPosted Oct 10, 2017, 8:06 AM
I am using your code. It works. But Images are not showing. after click image will show but after few seconds images are disappear(vanish).Please help me .
Rajitha AlluriPosted Mar 23, 2017, 1:07 AM
Hi, yes i think i missed adding the output. Will update it very soon.
Vinodh NarayananPosted Mar 23, 2017, 12:36 AM
Welcome good one thanks for sharing one query please show the final results of your code