Elegant Accordion with jQuery and CSS3

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 […]

From our monthly sponsor: HelloSign API: Everything IT wants and developers love. Try it free today.

View demoDownload source

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!

View demoDownload source

Message from TestkingWe offer online oracle training program to help you pass pmi certifications on time. Learn about jquery and css3 using security+ tutorial and other resources.
Previous:
Next:

Tagged with:

ML is a freelance web designer and developer with a passion for interaction design. She studied Cognitive Science and Computational Logic and has a weakness for the smell of freshly ground peppercorns.

View all contributions by

Website: http://www.codrops.com

Related Articles

Receive our bi-weekly Collective or official newsletter right in your inbox.

Which newsletter would you like to receive?

CSS Reference

Learn about all important CSS properties from the basics with our extensive and easy-to-read CSS Reference.

It doesn't matter if you are a beginner or intermediate, start learning CSS now.

Feedback 74

Comments are closed.
  1. 2

    great accordion !!!

    little trouble on FF, image appeart and disappeart rapidly on bottom right

    (sorry for my poor english, french inside)

    • 3

      Hi Jireck,
      I noticed that in my version of FF it seems to be a bit slower, but the image does not disappear. I was testing with version 3.6.3, which one do you have? Thanks a lot for the comment! Cheers, ML

  2. 4

    Hi WEB Family,

    This is the one was looking for, because I´ve used Flash for this but now I´ll follow your instruction.

    Thanks for sharing

    • 7

      @dox Which version are you using? Do you run Mac or Win? My version is 4.0.4 on Win and it runs very smoothly. Cheers, ML

  3. 10

    @Shua Ok, I tested on Safari 4.0.5 (531.22.7) and it looks just fine. Why exactly is it “unusable” to you? Cheers, ML

  4. 11

    Will be trying this on other browsers to see the difference. On my year-old laptop running FF 3.6 they were a bit choppy. Still, nice resource!

  5. 13

    Hi great script I will use in next project
    One question – How can i replace “hover” with “onmousedown” event
    It sok when slide open , but closing is problem

    Tnx

  6. 14

    Hey…
    @ML :
    I had same Problems like JIRECK.

    It appears when the bowser window is to short for the Accordion.
    At the time – one AccordionElement fade in and another AccordionElement fade out by selecting one of them, the Elements wrap because the width of the Accordion is greater than the Browser window…

    Sorry for my very BAD english O:-)

  7. 15

    @Marco I understand the problem now and I would say that you should try to change the timings in the function. You can make it slide back in faster. Or you can try to give the ul a height, let’s say of 480px and then say overflow:hidden. This will hide the nice shadow effect and it looks a little bit jerky, but at least the li elements don’t appear below the accordion. I hope it helps, let me know how it goes.

    @Jireck Sorry I did not understand what you meant, I hope this is useful for you, too! 🙂

    Thanks for your comments,
    cheers, ML

  8. 17

    I set it up and am testing the code as is before modifying. Using Firefox, when I mouse over the first (furtherest left) then go to the next, the first one drops below the so both are aligned to the right.

    I would love to use this but only 150px high.

    Great with the CSS.

  9. 19

    Spoke too soon, not working well. with the hidden it just isn’t the same.

  10. 20

    When i test locally in Firefox and IE the boxes appear tiled vertically until rolled over. After the initial rollover, they work properly. Can you advise?

  11. 21

    @ML :
    I test with my FF 3.6.3 at my job and it’s OK, no more trouble.

    I’ll check at home and tell you.

  12. 22

    This looks very cool, have used another accordion script, but would like to try this one out next time.
    Thanks

  13. 23

    Excellent JQuery 🙂 BUT can it start with the first panel open? If so how?

Comments are closed.