Previously, I’ve demonstrated how to make a fixed-width CSS image slider. Increasingly, web developers desire solutions that not only scale across viewport sizes, but perform well on mobile devices. The responsive solution detailed here is particularly well-suited to those goals, as it avoids JavaScript entirely, running purely in CSS3 (and thus faster, smoother, and with less overhead).
The idea is very similar to the previous example: an “imagestrip” containing all the photographs of our slider moving inside a window element which works to restrict the visibility of elements outside it. For this example, you’ll need four images, although in practice you could make a strip with as many images as you wished. The sole condition is that all the images must be exactly the same size.
Create A Responsive Frame
First, we need to make the window element responsive. We do this by inserting a copy of one of the images from the slider inside the element, and setting an appropriate inline style:
- <div id="window">
- <img src="austin-fireworks.jpg" alt=""
- style="max-width: 100%;">
- </div>
(I’m leaving the alt attribute blank to save on space; in production, it would be filled out appropriately for accessibility and SEO purposes).
The window is given a max-width that corresponds to the natural width of the image (1000px, in the case of the example), as we don’t want to show more of the imagestrip than one image. All images are floated left to remove space at the bottom of their containers.
- div#window { max-width: 1000px;
- position: relative; overflow: hidden; }
- img { float: left; }
Design a Strip of Images
With the placeholder image added, we can create the interior imagestrip. This will immediately obscure the placeholder, but that’s okay: the sole purpose of the placeholder is to make the window responsive; it doesn’t need to be visible.
Our HTML becomes:
- <div id="window">
- <img src="austin-fireworks.jpg" alt=""
- style="max-width: 100%;">
- <figure id="imagestrip">
- <img src="austin-fireworks.jpg" alt="">
- <img src="taj-mahal.jpg" alt="">
- <img src="ibiza.jpg" alt="">
- <img src="ankor-wat.jpg" alt="">
- <img src="austin-fireworks.jpg" alt="">
- </figure>
- </div>
Note that the same image is placed at the start and end of the strip, allowing the animation to repeat in a smooth loop.
In our CSS, the width of #window is described as a percentage multiplier of the div that contains it. That is, if imagestrip contains five images, and the div shows just one, the figure is 5x wider, or 500% the width of the container div. It’s also positioned absolutely, enabling us to describe its position relative to its container.
- figure#imagestrip {
- position: absolute; width: 500%; margin: 0;
- top: 0; left: 0;
- }
We have to evenly divide the photographs inside the imagestrip. We could use a layout such as flexbox to do so, but the math in standard CSS is very simple. If we consider the figure element to be 100% wide, each image needs to take up 1/5 of the horizontal space:
100% ÷ 5 = 20%
Which leads to the following CSS declaration:
- figure#imagestrip img { float: left; width: 20%; }
Animate The Strip
Finally, we have to get the imagestrip moving from left to right. If the containing div is 100% wide, then each movement of the imagestrip to the left will be measured in increments of that distance:
- @keyframes slidy {
- 20% { left: 0%; }
- 25% { left: -100%; }
- 45% { left: -100%; }
- 50% { left: -200%; }
- 70% { left: -200%; }
- 75% { left: -300%; }
- 95% { left: -300%; }
- 100% { left: -400%; }
- }
Each image in the slider will stay framed in the div for 20% of the total length of the animation, and move for 5%.
We have to call on the animation to get things started:
- figure { position: absolute; width: 500%;
- margin: 0; top: 0; left: 0;
- animation: 30s slidy ease-in-out infinite; }
As you can see, making a responsive slider is in many ways easier than making a fixed one.
(Both the keyframes and animation code is shown without vendor prefixes in order to save on space – you can resize your browser window to see the effect on the slider. Images by the always-excellent Trey Radcliff, used with permission).
Haha, that is actually incredibly clever.
![Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy] Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]](http://ecx.images-amazon.com/images/I/5192I1rtYnL._SL160_.jpg)

