Today I’ll tell you about a remarkable thing that you can implement with CSS3 – it’s a slider. I think you may have noticed that some time ago was very popular variety of sliders, and many chose their decision to use jQuery easySlider example. But it appears the same tasks can be implemented using pure CSS3, today I will tell you exactly how this can be done.
Here are samples and downloadable package:
Live Demo
[sociallocker]
download in package
[/sociallocker]
Ok, download the example files and lets start coding !
Step 1. HTML
Here are html source code of our demo. Pay attention how we built our slider. To implement the desired effect – I’ve put our slides followed one another (float: left). And put the correct links.
index.html
01 | <!DOCTYPE html> |
02 | < html lang = "en" > |
03 | < head > |
04 | < meta charset = "utf-8" /> |
05 | < title >Pure CSS3 Slider | Script Tutorials</ title > |
06 | < link rel = "stylesheet" href = "css/layout.css" type = "text/css" media = "screen" > |
07 | < link rel = "stylesheet" href = "css/slider.css" type = "text/css" media = "screen" > |
08 | </ head > |
09 | < body > |
10 | < header > |
11 | < h2 >Pure CSS3 Slider</ h2 > |
12 | < a href = "https://www.script-tutorials.com/pure-css3-slider/" class = "stuts" >Back to original tutorial on < span >Script Tutorials</ span ></ a > |
13 | </ header > |
14 | < div class = "container" > |
15 | < div class = "slider" > |
16 | < span id = "root" ></ span > |
17 | < span id = "parent1" ></ span > |
18 | < span id = "parent2" ></ span > |
19 | < span id = "parent3" ></ span > |
20 | < span id = "child1" ></ span > |
21 | < span id = "child2" ></ span > |
22 | < span id = "subchild1" ></ span > |
23 | < div id = "tabs" > |
24 | < dl > |
25 | < dd > |
26 | < a class = "fwd" href = "#parent1" >Parent #1</ a > |
27 | < a class = "fwd" href = "#parent2" >Parent #2</ a > |
28 | < a class = "fwd" href = "#parent3" >Parent #3</ a > |
29 | </ dd > |
30 | </ dl > |
31 | < dl class = "parent1" > |
32 | < dd > |
33 | < a class = "fwd" href = "#child1" >Child #1#1</ a > |
34 | < a class = "fwd" href = "#child2" >Child #1#2</ a > |
35 | < a href = "#" >Child #1#3</ a > |
36 | < a href = "#" >Child #1#4</ a > |
37 | < a href = "#" >Child #1#5</ a > |
38 | </ dd > |
39 | < dd >< a href = "#root" >Back</ a ></ dd > |
40 | </ dl > |
41 | < dl class = "parent2" > |
42 | < dd > |
43 | < a href = "#" >Child #2#1</ a > |
44 | < a href = "#" >Child #2#2</ a > |
45 | < a href = "#" >Child #2#3</ a > |
46 | < a href = "#" >Child #2#4</ a > |
47 | < a href = "#" >Child #2#5</ a > |
48 | </ dd > |
49 | < dd >< a href = "#root" >Back</ a ></ dd > |
50 | </ dl > |
51 | < dl class = "parent3" > |
52 | < dd > |
53 | < a href = "#" >Child #3#1</ a > |
54 | < a href = "#" >Child #3#2</ a > |
55 | < a href = "#" >Child #3#3</ a > |
56 | < a href = "#" >Child #3#4</ a > |
57 | < a href = "#" >Child #3#5</ a > |
58 | </ dd > |
59 | < dd >< a href = "#root" >Back</ a ></ dd > |
60 | </ dl > |
61 | < dl class = "child1" > |
62 | < dd > |
63 | < a class = "fwd" href = "#subchild1" >SubChild #1#1</ a > |
64 | < a href = "#" >SubChild #1#2</ a > |
65 | < a href = "#" >SubChild #1#3</ a > |
66 | < a href = "#" >SubChild #1#4</ a > |
67 | < a href = "#" >SubChild #1#5</ a > |
68 | </ dd > |
69 | < dd >< a href = "#parent1" >Back</ a ></ dd > |
70 | </ dl > |
71 | < dl class = "child2" > |
72 | < dd > |
73 | < a href = "#" >SubChild #2#1</ a > |
74 | < a href = "#" >SubChild #2#2</ a > |
75 | < a href = "#" >SubChild #2#3</ a > |
76 | < a href = "#" >SubChild #2#4</ a > |
77 | < a href = "#" >SubChild #2#5</ a > |
78 | </ dd > |
79 | < dd >< a href = "#parent1" >Back</ a ></ dd > |
80 | </ dl > |
81 | < dl class = "subchild1" > |
82 | < dd > |
83 | < a href = "#" >Links #1</ a > |
84 | < a href = "#" >Links #2</ a > |
85 | < a href = "#" >Links #3</ a > |
86 | </ dd > |
87 | < dd >< a href = "#child1" >Back</ a ></ dd > |
88 | </ dl > |
89 | </ div > |
90 | </ div > |
91 | </ div > |
92 | </ body > |
93 | </ html > |
Step 2. CSS
Here are the CSS styles of our slider. Maybe you’ve noticed – that in our html we have two CSS files: layout.css and menu.css. The first file (layout.css) contain the styles of our demo page. We will not publish these styles in this article, but if you wish – you can find these styles in the package.
css/slider.css
001 | .slider { |
002 | background-color : #FFFFFF ; |
003 | border : 1px solid #000000 ; |
004 | margin : 50px auto ; |
005 | overflow : hidden ; |
006 | padding : 10px ; |
007 | position : relative ; |
008 | width : 200px ; |
009 | } |
010 | .slider span { |
011 | display : none |
012 | } |
013 | #tabs { |
014 | background-color : #FFFFFF ; |
015 | margin-left : 0 ; |
016 | overflow : hidden ; |
017 | width : 2000px ; |
018 | -moz-transition: 0.5 s; |
019 | -ms-transition: 0.5 s; |
020 | -o-transition: 0.5 s; |
021 | -webkit-transition: 0.5 s; |
022 | transition: 0.5 s; |
023 | } |
024 | #tabs dl { |
025 | float : left ; |
026 | height : 300px ; |
027 | margin : 0 ; |
028 | opacity: 0 ; |
029 | padding : 0 ; |
030 | position : relative ; |
031 | width : 200px ; |
032 | } |
033 | #tabs dl:first-child { |
034 | opacity: 1 ; |
035 | } |
036 | #tabs dl dd a.fwd { |
037 | background-color : #C8CEFF ; |
038 | } |
039 | #tabs dl dd a { |
040 | background-color : #F4F5FE ; |
041 | color : #000000 ; |
042 | display : block ; |
043 | font-size : 18px ; |
044 | line-height : 32px ; |
045 | margin : 10px ; |
046 | text-align : center ; |
047 | text-decoration : none ; |
048 | border-radius: 10px ; |
049 | -moz-border-radius: 10px ; |
050 | -ms-border-radius: 10px ; |
051 | -o-border-radius: 10px ; |
052 | -webkit-border-radius: 10px ; |
053 | box-shadow: 0px 0px 7px rgba( 0 , 0 , 0 , 0.4 ) inset , 0px 0px 0px 4px rgba( 255 , 255 , 255 , 0.1 ); |
054 | -moz-box-shadow: 0px 0px 7px rgba( 0 , 0 , 0 , 0.4 ) inset , 0px 0px 0px 4px rgba( 255 , 255 , 255 , 0.1 ); |
055 | -ms-box-shadow: 0px 0px 7px rgba( 0 , 0 , 0 , 0.4 ) inset , 0px 0px 0px 4px rgba( 255 , 255 , 255 , 0.1 ); |
056 | -o-box-shadow: 0px 0px 7px rgba( 0 , 0 , 0 , 0.4 ) inset , 0px 0px 0px 4px rgba( 255 , 255 , 255 , 0.1 ); |
057 | -webkit-box-shadow: 0px 0px 7px rgba( 0 , 0 , 0 , 0.4 ) inset , 0px 0px 0px 4px rgba( 255 , 255 , 255 , 0.1 ); |
058 | -moz-transition: 0.3 s; |
059 | -ms-transition: 0.3 s; |
060 | -o-transition: 0.3 s; |
061 | -webkit-transition: 0.3 s; |
062 | transition: 0.3 s; |
063 | } |
064 | #tabs dl dd:first-child { |
065 | height : 250px ; |
066 | overflow : auto ; |
067 | border-radius: 5px ; |
068 | -moz-border-radius: 5px ; |
069 | -ms-border-radius: 5px ; |
070 | -o-border-radius: 5px ; |
071 | -webkit-border-radius: 5px ; |
072 | } |
073 | #tabs dl dd a:hover { |
074 | box-shadow: 0px 0px 4px rgba( 0 , 0 , 0 , 0.5 ) inset , 0px 0px 0px 4px rgba( 51 , 51 , 204 , 0.5 ); |
075 | -moz-box-shadow: 0px 0px 4px rgba( 0 , 0 , 0 , 0.5 ) inset , 0px 0px 0px 4px rgba( 51 , 51 , 204 , 0.5 ); |
076 | -webkit-box-shadow: 0px 0px 4px rgba( 0 , 0 , 0 , 0.5 ) inset , 0px 0px 0px 4px rgba( 51 , 51 , 204 , 0.5 ); |
077 | } |
078 | #root:target ~ #tabs { |
079 | margin-left : 0 ; |
080 | } |
081 | #parent 1: target ~ #tabs { |
082 | margin-left : -200px ; |
083 | } |
084 | #parent 2: target ~ #tabs { |
085 | margin-left : -400px ; |
086 | } |
087 | #parent 3: target ~ #tabs { |
088 | margin-left : -600px ; |
089 | } |
090 | #child 1: target ~ #tabs { |
091 | margin-left : -800px ; |
092 | } |
093 | #child 2: target ~ #tabs { |
094 | margin-left : -1000px ; |
095 | } |
096 | #subchild 1: target ~ #tabs { |
097 | margin-left : -1200px ; |
098 | } |
099 | #parent 1: target ~ #tabs .parent 1 { |
100 | opacity: 1 ; |
101 | } |
102 | #parent 2: target ~ #tabs .parent 2 { |
103 | opacity: 1 ; |
104 | } |
105 | #parent 3: target ~ #tabs .parent 3 { |
106 | opacity: 1 ; |
107 | } |
108 | #child 1: target ~ #tabs .child 1 { |
109 | opacity: 1 ; |
110 | } |
111 | #child 2: target ~ #tabs .child 2 { |
112 | opacity: 1 ; |
113 | } |
114 | #subchild 1: target ~ #tabs .subchild 1 { |
115 | opacity: 1 ; |
116 | } |
Live Demo
Conclusion
Looks great, isn’t it? If you have got any good ideas you would like to share, be sure to drop those in the comments as well. Good luck!