Today I’ll tell you about another one great thing that you can implement with CSS3 – it’s an accordion. I think that accordion is still pretty popular in web. This is nice and compact way to keep some information at page. Of course, we always can use javascript (jQuery) plugins in order to set accordion, but sometimes this is not so necessary. We can use only CSS3 to achieve the same effect, today I will tell you exactly how this can be done.
Here are samples and downloadable package:
[sociallocker]
[/sociallocker]
Ok, download the example files and lets start coding !
Step 1. HTML Markup
Here are html source code of our demo:
index.html
01 | <div class="accordion"> |
02 | <span id="tab1"></span> |
03 | <span id="tab2"></span> |
04 | <span id="tab3"></span> |
05 | <span id="tab4"></span> |
09 | <a href="#tab1">Tab #1</a> |
10 | <div><p>Cascading Style Sheets (CSS) is a style sheet language used to describe the presentation semantics (the look and formatting) of a document written in a markup language. Its most common application is to style web pages written in HTML and XHTML, but the language can also be applied to any kind of XML document, including plain XML, SVG and XUL.</p></div> |
15 | <a href="#tab2">Tab #2</a> |
25 | <a href="#tab3">Tab #3</a> |
26 | <div><p>CSS is designed primarily to enable the separation of document content (written in HTML or a similar markup language) from document presentation, including elements such as the layout, colors, and fonts.[1] This separation can improve content accessibility, provide more flexibility and control in the specification of presentation characteristics, enable multiple pages to share formatting, and reduce complexity and repetition in the structural content (such as by allowing for tableless web design). CSS can also allow the same markup page to be presented in different styles for different rendering methods, such as on-screen, in print, by voice (when read out by a speech-based browser or screen reader) and on Braille-based, tactile devices. It can also be used to allow the web page to display differently depending on the screen size or device on which it is being viewed. While the author of a document typically links that document to a CSS style sheet, readers can use a different style sheet, perhaps one on their own computer, to override the one the author has specified.</p></div> |
31 | <a href="#tab4">Tab #4</a> |
32 | <div><p><img src="images/icons.jpg" alt="" /></p></div> |
We will use list of hidden span objects, and list of custom tabs, where you can put everything you want (any custom html content). Every tab have own ID (in order to slide).
Step 2. CSS
Here are the CSS styles of our accordion:
css/accordion.css
11 | background-color: #FFFFFF; |
15 | background-color: #C8CEFF; |
17 | border-color:#ccc;border-bottom-color:#aaa; |
22 | text-decoration: none; |
23 | filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr=#ffffffff,EndColorStr=#ffe0e0e0); |
24 | background-image: -moz-linear-gradient(top,#fff 0,#e0e0e0 100%); |
25 | background-image: -ms-linear-gradient(top,#fff 0,#e0e0e0 100%); |
26 | background-image:-o-linear-gradient(top,#fff 0,#e0e0e0 100%); |
27 | background-image: -webkit-gradient(linear,left top,left bottom,color-stop(0,#fff),color-stop(100%,#e0e0e0)); |
28 | background-image: -webkit-linear-gradient(top,#fff 0,#e0e0e0 100%); |
29 | background-image: linear-gradient(to bottom,#fff 0,#e0e0e0 100%); |
30 | -moz-transition: 0.3s; |
33 | -webkit-transition: 0.3s; |
37 | background-color: #FFF; |
40 | box-shadow: 0 0 1px rgba(0, 0, 0, 1) inset; |
41 | -moz-box-shadow: 0 0 1px rgba(0, 0, 0, 1) inset; |
42 | -webkit-box-shadow: 0 0 1px rgba(0, 0, 0, 1) inset; |
43 | -webkit-transition: all 0.3s ease-in-out; |
44 | -moz-transition: all 0.3s ease-in-out; |
45 | -o-transition: all 0.3s ease-in-out; |
46 | -ms-transition: all 0.3s ease-in-out; |
47 | transition: all 0.3s ease-in-out; |
56 | box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.5) inset; |
57 | -moz-box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.5) inset; |
58 | -webkit-box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.5) inset; |
60 | .tabs dl dd a:active { |
61 | filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr=#e6e6e6,EndColorStr=#dcdcdc); |
62 | background-image: -moz-linear-gradient(top,#e6e6e6 0,#dcdcdc 100%); |
63 | background-image: -ms-linear-gradient(top,#e6e6e6 0,#dcdcdc 100%); |
64 | background-image: -o-linear-gradient(top,#e6e6e6 0,#dcdcdc 100%); |
65 | background-image: -webkit-gradient(linear,left top,left bottom,color-stop(0,#e6e6e6),color-stop(100%,#dcdcdc)); |
66 | background-image: -webkit-linear-gradient(top,#e6e6e6 0,#dcdcdc 100%); |
67 | background-image: linear-gradient(to bottom,#e6e6e6 0,#dcdcdc 100%); |
69 | #tab1:target ~ .tabs .tab1 dd div { |
72 | #tab2:target ~ .tabs .tab2 dd div { |
75 | #tab3:target ~ .tabs .tab3 dd div { |
78 | #tab4:target ~ .tabs .tab4 dd div { |
81 | #tab1:target ~ .tabs .tab1 dd a, |
82 | #tab2:target ~ .tabs .tab2 dd a, |
83 | #tab3:target ~ .tabs .tab3 dd a, |
84 | #tab4:target ~ .tabs .tab4 dd a { |
85 | filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr=#e6e6e6,EndColorStr=#dcdcdc); |
86 | background-image: -moz-linear-gradient(top,#e6e6e6 0,#dcdcdc 100%); |
87 | background-image: -ms-linear-gradient(top,#e6e6e6 0,#dcdcdc 100%); |
88 | background-image: -o-linear-gradient(top,#e6e6e6 0,#dcdcdc 100%); |
89 | background-image: -webkit-gradient(linear,left top,left bottom,color-stop(0,#e6e6e6),color-stop(100%,#dcdcdc)); |
90 | background-image: -webkit-linear-gradient(top,#e6e6e6 0,#dcdcdc 100%); |
91 | background-image: linear-gradient(to bottom,#e6e6e6 0,#dcdcdc 100%); |
92 | box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.5) inset; |
93 | -moz-box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.5) inset; |
94 | -webkit-box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.5) inset; |
How to slide – pretty easy: I have added transition for the height of our tabs.
Conclusion
Thats all. 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!