You are Viewing : HomeBlogsWordPress dropdown menu using pure CSS dropdown framework

WordPress dropdown menu using pure CSS dropdown framework

CSS dropdown menu framework is a free and pure CSS dropdown menu solution. It clearly has some advantages over javascript based solutions. Well, It is cross browser compatible, completely customizable, fast and does not rely on javascript (except for IE 6 and earlier). Let us see how to employ it in a wordpress theme.

WordPress and CSS drop-down Framework

Always perform your wordpress experiments on a Local server(Mac, Windows). Optionally, you can download my sample theme made just for this purpose, Axtra. Download Here.

  1. First things first. Choose any wordpress theme with native menu support. Or, you can use Axtra with this guide. Insert the following code into the theme’s functions.php to quick enable menu support.
    /* Create a function for register_nav_menus() */
    function add_wp3menu_support() {
    	register_nav_menus(
    		array(
    		'main-menu' => __('Main Navigation'),
    		'another-menu' => __('Another Navigation')
    			)
    	 );
    }
    // Initialise the menus with the theme.
    add_action('init', add_wp3menu_support);
  2. Create and select your custom menu through the wordpress admin. (wp-admin –> Appearance –> Themes –> Menus) .
  3. Download CSS drop-down menu framework from here. Extract the folders css and js into the wordpress theme folder.  Framework categorizes the layout and design into individual style sheets.
  4. Next, we need to load these stylesheets. We open the header.php and include these two lines before <?php wp_head(); ?> hook. Don’t close the file yet.
    <!-- The Css dropdown framework's layout -->
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/dropdown/dropdown.css" type="text/css" media="screen" />
    <!-- And the menu's design aka theme-->
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/dropdown/themes/default/default.css" type="text/css" media="screen" />
  5. Framework uses the class dropdown to identify the menu. We will use  wp_nav_menu()’s arguments to do this for us.
    <div id="navigation">
    	<?php
    	  wp_nav_menu( array(
    		'theme_location' => 'main-menu', // Setting up the location for the main-menu, Main Navigation.
    		'menu_class' => 'dropdown', //Adding the class for dropdowns
    		'container_id' => 'navwrap', //Add CSS ID to the containter that wraps the menu.
    		'fallback_cb' => 'wp_page_menu'
    			)
    		);
    	?>
    </div>
  6. On reloading the theme, dropdown menu appears with the default style that we included.
    CSS dropdown framework with default style

    CSS dropdown menu with default style.

  7. Now we have two options. One, is to modify the default.css file located in css/dropdown/themes/default/default.css . And the second – copy the default file and image sub dir to same directory as dropdown.css (css/dropdown/) and modify it. I chose to go with the second. Whatever you choose, make sure to use the correct paths. So my  header.php reads like -
    <!-- The Css dropdown framework's layout -->
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/dropdown/dropdown.css" type="text/css" media="screen" />
    <!-- And the menu's design aka theme -->
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/dropdown/axtra.css" type="text/css" media="screen" />
  8. Now all that’s remaining is styling the menu. Here are the contents of Axtra’s css/dropdown/axtra.css.
/* Container wrapping the menu  */
#navwrap {
    float: left;
    width: 100%;
    font: 1.0em "Segoe UI", "Lucida Grande", Verdana, Arial, sans-serif;
	/* Now the theme */
	background: #600;
	background: -moz-linear-gradient(top, #600, #200);
	background:	-webkit-gradient(linear, left bottom, left top, from(#200), to(#600));
}
ul.dropdown {
}
ul.dropdown li {
	padding: 10px 12px;
}
ul.dropdown ul li {
	border-style: solid;
	border-width: 1px;
	border-color: #777 #222 #000 #222;
	background-color: #444;
	color: #FFF;
}
ul.dropdown li.hover,
ul.dropdown li:hover {
	background-color: #444;
}
ul.dropdown ul li:hover,
ul.dropdown ul li.hover {
	background-color: #600;
	background-image: -moz-linear-gradient(top, #200, #600);
	background-image: -webkit-gradient(linear, left bottom, left top, from(#600), to(#200));
}
ul.dropdown a:link,
ul.dropdown a:visited	{ color: #ddd; text-decoration: none; }
ul.dropdown a:hover		{ color: #F9F9F9; }
ul.dropdown a:active	{ color: #FFF; }

/* -- level mark -- */

ul.dropdown ul {
	width: 150px;
	margin-top: -1px;
	-moz-box-shadow: 0 2px 5px #222;
	-webkit-box-shadow: 0 2px 5px #222;
}

But, What about menu arrows?

Framework needs a CSS support class to display arrows. For now, wp_nav_menu have no direct options to do this except defining a custom Walker class.

CSS framework applies the arrow images to the list items<li> with additional class name dir. Let us look at the below code borrowed from Goldenapplesdesign.

// Check for submenus. Code by Nathaniel Taintor of goldenapplesdesign.com
function check_for_submenu($classes, $item) {
	global $wpdb;
	$has_children = $wpdb->get_var("SELECT COUNT(meta_id) FROM wp_postmeta WHERE meta_key='_menu_item_menu_item_parent' AND meta_value='".$item->ID."'");
	if ($has_children > 0) array_push($classes,'dir'); // assign the class dir to list items with sub menu.
	return $classes;
}

add_filter( 'nav_menu_css_class', 'check_for_submenu', 10, 2);

Explanation: Above code queries for the list items with submenus. If sub menus are present, function adds the CSS class dir to parent list item<li>. This is accomplished through the  nav_menu_css_class hook. Arrows are transparent png files present in the sub directory “images”, residing inside the dropdown folder (css/dropdown/images/)

Now the respective code.

ul.dropdown li.dir > a {
	padding-right: 20px;
	background-image: url(images/nav-arrow-down.png);
	background-position: 100% 50%;
	background-repeat: no-repeat;
	display:block;
	z-index:599;
}
ul.dropdown ul li.dir > a {
	padding-right: 20px;
	background-image: url(images/nav-arrow-right.png);
	background-position: 100% 50%;
	background-repeat: no-repeat;
	display:block;
	width:115px;
	z-index:599;
}

And the shadows can be added with some CSS3.

Here is the preview of the finished dropdown menu .

finished_dropdown

CSS dropdown framework menu with Finished style

Little housekeeping and some notes

  • Once you have got the perfect design, you can delete all the unnecessary files in the dropdown folder. Just make sure not to delete the required files – layout  dropdown.css , design (here, axtra.css) and the sub folder images.
  • You can easily transform the menu with additional css classes. For example – dropdown-vertical and dropdown-upward. You need to include the required stylesheets though (for Vertical dropdown menu, include dropdown.vertical.css).
  • If you don’t want to clutter up your header.php, You can always add it , the proper way.
    /* Add styles, the proper way */
    // This code goes in functions.php
    function add_styles() {
    	$themedir = get_bloginfo('template_url');
    	// register the stylesheets
    	wp_register_style( 'dropdown-layout', $themedir.'/css/dropdown/dropdown.css', false, false);
    	wp_register_style( 'dropdown-axtra', $themedir.'/css/dropdown/axtra.css', false, false);
    	// and load them.
    	wp_enqueue_style('dropdown-layout');
    	wp_enqueue_style('dropdown-axtra');
    }
    
    // You can also use wp_head hook.
    add_action( 'wp_print_styles', 'add_styles');
  • CSS dropdown menu framework can peacefully co-exist with other javascript solutions, provided we use different css class names. we can even make the menu solutions switchable through theme’s admin page.

This article will be updated once I get the custom walker class working as expected.

Thanks for reading,

Kavin

  • http://www.dhtml-menu-builder.com/ lucky_girl

    Great post. You seem to have a good understanding that how to create a professional drop down menu. When I entering your blog, I felt this. Come on and keep writing your blog will be more attractive. To Your Success!

  • http://twitter.com/cramdesign matt cram

    The filter check_for_submenu is not working on a multisite install. Likely something to do with the global $wpdb but I have no idea what it would be… help?

    • Anonymous

      Hi Matt, re check your code once more. Here it is working WPMU 3.2.1. Oh and if you have used a different database prefix while installing, then you’ve got to change the wp_ to your_prefix_ . :)

  • http://www.facebook.com/forfeit4u Krishna Consciousness

    Thanks good one!

  • Caroltompkins

    Hello,
    I’m wondering if it’s possible to have each “tab” a different color (along with their drop downs). If anyone has done this, I’d love to hear what they did to adjust the code.
    thanks,
    Carol

  • Doggy

    this does not work. on my twentyeleven child theme, ive followed the exact instructions, and the  5th step, the wpnav argument is not adding the class.

    it doesn’t do anything.

    what do you do then? if its not working?
    you must have missed out a step or something

    • Kavin Gray

      It does work. Have some patience – If it shows as HTML then clearly you are typing something off.

  • Doggy

    the code in 5th step is actually doing this.

    inputting this:

        ‘home’, // Setting up the location for the main-menu, Main Navigation.
            ‘menu_class’ => ‘dropdown’, //Adding the class for dropdowns
            ‘container_id’ => ‘navwrap’, //Add CSS ID to the container that wraps the menu.
            ‘fallback_cb’ => ‘wp_page_menu’
                )
            );
        ?>

            

    gives this in the HTML which is not working.

    <li class=bla bla ablalaballa……….

  • Kavin Gray

     The code is incomplete, there should be php start tag and couple lines after the .

  • Doggy

     i figured it out myself.

    the code in 5th step is not working on twentyeleven child theme.

    but if it put it in this way it works.

    false, ‘container_id’=>
    ‘navwrap’, ‘menu_class’=> ‘dropdown’, ‘menu_id’=> ‘home’ ) );
    ?>

    i don’t understand why you have used container id as “navwrap”? when this is not even required as you already have a container called “navigator”?

    i think you should fix the tutorial for these steps.

    • Doggy

       nevermind, this stupid comment system you have is not pasting my php code correctly. see the image for correct code for step 5. this works on my twentyeleven child theme.

    • Kavin Gray

      div#navwrap was present on the original tut for layout purposes.

  • Andrei_ule

    this works, I implemented it, the only problem I had is that I renamed my database tables, but once I modified that it worked. Now I only have to style it according to my website

  • justuser

    Hey Kevin ,

    Useful tutorial  !

    Can you you tell me if the menu created with it is compatible with responsive layout and small screens(Mobile and others)? 

    Thanks !

  • Christos_s

    Hi, Works nice on my first template! Thanks for the tutorial :-) . is there a way i can add more levels?

    Thanks