Automatic Image Slider in HTML & CSS only without JavaScript
Hello coders! Welcome to this blog.
We are talking about Automatic Image Slider, and today In this tutorial, we will learn How to make an Automatic Image Slider in HTML & CSS only. Yes! you heard it right, we will use only HTML & CSS to make this project.
Automatic Image Slider is common website UI, that you have must seen on many websites. On websites, you will generally see this in the Hero Section, Product Section, or Gallery Section. There are many images in the image slider, which it shows one by one to the User.
By the way, there are two types of sliders, one is a manual slider which has two buttons, the Next button, and the Previous button. And the second is the automatic slider which moves automatically at a fixed time interval. So we are going to make that, Automatic Image Slider using HTML and CSS only without JavaScript.
First of all, watch the preview video of this project.
Video preview of the Automatic Image Slider
How to create Automatic Image Slider using HTML and CSS only?
Here we have discussed How to create Automatic Image Slider in HTML CSS without JavaScript, step by step. Also, we have provided Automatic Image Slider source code!
Step 1 – Write HTML code
First, create an index.html
file. Follow the instruction below.
In the HTML code, first, we have linked our stylesheet file in the <head>
tag. Then under the body
tag we have created parent <section>
,which class name is slider_container
and under this parent slider_container
we have created child <section>
which class name is slider
.
<section class=”slider_container”>
- It will be the whole slider container.<section class=”slider”>
– It will be our main slider, which will wrap all the slides and slide/move right to left automatically.
Then we created <div>
under the child section, we give a class name of the <div>
slide one, which will be our slide, and in this slide, we have <img>
for images content and <span>
tag with class name caption
from slider caption.
If you have noticed that, we have created the class name of slide <div>
is slide one. Here slider is the common class and one is the unique class.
Because, we will keep this slider as multiple, then if we design a particular slider, then this class name will help us at the same time. Right now this is the first slider that’s why it has unique class one
.
When we will add more slider then we will add two for second slider, three for third slider etc.
Note:
<img src="..." alt=" ">
in the image tag, you should not add these dots. Replace these dots with your image path or any other image link source.
Here we have duplicated the slider <div>
4 times, so we have created 5 sliders in the HTML code. Alright, now completed the HTML structure, now watch and copy the HTML code of Automatic Image Slider and paste it into your index.html
file.
Read also: Awesome Slider Animation using HTML, CSS, and JavaScript
Copy HTML code
Step 2 – Write CSS code and styling it.
After structuring the HTML code, then create a style.css
file and follow the instruction below.
In the CSS code, we have styled the components and the elements. We have width, height, padding, margin, background, etc. We have used a flex-box layout container for proper alignment of the slider and all slides.
How do I make an Image Slider Autoplay?
As we know we are making Automatic Image Slider in HTML & CSS only, without JavaScript. So we should create that Autoplay slider animation using CSS.
So for that, as we discussed before, we created a <div>
in HTML with the class name slider, which will move right to left automatically with the help of CSS @keyframes
animation.
So animating the slider, we have created a @keyframes
animation whose name is sliding. Watch the below CSS code.
Copy CSS code
In this @keyframes animation, as you can see here, we have used CSS transform: translateX()
. All the values in minus(-), because we will move them, from right to left.
Live preview of Automatic Image Slider in HTML & CSS only
Alright! We have Automatic Image Slider in HTML CSS CodePen live preview link here. CodePen Demo
Conclusion
This is all about Automatic Image Slider in HTML & CSS only. I hope you liked this project and enjoy it and learned something new. You can use it for your projects, also you can add it to your portfolio. If you have any queries related to this Project, let me know in the comments and contact me.