This code creates a simple image slider using CSS only. It comes with a list of images that are displayed in a row. When the user hovers over the slider, the images will slide to the left, revealing the next image in the list. The user can also click on the images to view them in full size.
The CSS code for the slider uses the :hover
pseudo-class to change the width of the li
elements. When the user hovers over the slider, the li
elements will become wider, revealing the next image in the list. The CSS code also uses the transition
property to create a smooth animation effect.
How to Create Simple Image Slider CSS Only
1. Copy the following HTML code, specifically the structure within the <div class="accord">
element. This includes the <ul>
list containing <li>
items, each with a <div class="title">
and an <img>
tag. Replace the image sources in the <img>
tags with URLs or paths to your desired images.
<div class="accord"> <ul> <li> <div class="title"> <a href="#">the style</a> </div> <img src="https://p1.pxfuel.com/preview/1015/988/1021/watch-fashion-accessories-objects.jpg" alt=""/> </li> <li> <div class="title"> <a href="#">A leaf </a> </div> <img src="https://p0.pxfuel.com/preview/248/813/143/achievement-adult-battle-black.jpg" alt=""> </li> <li> <div class="title"> <a href="#">rose</a> </div> <img src="https://p0.pxfuel.com/preview/990/311/594/coffee-cup-mug-office.jpg" alt=""/> </li> <li> <div class="title"> <a href="#">the water</a> </div> <img src="https://p1.pxfuel.com/preview/259/365/195/finger-pointing-hands-watch.jpg" alt=""/> </li> </ul> </div>
2. Add the following CSS styles within <style>
tag or external CSS file in your project. Modify the width, height, and color attributes in the CSS code to match your project’s design. Adjust the dimensions of the slider, images, and any other elements to suit your layout preferences.
* { margin: 0; padding: 0; font-family:ubuntu; } body { background-color: #ff4d4d; } .accord { width: 610px; margin: auto; margin-top: 100px; height: 400px; overflow: hidden; border: white 3px solid; background-color: white; transition: all 0.8s ease; } .accord ul li { width: 150px; display: block; float: left; position: relative; border-left: 2px solid white; height: 400px; transition: all 0.5s ease, opacity 0.2s ease; opacity: 0.8; backface-visibility: ; } .accord ul li img { width: 300px; height: inherit; } .title { transition: all 0.5s ease-in; position: absolute; bottom: 0; width: 300px; display: block; background-color: rgba(89, 89, 89, 0.4); } .title a { font-size: 110%; display: block; text-transform: capitalize; text-decoration: none; padding: 20px; color: white; } .accord:hover { box-shadow: 6px 6px 10px 5px grey; transform: scale(1.1, 1.1); } .accord ul:hover li { width: 100px; } .accord ul li:hover { width: 300px; opacity: 1; }
That’s all! hopefully, you have successfully created a simple image slider using HTML and CSS only. If you have any questions or suggestions, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.