The accordion on my homepage uses CSS3 only. No JavaScript is required!
CSS3-only Accordions!
With this tutorial I will show you how I implemented the accordion on the project page of my website. It is based on CSS3, requires no JavaScript and is fully responsive.
Yet another accordion?
Update: Scroll down to the end of the post for the final SASS code!
There is one main advantage of the approach presented here over others I found on the internet: The slides truly overlap each other. This prevents ugly text reflow during the animation (as seen in this prominent example, this one or this one) and allows for multiple lines of text in the captions (as opposed to this example).
Furthermore the horizontal accordion presented here is fully responsive - no fixed width is required; it will always fill 100% of its parent container (other accordions, such as this one or this one are hard-coded to a fixed width).
At first we will set up the basic behavior, then we will add background images and as a last step we will add captions. All style code will be provided as SASS code.
The basic bahavior
Here is the most basic version of the accordion. It has three items, each has a specific background color. If you hover over one slide it will expand, shrinking the others.
As you can see, the behavior is configured with three variables. $slides adjusts the number of supported entries, $accordion_aspect_ratio sets the aspect ratio of the accordion (when the size of the website changes, so will the accordion) and $slide_aspect_ratio. The latter describes the aspect ratio of the expanded slide and should not be set to a greater value than the $accordion_aspect_ratio.
Variable number of slides
Our next goal is to support any number of items. This can be achieved by replacing the bottom half of the style code above with a for-loop.
Background images
As a next step, we want to add pictures to our accordion. To achieve this, we set a background image for each slide and add a little bit of styling to the <li> element of our accordion.
Captions
Finally we want to have some captions on our slides. Since they should look really awesome we will add some transitions!
Lorem Ipsum
Lorem ipsum dolor sit amet, consectetur adipisicing elit. read on »
Lorem Ipsum
Lorem ipsum dolor sit amet, consectetur adipisicing elit. read on »
Lorem Ipsum
Lorem ipsum dolor sit amet, consectetur adipisicing elit. read on »
Lorem Ipsum
Lorem ipsum dolor sit amet, consectetur adipisicing elit. read on »
Final words
That’s it! I hope you can use this technique somewhere. If you have further questions just use the comment form below! And it would be awesome if you could post a link if you used this accordion somewhere! Below you find the final SASS class.