• ;
  • A Coding Boy
  • Blog
  • Videos
  • Projects
  • Home
HTML AND CSS
LOGIN PAGE
WEBSITE DESGIN
API PROJECTS
CARD DESGIN
JAVASCRIPTS GAMES
JAVASCRIPT PROJECTS
JAVA PROJECTS
PYTHON PROJECTS
demo post
only for demo nasa prospect
only for demo the gonnies
most popular
how to create parallax website
how to create a beautiful card
how to create a netflix login page
how to create flipping ui card
how to create image generator website
Create A Responsive Image Slider in HTML CSS and JavaScript
Image sliders have become an important component of websites, used to showcase multiple images in an engaging way. As a beginner web developer, creating an image slider can be a useful project to understand and improve your fundamental web development concepts, such as responsive designs, DOM manipulation, and JavaScript event listeners.
In this image slider, there are two buttons for sliding images: one for going back and one for moving forward. There is also a horizontal scrollbar that acts as a slider indicator and can be used to slide images by dragging it. This slider supports all major browsers like Chrome, Firefox, and Edge, as well as mobile or tablet devices.
Steps to Create Image Slider in HTML & JavaScript
To create a responsive image slider using HTML, CSS, and vanilla JavaScript, follow these simple step-by-step instructions:
1. Create a folder. You can put any name of this folder and create the below-mentioned files inside this folder.
2. Create an index.html file. The file name must be index and its extension .html
3. Create a style.css file. The file name must be style and its extension .css
4. Create a script.js file. The file name must be script and its extension .js
First, paste the following codes into your index.html file.
Plain text
Copy to clipboard
Open in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<!-- Coding By CodingNepal - www.codingnepalweb.com -->
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Image Slider in HTML CSS and JavaScript | CodingNepal</title>
<!-- Google Fonts Link For Icons -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD@48,400,0,0" />
<link rel="stylesheet" href="style.css" />
<script src="script.js" defer></script>
</head>
<body>
<div class="container">
<div class="slider-wrapper">
<button id="prev-slide" class="slide-button material-symbols-rounded">
chevron_left
</button>
<ul class="image-list">
<img class="image-item" src="images/img-1.jpg" alt="img-1" />
<img class="image-item" src="images/img-2.jpg" alt="img-2" />
<img class="image-item" src="images/img-3.jpg" alt="img-3" />
<img class="image-item" src="images/img-4.jpg" alt="img-4" />
<img class="image-item" src="images/img-5.jpg" alt="img-5" />
<img class="image-item" src="images/img-6.jpg" alt="img-6" />
<img class="image-item" src="images/img-7.jpg" alt="img-7" />
<img class="image-item" src="images/img-8.jpg" alt="img-8" />
<img class="image-item" src="images/img-9.jpg" alt="img-9" />
<img class="image-item" src="images/img-10.jpg" alt="img-10" />
</ul>
<button id="next-slide" class="slide-button material-symbols-rounded">
chevron_right
</button>
</div>
<div class="slider-scrollbar">
<div class="scrollbar-track">
<div class="scrollbar-thumb"></div>
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE html> <!-- Coding By CodingNepal - www.codingnepalweb.com --> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Image Slider in HTML CSS and JavaScript | CodingNepal</title> <!-- Google Fonts Link For Icons --> <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD@48,400,0,0" /> <link rel="stylesheet" href="style.css" /> <script src="script.js" defer></script> </head> <body> <div class="container"> <div class="slider-wrapper"> <button id="prev-slide" class="slide-button material-symbols-rounded"> chevron_left </button> <ul class="image-list"> <img class="image-item" src="images/img-1.jpg" alt="img-1" /> <img class="image-item" src="images/img-2.jpg" alt="img-2" /> <img class="image-item" src="images/img-3.jpg" alt="img-3" /> <img class="image-item" src="images/img-4.jpg" alt="img-4" /> <img class="image-item" src="images/img-5.jpg" alt="img-5" /> <img class="image-item" src="images/img-6.jpg" alt="img-6" /> <img class="image-item" src="images/img-7.jpg" alt="img-7" /> <img class="image-item" src="images/img-8.jpg" alt="img-8" /> <img class="image-item" src="images/img-9.jpg" alt="img-9" /> <img class="image-item" src="images/img-10.jpg" alt="img-10" /> </ul> <button id="next-slide" class="slide-button material-symbols-rounded"> chevron_right </button> </div> <div class="slider-scrollbar"> <div class="scrollbar-track"> <div class="scrollbar-thumb"></div> </div> </div> </div> </body> </html>
Second, paste the following codes into your style.css file
Plain text
Copy to clipboard
Open in new window
EnlighterJS 3 Syntax Highlighter
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
background: #f1f4fd;
}
.container {
max-width: 1200px;
width: 95%;
}
.slider-wrapper {
position: relative;
}
.slider-wrapper .slide-button {
position: absolute;
top: 50%;
outline: none;
border: none;
height: 50px;
width: 50px;
z-index: 5;
color: #fff;
display: flex;
cursor: pointer;
font-size: 2.2rem;
background: #000;
align-items: center;
justify-content: center;
border-radius: 50%;
transform: translateY(-50%);
}
.slider-wrapper .slide-button:hover {
background: #404040;
}
.slider-wrapper .slide-button#prev-slide {
left: -25px;
display: none;
}
.slider-wrapper .slide-button#next-slide {
right: -25px;
}
.slider-wrapper .image-list {
display: grid;
grid-template-columns: repeat(10, 1fr);
gap: 18px;
font-size: 0;
list-style: none;
margin-bottom: 30px;
overflow-x: auto;
scrollbar-width: none;
}
.slider-wrapper .image-list::-webkit-scrollbar {
display: none;
}
.slider-wrapper .image-list .image-item {
width: 325px;
height: 400px;
object-fit: cover;
}
.container .slider-scrollbar {
height: 24px;
width: 100%;
display: flex;
align-items: center;
}
.slider-scrollbar .scrollbar-track {
background: #ccc;
width: 100%;
height: 2px;
display: flex;
align-items: center;
border-radius: 4px;
position: relative;
}
.slider-scrollbar:hover .scrollbar-track {
height: 4px;
}
.slider-scrollbar .scrollbar-thumb {
position: absolute;
background: #000;
top: 0;
bottom: 0;
width: 50%;
height: 100%;
cursor: grab;
border-radius: inherit;
}
.slider-scrollbar .scrollbar-thumb:active {
cursor: grabbing;
height: 8px;
top: -2px;
}
.slider-scrollbar .scrollbar-thumb::after {
content: "";
position: absolute;
left: 0;
right: 0;
top: -10px;
bottom: -10px;
}
/* Styles for mobile and tablets */
@media only screen and (max-width: 1023px) {
.slider-wrapper .slide-button {
display: none !important;
}
.slider-wrapper .image-list {
gap: 10px;
margin-bottom: 15px;
scroll-snap-type: x mandatory;
}
.slider-wrapper .image-list .image-item {
width: 280px;
height: 380px;
}
.slider-scrollbar .scrollbar-thumb {
width: 20%;
}
}
* { margin: 0; padding: 0; box-sizing: border-box; } body { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: #f1f4fd; } .container { max-width: 1200px; width: 95%; } .slider-wrapper { position: relative; } .slider-wrapper .slide-button { position: absolute; top: 50%; outline: none; border: none; height: 50px; width: 50px; z-index: 5; color: #fff; display: flex; cursor: pointer; font-size: 2.2rem; background: #000; align-items: center; justify-content: center; border-radius: 50%; transform: translateY(-50%); } .slider-wrapper .slide-button:hover { background: #404040; } .slider-wrapper .slide-button#prev-slide { left: -25px; display: none; } .slider-wrapper .slide-button#next-slide { right: -25px; } .slider-wrapper .image-list { display: grid; grid-template-columns: repeat(10, 1fr); gap: 18px; font-size: 0; list-style: none; margin-bottom: 30px; overflow-x: auto; scrollbar-width: none; } .slider-wrapper .image-list::-webkit-scrollbar { display: none; } .slider-wrapper .image-list .image-item { width: 325px; height: 400px; object-fit: cover; } .container .slider-scrollbar { height: 24px; width: 100%; display: flex; align-items: center; } .slider-scrollbar .scrollbar-track { background: #ccc; width: 100%; height: 2px; display: flex; align-items: center; border-radius: 4px; position: relative; } .slider-scrollbar:hover .scrollbar-track { height: 4px; } .slider-scrollbar .scrollbar-thumb { position: absolute; background: #000; top: 0; bottom: 0; width: 50%; height: 100%; cursor: grab; border-radius: inherit; } .slider-scrollbar .scrollbar-thumb:active { cursor: grabbing; height: 8px; top: -2px; } .slider-scrollbar .scrollbar-thumb::after { content: ""; position: absolute; left: 0; right: 0; top: -10px; bottom: -10px; } /* Styles for mobile and tablets */ @media only screen and (max-width: 1023px) { .slider-wrapper .slide-button { display: none !important; } .slider-wrapper .image-list { gap: 10px; margin-bottom: 15px; scroll-snap-type: x mandatory; } .slider-wrapper .image-list .image-item { width: 280px; height: 380px; } .slider-scrollbar .scrollbar-thumb { width: 20%; } }
Last, paste the following codes into your script.js file.
Plain text
Copy to clipboard
Open in new window
EnlighterJS 3 Syntax Highlighter
const initSlider = () => {
const imageList = document.querySelector(".slider-wrapper .image-list");
const slideButtons = document.querySelectorAll(".slider-wrapper .slide-button");
const sliderScrollbar = document.querySelector(".container .slider-scrollbar");
const scrollbarThumb = sliderScrollbar.querySelector(".scrollbar-thumb");
const maxScrollLeft = imageList.scrollWidth - imageList.clientWidth;
// Handle scrollbar thumb drag
scrollbarThumb.addEventListener("mousedown", (e) => {
const startX = e.clientX;
const thumbPosition = scrollbarThumb.offsetLeft;
const maxThumbPosition = sliderScrollbar.getBoundingClientRect().width - scrollbarThumb.offsetWidth;
// Update thumb position on mouse move
const handleMouseMove = (e) => {
const deltaX = e.clientX - startX;
const newThumbPosition = thumbPosition + deltaX;
// Ensure the scrollbar thumb stays within bounds
const boundedPosition = Math.max(0, Math.min(maxThumbPosition, newThumbPosition));
const scrollPosition = (boundedPosition / maxThumbPosition) * maxScrollLeft;
scrollbarThumb.style.left = `${boundedPosition}px`;
imageList.scrollLeft = scrollPosition;
}
// Remove event listeners on mouse up
const handleMouseUp = () => {
document.removeEventListener("mousemove", handleMouseMove);
document.removeEventListener("mouseup", handleMouseUp);
}
// Add event listeners for drag interaction
document.addEventListener("mousemove", handleMouseMove);
document.addEventListener("mouseup", handleMouseUp);
});
// Slide images according to the slide button clicks
slideButtons.forEach(button => {
button.addEventListener("click", () => {
const direction = button.id === "prev-slide" ? -1 : 1;
const scrollAmount = imageList.clientWidth * direction;
imageList.scrollBy({ left: scrollAmount, behavior: "smooth" });
});
});
// Show or hide slide buttons based on scroll position
const handleSlideButtons = () => {
slideButtons[0].style.display = imageList.scrollLeft <= 0 ? "none" : "flex";
slideButtons[1].style.display = imageList.scrollLeft >= maxScrollLeft ? "none" : "flex";
}
// Update scrollbar thumb position based on image scroll
const updateScrollThumbPosition = () => {
const scrollPosition = imageList.scrollLeft;
const thumbPosition = (scrollPosition / maxScrollLeft) * (sliderScrollbar.clientWidth - scrollbarThumb.offsetWidth);
scrollbarThumb.style.left = `${thumbPosition}px`;
}
// Call these two functions when image list scrolls
imageList.addEventListener("scroll", () => {
updateScrollThumbPosition();
handleSlideButtons();
});
}
window.addEventListener("resize", initSlider);
window.addEventListener("load", initSlider);
const initSlider = () => { const imageList = document.querySelector(".slider-wrapper .image-list"); const slideButtons = document.querySelectorAll(".slider-wrapper .slide-button"); const sliderScrollbar = document.querySelector(".container .slider-scrollbar"); const scrollbarThumb = sliderScrollbar.querySelector(".scrollbar-thumb"); const maxScrollLeft = imageList.scrollWidth - imageList.clientWidth; // Handle scrollbar thumb drag scrollbarThumb.addEventListener("mousedown", (e) => { const startX = e.clientX; const thumbPosition = scrollbarThumb.offsetLeft; const maxThumbPosition = sliderScrollbar.getBoundingClientRect().width - scrollbarThumb.offsetWidth; // Update thumb position on mouse move const handleMouseMove = (e) => { const deltaX = e.clientX - startX; const newThumbPosition = thumbPosition + deltaX; // Ensure the scrollbar thumb stays within bounds const boundedPosition = Math.max(0, Math.min(maxThumbPosition, newThumbPosition)); const scrollPosition = (boundedPosition / maxThumbPosition) * maxScrollLeft; scrollbarThumb.style.left = `${boundedPosition}px`; imageList.scrollLeft = scrollPosition; } // Remove event listeners on mouse up const handleMouseUp = () => { document.removeEventListener("mousemove", handleMouseMove); document.removeEventListener("mouseup", handleMouseUp); } // Add event listeners for drag interaction document.addEventListener("mousemove", handleMouseMove); document.addEventListener("mouseup", handleMouseUp); }); // Slide images according to the slide button clicks slideButtons.forEach(button => { button.addEventListener("click", () => { const direction = button.id === "prev-slide" ? -1 : 1; const scrollAmount = imageList.clientWidth * direction; imageList.scrollBy({ left: scrollAmount, behavior: "smooth" }); }); }); // Show or hide slide buttons based on scroll position const handleSlideButtons = () => { slideButtons[0].style.display = imageList.scrollLeft <= 0 ? "none" : "flex"; slideButtons[1].style.display = imageList.scrollLeft >= maxScrollLeft ? "none" : "flex"; } // Update scrollbar thumb position based on image scroll const updateScrollThumbPosition = () => { const scrollPosition = imageList.scrollLeft; const thumbPosition = (scrollPosition / maxScrollLeft) * (sliderScrollbar.clientWidth - scrollbarThumb.offsetWidth); scrollbarThumb.style.left = `${thumbPosition}px`; } // Call these two functions when image list scrolls imageList.addEventListener("scroll", () => { updateScrollThumbPosition(); handleSlideButtons(); }); } window.addEventListener("resize", initSlider); window.addEventListener("load", initSlider);
Conclusion and Final words
In conclusion, creating a responsive image slider from scratch using HTML, CSS, and vanilla JavaScript is not only a valuable learning experience but also a practical addition to your web development skills. By following the steps in this post, you have successfully built a functional image slider, and you can now easily customize it according to your choice.
If you encounter any problems while creating your image slider, you can download the source code files for this project for free by clicking the Download button. Additionally, you can view a live demo of it by clicking the View Live button.
live demo
Download files
Info
Home
Projects
Blogs
Videos
About us
Follow us for more!
© 2023 All Rights Reserved A Coding Boy

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Glassmorphism Login Form | CodingNepal</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="wrapper"> <form action="#"> <h2>Login</h2> <div class="input-field"> <input type="text" required> <label>Enter your email</label> </div> <div class="input-field"> <input type="password" required> <label>Enter your password</label> </div> <div class="forget"> <label for="remember"> <input type="checkbox" id="remember"> <p>Remember me</p> </label> <a href="#">Forgot password?</a> </div> <button type="submit">Log In</button> <div class="register"> <p>Don't have an account? <a href="#">Register</a></p> </div> </form> </div> </body> </html>

@import url("https://fonts.googleapis.com/css2?family=Open+Sans:wght@200;300;400;500;600;700&display=swap"); * { margin: 0; padding: 0; box-sizing: border-box; font-family: "Open Sans", sans-serif; } body { display: flex; align-items: center; justify-content: center; min-height: 100vh; width: 100%; padding: 0 10px; } body::before { content: ""; position: absolute; width: 100%; height: 100%; background: url("https://www.codingnepalweb.com/demos/create-glassmorphism-login-form-html-css/hero-bg.jpg"), #000; background-position: center; background-size: cover; } .wrapper { width: 400px; border-radius: 8px; padding: 30px; text-align: center; border: 1px solid rgba(255, 255, 255, 0.5); backdrop-filter: blur(9px); -webkit-backdrop-filter: blur(9px); } form { display: flex; flex-direction: column; } h2 { font-size: 2rem; margin-bottom: 20px; color: #fff; } .input-field { position: relative; border-bottom: 2px solid #ccc; margin: 15px 0; } .input-field label { position: absolute; top: 50%; left: 0; transform: translateY(-50%); color: #fff; font-size: 16px; pointer-events: none; transition: 0.15s ease; } .input-field input { width: 100%; height: 40px; background: transparent; border: none; outline: none; font-size: 16px; color: #fff; } .input-field input:focus~label, .input-field input:valid~label { font-size: 0.8rem; top: 10px; transform: translateY(-120%); } .forget { display: flex; align-items: center; justify-content: space-between; margin: 25px 0 35px 0; color: #fff; } #remember { accent-color: #fff; } .forget label { display: flex; align-items: center; } .forget label p { margin-left: 8px; } .wrapper a { color: #efefef; text-decoration: none; } .wrapper a:hover { text-decoration: underline; } button { background: #fff; color: #000; font-weight: 600; border: none; padding: 12px 20px; cursor: pointer; border-radius: 3px; font-size: 16px; border: 2px solid transparent; transition: 0.3s ease; } button:hover { color: #fff; border-color: #fff; background: rgba(255, 255, 255, 0.15); } .register { text-align: center; margin-top: 30px; color: #fff;}