Sitemap
gradinariu bogdan

gradinariu bogdan

javascript developer, piano player

CSS only image slideshow for your website

4 min readDec 20, 2016

--

I know that this topic might not be as hot as other cutting edge technological breakthroughs that alters the today’s front-end world (such as ES6, CSS4 or any other new features), but I want to share my experience on creating a css slideshow without altering any DOM or writing any JS.

TL;DR.

Here’s what I ended up using as a css image slideshow on my site: http://codepen.io/gion/full/MbLVZR/

Continue reading for the how and why.

Why

I thought that adding a simple image slideshow in my website would be a trivial job, I started googling for “web image slideshow” and, as expected, my screen was flooded with lots of results that either pointed me to jQuery slider plugins or tutorials on how to build your own javascript based image slideshow.

Since I didn’t want to add other JS dependencies and/or drastically alter the HTML structure of my existing site, I refined the search to “css only slideshow” or similar queries. Unfortunately, that didn’t help and I was stuck with either adding some 3rd party slider libraries or altering my markup to include all the images that would magically be animated with CSS.

And I went with the secret option #3: write my own CSS image slideshow that doesn’t rely on any JS and does not alter the HTML markup.

How

1. I don’t want any other JS/HTML

Ok, no JS means that we only need CSS for displaying, styling and animating the images. That’s something that any intermediate front end developer should be able to do with some help from google. But no HTML?

I originally thought I could go away with only using multiple background images on an element:

Press enter or click to view image in full size

And here’s my first take:

Pretty neat and simple for this simple use case where you want the images to slide. This means that the pictures couldn’t crossfade :(

2. Pseudo elements to the rescue!

Because I needed to animate the images independently (fade/zoom/translate) and not just rely on animating the background position, I actually needed 2 image containers which translates to 2 DOM elements. Luckily we now about :before and :after pseudo elements and can use a single DOM node to represent at least 2 different things:

Press enter or click to view image in full size
pseudo elements

Here’s the previous example pumped up with the power of pseudo elements:

This is great, but I still wanted more than 2 images :(

3. Pseudo elements with changing images

I decided to go with the pseudo elements for displaying images and just swap images on each animation step.

Press enter or click to view image in full size

This way, I can animate seamlessly between the 2 layers (:before and :after) and while one of them is active, I can swap the image on the other so I can have more than 2 images.

Get gradinariu bogdan’s stories in your inbox

Join Medium for free to get updates from this writer.

Since I want to control these animations from CSS, I’m going to use CSS animations for the slide change effect & logic:

This is great… but not quite :(

Aside of the fact that there are still some glitches that can maybe be tweaked, if you’re running the above example in Firefox, you might not see anything. This is because the background-image property is not animatable ( as stated in the specs), even though webkit based browsers seem to support it.

4. Multiple background images on pseudo elements

I needed to reconsider multiple backgrounds and push the slideshow forward to have multiple images :before and :after the targeted element:

Press enter or click to view image in full size

Combining the 2 previous steps, I ended up with this:

The above example works because that the transition only targets opacity in order to avoid image flickers while changing the steps. The challenge now is to implement it with css animations and lose the html/js helpers in the demo.

First of all, I defined the keyframes that fade in/out each one of the background images by animating the opacity.

In order to reuse the same animation for both pseudo elements, I added a delay to the second one so that when the first animation is fading out the image, the second animation should fade it in:

Press enter or click to view image in full size

I realised that if I can’t animate the background-image, I can get things done by animating other background properties such as background-size. But with animations every style change gets animated… even the background-size that should just instantly change the image.

To get around this issue, I added a short extra step in the animation that only change the background-size after the image is not visible.

And, in order to spice things up, I animated some other props to enhance the slideshow effects.

And the result… [drumrolls]:

Well, that’s it for my approach on generating a pure CSS image slideshow. It works cross browser and can be used as a full background image slideshow. The CSS can get complicated, but if you’re using a preprocessor, things can get pretty easy with some mixins.

Have an opinion? Feel free to comment & share.

--

--

Written by gradinariu bogdan

javascript developer, piano player