JoomlaVision > Web design > How to Create a Stylish Image Content Slider in Pure CSS3 [Tutorial]

How to Create a Stylish Image Content Slider in Pure CSS3 [Tutorial]

Topic: CSS3
Difficulty: Intermediate
Estimated completion time: 45 mins

In this tutorial we will create a CSS3 only image slider inspired on the Futurico User Interface by Vladimir Kudinov. The CSS3 features that we’ll be using in this tutorial are in tests in the most recent browsers so this slider will not work in all browsers (try preview in Chrome and Safari). I don’t recommend you to use it on your professional projects as this will not work properly, use this tutorial just to play around with the last CSS3 features that you will be able to use in the feature.

Step 1 – HTML

We’ll create two unordered lists, one for thumbnails and one for the images. To link the thumbnail to the respective image we will add an id for each image.

1 <div class="slider-wrapper">
2 <ul class="s-thumbs">
3     <li><a href="#slide-1"><img src="img/thumb1.png" alt="" /></a></li>
4     <li><a href="#slide-2"><img src="img/thumb2.png" alt="" /></a></li>
5     <li><a href="#slide-3"><img src="img/thumb3.png" alt="" /></a></li>
6     <li><a href="#slide-4"><img src="img/thumb4.png" alt="" /></a></li>
7     <li><a href="#slide-5"><img src="img/thumb5.png" alt="" /></a></li>
8     <li><a href="#slide-6"><img src="img/thumb6.png" alt="" /></a></li>
9     <li><a href="#slide-7"><img src="img/thumb7.png" alt="" /></a></li>
10 </ul>
11 <ul class="s-slides">
12     <li id="slide-1"><img src="img/slide1.png" alt="" /></li>
13     <li id="slide-2"><img src="img/slide2.png" alt="" /></li>
14     <li id="slide-3"><img src="img/slide3.png" alt="" /></li>
15     <li id="slide-4"><img src="img/slide4.png" alt="" /></li>
16     <li id="slide-5"><img src="img/slide5.png" alt="" /></li>
17     <li id="slide-6"><img src="img/slide6.png" alt="" /></li>
18     <li id="slide-7"><img src="img/slide7.png" alt="" /></li>
19 </ul>
20 </div>

Step 2 – Basic CSS Styling

In this step we’ll create the basic layout of our slider. To do that we will reset the margin and padding of all the slider elements, give them a fixed width / height and position them.

1 .slider-wrapper ul,
2 .slider-wrapper li,
3 .slider-wrapper div,
4 .slider-wrapper img,
5 .slider-wrapper a {
6     margin: 0;
7     padding: 0;
8     border: none;
9     outline: none;
10     list-style: none;
11 }
12
13 .slider-wrapper {
14     width: 508px;
15     overflow: hidden;
16 }
17
18 ul.s-thumbs li {
19     float: left;
20 }
21
22 ul.s-slides,
23 ul.s-slides li,
24 ul.s-slides a,
25 ul.s-slides img {
26     width: 500px;
27     height: 350px;
28     position: relative;
29 }
30
31 ul.s-slides {
32     overflow: hidden;
33     clear: both;
34 }
35
36 ul.s-slides li {
37     position: absolute;
38     z-index: 50;
39 }

Step 3 – CSS3 Styling

Now we will add the CSS3 Styles to make it look nicer. Here we will use some CSS3 properties like shadows, rounded corners, etc. You will notice that we will use various prefixes for each browser. This is required because these properties are in tests in some browsers and we need to add prefixes to target them.

1 ul.s-thumbs li {
2     float: left;
3     margin-bottom: 10px;
4     margin-right: 11px;
5 }
6
7 ul.s-thumbs li:last-child {
8     margin-left: 1px;
9     margin-right: 0;
10 }
11
12 ul.s-thumbs a {
13     display: block;
14     position: relative;
15     width: 55px;
16     height: 55px;
17     border: 4px solid transparent;
18
19     -webkit-transition: all 0.25s ease-in-out;
20     -moz-transition: all 0.25s ease-in-out;
21     -o-transition: all 0.25s ease-in-out;
22     -ms-transition: all 0.25s ease-in-out;
23     transition: all 0.25s ease-in-out;
24
25     font: bold 12px/25px Arial, sans-serif;
26     color: #515151;
27     text-decoration: none;
28     text-shadow: 1px 1px 0px rgba(255,255,255,.25), inset 1px 1px 0px rgba(0,0,0,.15);
29 }
30
31 ul.s-thumbs img {
32     -webkit-box-shadow: 1px 1px 5px rgba(0,0,0,.5);
33     -moz-box-shadow: 1px 1px 5px rgba(0,0,0,.5);
34     box-shadow: 1px 1px 5px rgba(0,0,0,.5);
35 }
36
37 ul.s-thumbs a:hover,
38 ul.s-slides {
39     border: 4px solid #141517;
40
41     -webkit-border-radius: 3px;
42     -moz-border-radius: 3px;
43     border-radius: 3px;
44
45     -webkit-box-shadow: 0px 1px 0px rgba(255,255,255,.05);
46     -moz-box-shadow: 0px 1px 0px rgba(255,255,255,.05);
47     box-shadow: 0px 1px 0px rgba(255,255,255,.05);
48 }

Step 4 – Image Description

In this step we will add an image description box to appear when hover over the image. We’ll create this using some CSS3 styles and the :before selector to add the arrow to the box.

HTML

To create the box add a span tag with some text to the anchor tag.

1 <ul class="s-thumbs">
2     <li><a href="#slide-1"><img src="img/thumb1.png" alt="" /><span>Image 1</span></a></li>
3     <li><a href="#slide-2"><img src="img/thumb2.png" alt="" /><span>Image 2</span></a></li>
4     <li><a href="#slide-3"><img src="img/thumb3.png" alt="" /><span>Image 3</span></a></li>
5     <li><a href="#slide-4"><img src="img/thumb4.png" alt="" /><span>Image 4</span></a></li>
6     <li><a href="#slide-5"><img src="img/thumb5.png" alt="" /><span>Image 5</span></a></li>
7     <li><a href="#slide-6"><img src="img/thumb6.png" alt="" /><span>Image 6</span></a></li>
8     <li><a href="#slide-7"><img src="img/thumb7.png" alt="" /><span>Image 7</span></a></li>
9 </ul>

CSS

To create the box we will add a fixed width to be able to center the box on the image and will and some shadows and gradients to make it look better. To create the arrow with only CSS we will use a border trick.

1 ul.s-thumbs li a:hover span {
2     position: absolute;
3     z-index: 101;
4     bottom: -30px;
5     left: -22px;
6     display: block;
7     width: 100px;
8     height: 25px;
9     text-align: center;
10
11     -webkit-border-radius: 3px;
12     -moz-border-radius: 3px;
13     border-radius: 3px;
14
15     -webkit-box-shadow: 0px 1px 0px rgba(0,0,0,.4);
16     -moz-box-shadow: 0px 1px 0px rgba(0,0,0,.4);
17     box-shadow: 0px 1px 0px rgba(0,0,0,.4);
18
19     -webkit-transition: all 0.25s ease-in-out;
20     -moz-transition: all 0.25s ease-in-out;
21     -o-transition: all 0.25s ease-in-out;
22     -ms-transition: all 0.25s ease-in-out;
23     transition: all 0.25s ease-in-out;
24
25     background: #ffffff; /* Old browsers */
26     background: -moz-linear-gradient(top#ffffff 0%, #bcbcbc 100%); /* FF3.6+ */
27     background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffffff), color-stop(100%,#bcbcbc)); /* Chrome,Safari4+ */
28     background: -webkit-linear-gradient(top#ffffff 0%,#bcbcbc 100%); /* Chrome10+,Safari5.1+ */
29     background: -o-linear-gradient(top#ffffff 0%,#bcbcbc 100%); /* Opera 11.10+ */
30     background: -ms-linear-gradient(top#ffffff 0%,#bcbcbc 100%); /* IE10+ */
31     background: linear-gradient(top#ffffff 0%,#bcbcbc 100%); /* W3C */
32 }
33
34 ul.s-thumbs li a:hover span:before {
35     width: 0;
36     height: 0;
37     border-bottom: 5px solid #ffffff;
38     border-left: 5px solid transparent;
39     border-right: 5px solid transparent;
40     content: '';
41     position: absolute;
42     top: -5px;
43     left: 44px;
44 }
45
46 ul.s-thumbs li:first-child a:hover span {
47     left: -3px;
48 }
49
50 ul.s-thumbs li:first-child a:hover span:before {
51     left: 25px;
52 }
53
54 ul.s-thumbs li:last-child a:hover span {
55     left: auto;
56     right: -3px;
57 }
58
59 ul.s-thumbs li:last-child a:hover span:before {
60     left: auto;
61     right: 26px;
62 }

Step 5 – Slider Transitions

Now we’ll start creating the slider transitions. As we’ll create a different transition to each image we need to add a different class name to the images.

HTML

1 <ul class="s-slides">
2     <li id="slide-1" class="slideLeft"><img src="img/slide1.png" alt="" /></li>
3     <li id="slide-2" class="slideRight"><img src="img/slide2.png" alt="" /></li>
4     <li id="slide-3" class="slideTop"><img src="img/slide3.png" alt="" /></li>
5     <li id="slide-4" class="slideBottom"><img src="img/slide4.png" alt="" /></li>
6     <li id="slide-5" class="zoomIn"><img src="img/slide5.png" alt="" /></li>
7     <li id="slide-6" class="zoomOut"><img src="img/slide6.png" alt="" /></li>
8     <li id="slide-7" class="rotate"><img src="img/slide7.png" alt="" /></li>
9 </ul>

CSS

To create the transitions we will use @keyframes. The animation is created by gradually changing from one set of CSS styles to another. To specify when the animation will start, change and finish we’ll use percentages, 0% is the beginning of the animation and 100% is when the animation is complete. Let’s start creating this animations..

Slide from left

At the beginning of the animation the image will be positioned negative 500px left and when the animation completes the image will be positioned 0px left. By setting the animation duration to 1 second we will get a slide effect from left to the right.

1 /* Slide Left */
2
3 @-webkit-keyframes 'slideLeft' {
4     0% { left: -500px; }
5     100% { left: 0; }
6 }
7
8 ul.s-slides li.slideLeft:target {
9     z-index: 100;
10
11     -webkit-animation-name: slideLeft;
12     -webkit-animation-duration: 1s;
13     -webkit-animation-iteration-count: 1;
14 }

Slide From Right

1 /* Slide Right */
2
3 @-webkit-keyframes 'slideRight' {
4     0% { left: 500px; }
5     100% { left: 0; }
6 }
7
8 ul.s-slides li.slideRight:target {
9     z-index: 100;
10
11     -webkit-animation-name: slideRight;
12     -webkit-animation-duration: 1s;
13     -webkit-animation-iteration-count: 1;
14 }

Slide From Top

1 /* Slide Top */
2
3 @-webkit-keyframes 'slideTop' {
4     0% { top: -350px; }
5     100% { top: 0; }
6 }
7
8 ul.s-slides li.slideTop:target {
9     z-index: 100;
10
11     -webkit-animation-name: slideTop;
12     -webkit-animation-duration: 1s;
13     -webkit-animation-iteration-count: 1;
14 }

Slide From Bottom

1 /* Slide Bottom */
2
3 @-webkit-keyframes 'slideBottom' {
4     0% { top: 350px; }
5     100% { top: 0; }
6 }
7
8 ul.s-slides li.slideBottom:target {
9     z-index: 100;
10
11     -webkit-animation-name: slideBottom;
12     -webkit-animation-duration: 1s;
13     -webkit-animation-iteration-count: 1;
14 }

Zoom Image From Inside

To make the zoom transition we will use the CSS3 transform property. At the beginning of the animation the image size will be 10% and when the animation completes it will have the original image size. We’ll position it 4px from the top to fix a space that appears at the bottom.

1 /* Zoom In */
2
3 @-webkit-keyframes 'zoomIn' {
4     0% { -webkit-transform: scale(0.1); }
5     100% { -webkit-transform: none; }
6 }
7
8 ul.s-slides li.zoomIn:target {
9     z-index: 100;
10     top: 4px;
11
12     -webkit-animation-name: zoomIn;
13     -webkit-animation-duration: 1s;
14     -webkit-animation-iteration-count: 1;
15 }

Zoom Image From Outside

1 /* Zoom Out */
2
3 @-webkit-keyframes 'zoomOut' {
4     0% { -webkit-transform: scale(2); }
5     100% { -webkit-transform: none; }
6 }
7
8 ul.s-slides li.zoomOut:target {
9     z-index: 100;
10
11     -webkit-animation-name: zoomOut;
12     -webkit-animation-duration: 1s;
13     -webkit-animation-iteration-count: 1;
14 }

Rotate and Zoom the Image

1 /* Rotate */
2
3 @-webkit-keyframes 'rotate' {
4     0% { -webkit-transform: rotate(-360deg) scale(0.1); }
5     100% { -webkit-transform: none; }
6 }
7
8 ul.s-slides li.rotate:target {
9     z-index: 100;
10     top: 4px;
11
12     -webkit-animation-name: rotate;
13     -webkit-animation-duration: 1s;
14     -webkit-animation-iteration-count: 1;
15 }

Step 6 – “Not Target” Image

As we are using z-index to set what image to appear on the top we are getting a bug when we click to show a new image. When you click in a new thumbnail the previous active slide disappears and it shows the last slide during the new slide transition. To fix that we need to add a not-target style with a higher z-index during the new slide transition.

1 /* Not Target */
2
3 @-webkit-keyframes 'notTarget' {
4     0% { z-index: 75; }
5     100% { z-index: 75; }
6 }
7
8 ul.s-slides li:not(:target) {
9     -webkit-animation-name: notTarget;
10     -webkit-animation-duration: 1s;
11     -webkit-animation-iteration-count: 1;
12 }

Step 7 – First Visible Slider

When the slider is load it will show the last slider image. To be able to choose the first visible slider image we’ll create a class with a higher z-index and you will only need to add this class to the slider you want to be visible when the slider loads.

1 /* First Slide */
2
3 ul.s-slides li.first {
4     z-index: 60;
5 }

Step 8 – Slider Load

In this step we will add a load fade in animation to the slider because when it will load with the page it will wait 1 second to change to the first slide. We don’t want to see this transition and we will hide it for 1 second and then we will fade in.

1 /* Slider Load */
2
3 @-webkit-keyframes 'load' {
4     0% { opacity: 0; }
5     50% { opacity: 0; }
6     100% { opacity: 1; }
7 }
8
9 .slider-wrapper {
10     -webkit-animation-name: load;
11     -webkit-animation-duration: 2s;
12     -webkit-transition-timing-function: ease-in-out;
13     -webkit-animation-iteration-count: 1;
14 }

Conclusio

Congratulations, you have finished this tutorial! As we have used a lot of CSS3 features that are only supported by the last versions of the browsers this slider is not a good to use on your professional projects yet. I’ve made this just to show you a sneak peek of what we will could do in the feature just with CSS3. Hope you like it and don’t forget to live some feedback or any question you have in the comments.

Related posts