Today we will create an elegant accordion for content. The idea is to have some vertical accordion tabs that slide out when hovering. We will add some CSS3 properties to enhance the looks.
Ok, let’s start with the markup.
The Markup
The HTML will consist of a list where each accordion tab is a list element. Inside of the list element we will have a heading that disappears when we hover. Also, there will an element for the white background gradient and a div for the description. Here is the markup with just one list element:
<ul class="accordion" id="accordion"> <li class="bg1"> <div class="heading">Heading</div> <div class="bgDescription"></div> <div class="description"> <h2>Heading</h2> <p>Some descriptive text</p> <a href="#">more ?</a> </div> </li> </ul>
The other list elements will have the classes bg2, bg3 and bg4. The unordered list gets a class and an id since we want to address it later in the JavaScript. Of course, we would not really need the class but instead, style the element using #accordion in our style sheet. But this is a good way to keep things separate.
The CSS
The style for the list will look as follows:
ul.accordion{
list-style:none;
position:absolute;
right:80px;
top:0px;
font-family: Cambria, serif;
font-size: 16px;
font-style: italic;
line-height: 1.5em;
}
The list elements will each have a different background image, so we will first define the general style properties for every list element and then the single classes with the background images:
ul.accordion li{
float:right;
width:115px;
height:480px;
display:block;
border-right:2px solid #fff;
border-bottom:2px solid #fff;
background-color:#fff;
background-repeat:no-repeat;
background-position:center center;
position:relative;
overflow:hidden;
cursor:pointer;
-moz-box-shadow:1px 3px 15px #555;
-webkit-box-shadow:1px 3px 15px #555;
box-shadow:1px 3px 15px #555;
}
ul.accordion li.bg1{
background-image:url(../images/1.jpg);
}
ul.accordion li.bg2{
background-image:url(../images/2.jpg);
}
ul.accordion li.bg3{
background-image:url(../images/3.jpg);
}
ul.accordion li.bg4{
background-image:url(../images/4.jpg);
}
Note, that the box shadow will not work in IE.
The initial width of each item will be 115 pixels. We will alter this in the JavaScript hover function (to 480px).
The border that we give each list element can only be applied to one side, otherwise we would have a double border in between the items and just a single one at the outer limits. So, we need to define a class for a left border, that we will apply to the last list element (they are floating right, so the order is reversed):
ul.accordion li.bleft{
border-left:2px solid #fff;
}
Now, we want a nice looking header with a white semi-transparent background for each list item when in the normal “closed” state:
ul.accordion li .heading{
background-color:#fff;
padding:10px;
margin-top:60px;
opacity:0.9;
text-transform:uppercase;
font-style:normal;
font-weight:bold;
letter-spacing:1px;
font-size:14px;
color:#444;
text-align:center;
text-shadow:-1px -1px 1px #ccc;
}
The div that contains the description will have the following style:
ul.accordion li .description{
position:absolute;
width:480px;
height:175px;
bottom:0px;
left:0px;
display:none;
}
We set it to display:none initially, since we only want it to appear when hovering. And here is, how we style the inner elements:
ul.accordion li .description h2{
text-transform:uppercase;
font-style:normal;
font-weight:bold;
letter-spacing:1px;
font-size:45px;
color:#444;
text-align:left;
margin:0px 0px 15px 20px;
text-shadow:-1px -1px 1px #ccc;
}
ul.accordion li .description p{
line-height:14px;
margin:10px 22px;
font-family: "Trebuchet MS", sans-serif;
font-size: 12px;
font-style: italic;
font-weight: normal;
text-transform: none;
letter-spacing: normal;
line-height: 1.6em;
}
ul.accordion li .description a{
position:absolute;
bottom:5px;
left:20px;
text-transform:uppercase;
font-style:normal;
font-size:11px;
text-decoration:none;
color:#888;
}
ul.accordion li .description a:hover{
color:#333;
text-decoration:underline;
}
The only style missing is the one for the gradient element:
ul.accordion li .bgDescription{
background:transparent url(../images/bgDescription.png) repeat-x top left;
height:340px;
position:absolute;
bottom:0px;
left:0px;
width:100%;
display:none;
}
The image that we are repeating is a gradient that goes from white to transparent. This will give a nice effect when we make this div slide in from the bottom.
Let’s add some magic!
The JavaScript
First, we include the jQuery library before the body end tag:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
Now, we will add a function that makes a list element slide out when hovering over it. This will be achieved by animating the increase of the width to 480 pixels. Then the top heading should fade out while the gradient (css class “bgDescription”) and the description appear:
<script type="text/javascript">
$(function() {
$('#accordion > li').hover(
function () {
var $this = $(this);
$this.stop().animate({'width':'480px'},500);
$('.heading',$this).stop(true,true).fadeOut();
$('.bgDescription',$this).stop(true,true).slideDown(500);
$('.description',$this).stop(true,true).fadeIn();
},
function () {
var $this = $(this);
$this.stop().animate({'width':'115px'},1000);
$('.heading',$this).stop(true,true).fadeIn();
$('.description',$this).stop(true,true).fadeOut(500);
$('.bgDescription',$this).stop(true,true).slideUp(700);
}
);
});
</script>
The first function inside of $(‘#accordion > li’).hover is the function triggered when the mouse moves over the respective element and the second function gets triggered when moving the mouse out again. Here we reverse the actions and also add some timing to it, so that the elements disappear smoothly.
And that’s all! I hope you enjoyed this tutorial and find it useful!

@David, try this: in the inline style of the li with class bg4 set the width to 480px. Then set the display of the div with class heading to “none” and remove the inline style of the next two divs that say display:none. That should make the first one being open at the beginning. It will close then again once you hover over the li elements. I am not sure if it’s what you want. Let me know, cheers, ML
@MARY LOU
GREAT EFFECT!!! THANKY YOU VERY MUCH!!!!
I want to expand the accordion with 8 elements with a width of 960px and a height of 480px.
My problem ist, that the “on hover effect” slides the elements on the bottom and returns
How can I have smooth slide effect without the “bottom effect”?
Once again: BIG THANX for this slider!!!
Thank you
this is crazy, thank u very much. I love jquery
Amazing i just added a link to your script from my website in both languages , spanish and english
http://www.ajaxshake.com/en/JS/1111/free-js-jquery-plugins.html
Thanks
It Amazing. I intend to write a Joomla module based on it!
A question: Is it possible to have one of the strip open from the beginning?
It will make the width constant! and this is important in some site!
this looks sooo pretty! thanks for sharing
Very Cool Mary Lou, I have the same question as
tunnel maker’s post on June 22nd, 2010 at 23:22
I want to expand the accordion with 8 elements with a width of 960px and a height of 480px.
My problem ist, that the “on hover effect” slides the elements on the bottom and returns
How can I have smooth slide effect without the “bottom effect”?
Once again: BIG THANX for this slider!!!
Thank you
@MARY LOU
GREAT EFFECT!!! THANKY YOU VERY MUCH!!!!
tunnel maker, use
position:relative;
in ul.accordion
No sliding to the bottom or carrying over.
I hope this helps someone.
Great Build?
Turned this into a jQuery plugin. WHY?
You can track updates and bug fixes, plus it comes with several features and improvements:
http://proloser.github.com/ElegantAccordion/
Hi, thanks for a great tut!
Just wondering if it is possible to squeeze the rest of the images together (to like 80px instead of the original 115px) while hoovering one of them. an example of what i mean http://ticketcake.com/
Hi and thanks alot for this!
Is it possible to always have one element open? so that the width is consistant
This is a fantastic tutorial, thank you for putting it up Mary Lou. It’s great to see queries being answered as well.
If I may (as with others above) I would like to know how to make the width static and the hover movements contained within that static width.
Thank you so much.
Muy buena de verdad tiene algo parecido de flash
Thank you for the tutorial, Mary Lou. I’ve used the code and did a major revamp on the whole design itself. I think it turned out well don’t you think?
http://fan.aelysia.net/jquery
@Aelyn It turned out beautiful! We are glad you like the tutorial! Cheers, ML
hi.. it’s a verry cool menu, but i was trying to make the first “slide” to stay open at the beggining & tried out what u recomended b4 but it doesn’t seem to work for me :(
any suggestions??
That look’s great.
But i’m not able to use it perfectly.
Does someone can help me?
Can someone can explain to a real motivate guy how to use it?
I use some CMS, so I can use Html and Java, but I don’t know where to change the CSS…..
Please help me !!!! :)
Great work! However, its floating to the top of my page. How can I get it to stay where I need it to be on the page? Thanks for the help.
Awesome script!
only having some problems with the images themselfs, because i wanted to change the width of de images when they’re expand, because my images are smaller, i cant seem to figuere out how to change that. If somebody could help me, that would be great!
hi
I love the script! Is it possible to add a click function rather than a hover
Thank you
JC