multi-level-accordion-menu-featured
October 7, 2015 | 4 Feedbacks

Multi-Level Accordion Menu

A simple CSS accordion menu with support for sub level items.
Browser support
  • ie
  • Chrome
  • Firefox
  • Safari
  • Opera
9+

Today’s resource is a handy accordion menu with support for groups/subitems. It works with CSS only, using the :checked pseudo-class selector on the checkboxes input elements. However we included a version with jQuery as well, in case you prefer a subtle animation compared to the instant default effect. Your call!

The first user case I can think of for this resources is a “layer organizer”. Think of Sublime Text sidebar, or Photoshop layer window. Anyway, I’m sure you’ll find a use for this new snippet to store in your arsenal ;)

Icons: Nucleoapp.com

Creating the structure

The HTML structure is pretty simple: the accordion is an unordered list. If a list item contains subitems, then we insert an input[type=checkbox] and its label. Also, we add the .has-children class to the list item. All “standard” list items contain just an anchor tag.

Adding style

We use a smart (and quite standard nowadays) technique to detect the click and show sub content with CSS only: by including a checkbox input element, we can use the :checked pseudo-class and the adjacent sibling selector (div + div) to change the display mode of the sub <ul> element from “none” to “block”.

Step by step: first of all, we have to make sure that the checkbox input element covers the entire list item that contains subitems. Put in other words: we need to create a custom checkbox. So, firstly, you need to make sure that when you click on the label, the checkbox is checked/unchecked as well. This is achieved by using the “for” attribute inside the label (label “for” attribute = input “name” and “id” attributes. See html section above). This way you can simply hide the input element and work with the label instead.

Now notice in the HTML structure that input, label and the unordered list (that we make visible on click) are siblings. By using the :checked pseudo-class, you can set the following process in motion: when the checkbox input is checked (click on label), then take the <ul> sibling element and change its display value from “none” to “block”:

If you want to gently animate the opening phase, then include the .js file as well. Also remember to add the .animate class to the main .cd-accordion-menu element (this will animate the arrow rotation).

Changelog

Oct 7, 2015
  • Resource released by CodyHouse

Sebastiano Guerriero

UI/UX designer, with a huge passion for Nutella. Co-Founder of CodyHouse. You can follow him on Twitter or Dribbble.

  • Ramesh sau

    it is a nice , but it’s working only like vector design .not like flate design

    • brendan

      lol wut

  • http://www.lukeed.com/ Luke Edwards

    You can still have the CSS version animate as the jQuery version does. Like the color palette

    • http://codyhouse.co/ Sebastiano Guerriero

      hey Luke! True, but you should set a fixed height for the hidden elements, which is ok in some cases, not so convenient in others