How-To Create A Accordion Menu using CSS3

<span class="fa fa-user"></span>
Pascal
No Comment
Design
249
0
How-To Create A Accordion Menu using CSS3

In this tutorial we’ll learn about to create a Accordion Menu using CSS3. There are manyCSS3 accordion tutorials around on the web, in this version we’ll be using the :target pseudo-class and will work on browsers that support the CSS3 properties.

[button link="http://media.yokamo.com/design/howto-create-according-menu-css3/preview/" size="large" variation="olive" align="center" target="blank"]Preview[/button]

HTML

1. We’ll start off with the basic structure of HTML containing the head section which includes the page title, stylesheet link & body tags.

Wrapper & Menu

1
	<link href="ac-styles.css" rel="stylesheet" media="screen" type="text/css" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

2. Next inside the body tags create a main wrapper div tag to contain & center all of the content. In the wrapper tags, create an unordered list with a class name and create each list item with its own id. By giving each list item it’s own id, we’ll be using the :target selector which will allow us to style and open the accordion.

You need to make each list id different (eg. one, two) for the target selector to work, this will make the sub menus visible.

1
2
<!--end accordian-->
<!--end wrapper-->

Sub Menus

3. For the sub menus, add a second unordered list with a own class name inside each list item.

CSS Styles

4. Now for the css, first I’ve written out some basics for the body & wrapper. I’ve made the wrapper centered and made the width 250px, because the wrapper will be containing the menu the width will determine the size of your menu.

1
2
3
4
5
6
7
8
body {
background-image:url(bg.png);
}
 
#wrapper {
width:250px;
margin: 0 auto;
}

5. As a first step to the accordion menu, you add reset style to the menu. This will clear the margins, paddings, list styles, borders & outline.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.ac-menu,
.ac-menu ul,
.ac-menu li,
.ac-menu a,
 
.ac-menu span {
 margin: 0;
 padding: 0;
 border: 0;
 outline: none;
}
 
.ac-menu li {
list-style: none;
}

6. Now it’s time to style the menu links, this includes the CSS3 Gradient style, font styles, shadows along with some style basics.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* Accordian Style */
.ac-menu li &gt; a {
 
 display: block;
 
 position: relative;
 
 min-width: 160px;
 
 padding: 0 10px 0 0;
 
 text-align: center;
 
 height: 32px;
 
 color: #FFFFFF;
 
 font: bold 12px/32px Arial, sans-serif;
 
 text-decoration: none;
 
 background: #343435;
 
 background: -moz-linear-gradient(top, #343435 1%, #4a4a4a 100%);
 
 background: -webkit-gradient(linear, left top, left bottom, color-stop(1%,#343435), color-stop(100%,#4a4a4a));
 
 background: -webkit-linear-gradient(top, #343435 1%,#4a4a4a 100%);
 
 background: -o-linear-gradient(top, #343435 1%,#4a4a4a 100%);
 
 background: -ms-linear-gradient(top, #343435 1%,#4a4a4a 100%);
 
 background: linear-gradient(top, #343435 1%,#4a4a4a 100%);
 
 -webkit-box-shadow: inset 0px 1px 0px 0px rgba(255,255,255, .1), 0px 1px 0px 0px rgba(0,0,0, .1);
 
 -moz-box-shadow: inset 0px 1px 0px 0px rgba(255,255,255, .1), 0px 1px 0px 0px rgba(0,0,0, .1);
 
 box-shadow: inset 0px 1px 0px 0px rgba(255,255,255, .1), 0px 1px 0px 0px rgba(0,0,0, .1);
 
}
 
.ac-menu &gt; li:hover &gt; a,
 
.ac-menu &gt; li:target &gt; a {
 
 text-shadow: 1px 1px 1px rgba(255,255,255, .2);
 
 background: #20a4ca;
 
 background: -moz-linear-gradient(top, #20a4ca 1%, #47b5d4 100%);
 
 background: -webkit-gradient(linear, left top, left bottom, color-stop(1%,#20a4ca), color-stop(100%,#47b5d4));
 
 background: -webkit-linear-gradient(top, #20a4ca 1%,#47b5d4 100%);
 
 background: -o-linear-gradient(top, #20a4ca 1%,#47b5d4 100%);
 
 background: -ms-linear-gradient(top, #20a4ca 1%,#47b5d4 100%);
 
 background: linear-gradient(top, #20a4ca 1%,#47b5d4 100%);
 
 -webkit-box-shadow: inset 0px 1px 0px 0px rgba(255,255,255, .1), 0px 1px 0px 0px rgba(0,0,0, .1);
 
 -moz-box-shadow: inset 0px 1px 0px 0px rgba(255,255,255, .1), 0px 1px 0px 0px rgba(0,0,0, .1);
 
 box-shadow: inset 0px 1px 0px 0px rgba(255,255,255, .1), 0px 1px 0px 0px rgba(0,0,0, .1);
 
}

7. Next up you have the styles for the sub-menu’s, this include background colours, shadows and other basics to help make the sub-menu look clean.

/* Sub-Menu */
.sub-menu li a {
 color: #797979;
 text-shadow: 1px 1px 0px rgba(255,255,255, .2);
 background: #eae9e9;
 border-bottom: 1px solid #c1bfbf;
 -webkit-box-shadow: inset 0px 1px 0px 0px rgba(255,255,255, .1), 0px 1px 0px 0px rgba(0,0,0, .1);
 -moz-box-shadow: inset 0px 1px 0px 0px rgba(255,255,255, .1), 0px 1px 0px 0px rgba(0,0,0, .1);
 box-shadow: inset 0px 1px 0px 0px rgba(255,255,255, .1), 0px 1px 0px 0px rgba(0,0,0, .1);
}
 
.sub-menu li:hover a { background: #f6f5f5; }

8. To complete the accordion menu, you finish the stylesheet with the code below. To make the sub-menu hide, you make the height 0 and an hidden overflow for the first style. The transition styles tell the accordion sub-menus to drop out when clicked on. Lastly I’ve made the height 98px because I have the same amount of sub links for each menu. If you plan to have different amount of sub-links for each menu, you’ll need to add a different height for each sub-menu depending on how many links you have.

/* Accordian Function */
.ac-menu li &gt; .sub-menu {
 height: 0;
 overflow: hidden;
 -webkit-transition: height .2s ease-in-out;
 -moz-transition: height .2s ease-in-out;
 -o-transition: height .2s ease-in-out;
 -ms-transition: height .2s ease-in-out;
 transition: height .2s ease-in-out;
 
}
.ac-menu li:target &gt; .sub-menu {
 height: 98px;
}

[button link="http://media.yokamo.com/design/howto-create-according-menu-css3/preview/" size="large" variation="olive" align="center" target="blank"]Preview[/button] [button link="http://media.yokamo.com/design/howto-create-according-menu-css3/Accordion-Menu-CSS3-Files.zip" size="large" variation="pearl" align="center" target="blank"]Download Source[/button]

Facebook Comments

POST A COMMENT.