In this blog, I will show you how to create a simple slider from HTML only with a live demo
This is a Simple Slider Code here Only HTML, CSS, and JavaScript are used.
<!DOCTYPE html>
<html>
<head>
<title>slider</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body{
padding: 0;
margin: 0;
box-sizing: border-box;
}
/* Slideshow container */
.slideshow-container {
max-width: 80%;
position: relative;
margin: auto;
}
/* Next & previous buttons */
.prev, .next {
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;
user-select: none;
}
/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
}
.active:hover {
background-color: #717171;
}
/* Fading animation */
.fade {
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
h1{
text-align: center;
font-family: sans-serif;
}
/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
.prev, .next {font-size: 11px}
}
</style>
</head>
<body>
<h1>simple slider only HTML | CSS | JavaScript </h1>
<div class="slideshow-container">
<div class="mySlides fade">
<img src="https://i.ibb.co/RpmmZ7s/html-slider-1.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="https://i.ibb.co/WFjZZVS/html-slider-2.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="https://i.ibb.co/cCwb0mg/html-slider-3.jpg" style="width:100%">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<br>
<script>
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
}
</script>
</body>
</html>
Table of Contents
output:-
simple slider only HTML | CSS | Javascipt
note:- how is work your website totally responsive.
so compulsory to add a meta tag to your website.
<meta name="viewport" content="width=device-width, initial-scale=1">
the simple slider only HTML, CSS&JavaScript with Text or Title
<!DOCTYPE html>
<html>
<title>slider</title>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body{
padding: 0;
margin: 0;
box-sizing: border-box;
}
/* Slideshow container */
.slideshow-container {
max-width: 80%;
position: relative;
margin: auto;
}
/* Next & previous buttons */
.prev, .next {
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;
user-select: none;
}
/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
}
/* Caption text */
.text {
color: #000;
font-size: 25px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
font-weight: 700;
text-align: center;
}
.active:hover {
background-color: #717171;
}
/* Fading animation */
.fade {
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
h1{
text-align: center;
font-family: sans-serif;
}
/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
.prev, .next,.text {font-size: 11px}
}
</style>
</head>
<body>
<h1>the simple slider only HTML, CSS&JavaScript with Text or Title</h1>
<div class="slideshow-container">
<div class="mySlides fade">
<img src="https://i.ibb.co/RpmmZ7s/html-slider-1.jpg" style="width:100%">
<div class="text">Title Text</div>
</div>
<div class="mySlides fade">
<img src="https://i.ibb.co/WFjZZVS/html-slider-2.jpg" style="width:100%">
<div class="text">Title Two</div>
</div>
<div class="mySlides fade">
<img src="https://i.ibb.co/cCwb0mg/html-slider-3.jpg" style="width:100%">
<div class="text">Title Three</div>
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<br>
<script>
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
}
</script>
</body>
</html>
output:-
auto play slider
<!DOCTYPE html>
<html>
<title>auto play slider</title>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {box-sizing: border-box;}
body {font-family: Verdana, sans-serif;}
.mySlides {display: none;}
img {vertical-align: middle;}
/* Slideshow container */
.slideshow-container {
max-width: 80% ;
position: relative;
margin: auto;
}
.active {
background-color: #717171;
}
/* Fading animation */
.fade {
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
h2,p{
text-align: center;
}
</style>
</head>
<body>
<h2>auto play slider</h2>
<p>Change image 5 seconds:</p>
<div class="slideshow-container">
<div class="mySlides fade">
<img src="https://i.ibb.co/RpmmZ7s/html-slider-1.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="https://i.ibb.co/WFjZZVS/html-slider-2.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="https://i.ibb.co/cCwb0mg/html-slider-3.jpg" style="width:100%">
</div>
</div>
<br>
<script>
var slideIndex = 0;
showSlides();
function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slideIndex++;
if (slideIndex > slides.length) {slideIndex = 1}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace("active", "");
}
slides[slideIndex-1].style.display = "block";
setTimeout(showSlides, 5000); // Change image every 2 seconds
}
</script>
</body>
</html>
output:-
auto play slider
Change image 5 seconds: