Skip To Content
Skip To Content
Home
Categories
o
o
o
o
o
o
o
How To
Resources
o
o
o
o
o
o
o
You surely try out many JavaScript or jQuery sliders for your website. But how
about one build with CSS only? You may be thinking such sliders are limited in
their functionality, right?
But with the help of CSS3 animations, we are able to build a nice-looking & multi-
function slider. One such function is called automation and we can create a CSS-
only slider that supports the automatic function.
Such sliders allow us to automatically slide the images within the time limit we set
through CSS animation functions.
In this tutorial, I will build the slider which will move the images from the right to
left automatically without adding any kind of JavaScript.
Furthermore, you can also convert this slider into a text or content base automatic
slider by simply removing the tag img and place the text.
Properly, you don’t know that the JavaScript slider slows down your site or
webpage. Additionally, they don’t work well if the user has disabled the JavaScript
interpretation in the browser.
One of the best solutions to this problem is don’t use the slider or go with the CSS
build slideshow.
The label actually will be a toggle button which will we can switch ON/OFF to
play the slider automatically. When we set the toggle ON, the slider images will
automatically slide from right to left.
1. <input id="sliderSwitch" class="slider__switch" type="checkbox"
name="sliderSwitch" hidden />
The next step for HTML source code is to define the images for the slider and for
this, We will create a div with a class name slider. We will place the images with
the help of the unordered list element to keep the HTML nice and clean.
The basic styling starts from here. We will apply position relative to .slider so the
absolute position works with list items.
Moreover, We will add some margin, width/height according to our need, and box-
shadow to stand out the slider.
1. .slider {
2. position: relative;
3. margin-top: 3rem;
4. margin-right: auto;
5. margin-left: auto;
6. overflow: hidden;
7. width: 40.625rem;
8. height: 26.25rem;
9. box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
10.}
11..slider__list {
12. position: absolute;
13. left: 0;
14. width: 162.5rem;
15.}
16..slider__slide {
17. float: left;
18.}
In order to create an automatic image slider in HTML, the following CSS code is
required. Also, add it to your project.
1. .slider__control {
2. margin-right: auto;
3. margin-left: auto;
4. width: 4.5rem;
5. font-family: sans-serif;
6. }
7. .slider__control label {
8. position: relative;
9. display: block;
10. margin-top: 2rem;
11. margin-bottom: 1rem;
12. width: 4.5rem;
13. height: 2rem;
14. font-size: 1rem;
15. font-weight: normal;
16. line-height: 1.5;
17. color: transparent;
18. background: #ddd;
19. border-radius: 2rem;
20. cursor: pointer;
21. -webkit-transition: left 0.15s ease-out;
22. transition: left 0.15s ease-out;
23.}
24..slider__control label:before {
25. content: "autoplay";
26. position: absolute;
27. top: 2.5rem;
28. left: 0;
29. color: #333;
30. font-size: .95rem;
31. font-weight: bold;
32. text-transform: uppercase;
33.}
34..slider__control label:after {
35. content: "";
36. position: absolute;
37. top: .25rem;
38. left: .25rem;
39. display: block;
40. width: 1.5rem;
41. height: 1.5rem;
42. border-radius: 2rem;
43. background: #fff;
44. -webkit-transition: left 0.15s ease-out;
45. transition: left 0.15s ease-out;
46. -webkit-transform: translate3d(0, 0, 0);
47. transform: translate3d(0, 0, 0);
48.}
49..slider__switch:checked + .slider > .slider__list {
50. -webkit-animation-name: autoplay;
51. animation-name: autoplay;
52. /* This will change the time it takes to move to next slide */
53. -webkit-animation-duration: 10s;
54. animation-duration: 10s;
55. -webkit-animation-iteration-count: infinite;
56. animation-iteration-count: infinite;
57.}
58..slider__switch:checked + .slider + .slider__control > label
{ background: #455a64; }
59..slider__switch:checked + .slider + .slider__control > label:after
{ left: 2.75rem; }
To enable the autoplay function, We will use the CSS3 animation. We will set a
different position for each slide so they move accordingly.
1. @-webkit-keyframes
2. autoplay { /* position of the first slide */
3. 0% {
4. left: 0;
5. }
6. /* position of the second slide */
7. 25% {
8. left: -40.625rem;
9. }
10. /* position of the third slide */
11. 50% {
12. left: -81.25rem;
13.}
14. /* position of the fourth slide */
15. 100% {
16. left: -121.875rem;
17.}
18.}
19.@keyframes
20.autoplay { /* position of the first slide */
21. 0% {
22. left: 0;
23.}
24. /* position of the second slide */
25. 25% {
26. left: -40.625rem;
27.}
28. /* position of the third slide */
29. 50% {
30. left: -81.25rem;
31.}
32. /* position of the fourth slide */
33. 100% {
34. left: -121.875rem;
35.}
36.}
Conclusion
That’s it. We are done with the slider and it’s ready for implementation. You can
check out the demo and download the source code.
Just to mention here, It will not overload your site and also consumes less browser
memory. It will also work if the Javascript is disabled in the browser.
CategoriesCSS Slideshow TagsImage Slider
Share it!
You May Also Like
1.
Joe
January 2, 2021 at 2:20 am
I’m liking your slider but there is one thing that isn’t all that obvious. That is
the slide display duration. Even though you can change the rate of transition,
the length of display is always the same and the last slide always transitions
slower than all the rest. What’s up?
Reply
Muhammad Asif
January 4, 2021 at 1:01 pm
Hello Joe!
You need to change the value of animation-duration instead of
transition.
2.
Peter Quackenbush
January 23, 2021 at 11:33 pm
Your Slideshow works great; but, My question regards to BACKGROUND
Image Slideshow Auto-Scrolling, as I have Open-Close Curtains [= working
perfectly] that COVER your Slideshow ….
But the situation remains that, while your Auto-Scrolling shows up ALSO
[= working perfectly], it IS SHOWING UP OVER the curtains, and NOT
BEHIND them …
Both, codes for the Curtains and Slideshow still work PERFECTLY
simultaneously, except for the Slideshow BEHIND the Curtains …
Best of Regards
Reply
Muhammad Asif
January 25, 2021 at 11:06 am
Hello Peter!
Yes, you need to use the z-index property if you have placed your
images as a background but slides are behind the Curtains. You
can bring the slides to the front using:
.slider__slide{
z-index: 999;
}
Leave a Comment
Comment
Post Comment
Recommended
CSS OVERLAY IMAGE SLIDER SLIDE ANIMATION CSS GALLERY PROGRESS BAR FLIP
ANIMATION STICKY HEADER PARALLAX BACKGROUND
Create a Comment Box in HTML and CSS
March 31, 2021
Home
About Us
Contact us
License
Disclaimer
DMCA
Terms & Conditions
© 2021 Codeconvey.com - All rights reserved.