Animenu – a responsive dropdown navigation made with SASS

I’ve been pretty happy lately to see I’m still receiving a lot of positive feedback on my animated dropdown menu. I found that quite encouraging and I decided to start working on an improved version of it using SASS & Compass. The new version is responsive, has no JavaScript dependency and it’s hosted on GitHub too.

animenu-preview

View demo
or download the .zip from GitHub

Some words on this project

Some of the decisions I had to take on this new improved dropdown navigation were on the browser support and number of levels this menu will support. All these, considering that my previous versions support IE6 and until three levels of dropdown navigation.

But, this time the accent is put on responsiveness.

Browser support

The browser support for this dropdown navigation is IE8+, so it uses the new box model triggered by box-sizing: border-box and the shortest clearfix ever. With less effort, I know this could be updated to work on IE7 too, but at this time I think that if you still need support for IE7 and below, you should try my previous versions.

One dropdown level

Aimenu - one dropdown level

For a better user experience and to avoid usability issues, in my opinion one dropdown level is more than enough for a navigation menu. If you theoretically would need more levels, you may try considering another implementation of that content or/and other navigation techniques like tabs and breadcrumbs.

Also, an important factor on choosing just one dropdown level was the ability of using this type of navigation on touch devices like iPad, while on iPhone for example, the navigation dropdowns rearrange into a stack.

The markup

As you may have guessed, there’s nothing complicated here. The only thing worth mentioning is the advanced checkbox hack, which is used to simulate the toggle effect.

<nav class="animenu">	
    <input type="checkbox" id="button">
    <label for="button" onclick>Menu</label>
	<ul>
		<li>
			<a href="#">Item 1</a>
			<ul>
				<li><a href="">Sub Item 1</a></li>
				<li><a href="">Sub Item 2</a></li>
				<li><a href="">Sub Item 3</a></li>
			</ul>
		</li>
		<li> ... </li>
		<li> ... </li>
		<li> ... </li>
	</ul>
</nav>

SASS and Compass

I must admit at the beginning I was a bit reluctant on using SASS for writing CSS but in the end it turned out to be a real pleasure to do so. Having programming stuff like variables and functions at your reach is pretty satisfying. Also, I like how nesting rules helps you organize the code. Last but not least, I enjoy using single-line comments, just like in JavaScript or PHP. As a side note, I’m using SCSS here, which is the latest syntax of SASS.

With all these being said, here are some quick thoughts on how I SASSified my animated dropdown menu:

Variables

$subMenuItemWidth: 175px;		// Minimum width for submenus' items
$baseMenuBackground: #111;		// Base color theme
$secondaryMenuBackground: #0186ba;	// Secondary color (highlights, triangles...)
$baseMenuColor: #999;			// Proper menu items color
$gutter: 1em;				// Base gutter
$input: "input[type=checkbox]";  	// Checkbox hack selector

For a better look and consistency, I use $subMenuItemWidth as a minimum width for the dropdown lists. The reason why this value is stored into a variable is so that in the future to be easily updated without having to search for it too much.

The above lines contain some pretty self explanatory stuff like basic colors that will be used to style this dropdown navigation, a default gutter that will help building paddings and margins and a selector shortcut for the checkbox input.

On customization

The Animenu SASS code uses functions like darken() and lighten() with the above variables as parameters so it’s quite easy to customize the entire appearance by just changing the variable’s values. Also, most of included CSS3 stuff like text-shadow and box-shadow is based on the above predefined variables.

Animenu customizedAnimenu customization example

Mixins and silent classes

Beside the above variables, I’m using helpers like mixins and silent classes as well.

In the following mixin, when $menu-gradient argument value is equal to false, the background will contain no gradient. This is helpful when defining both main menu and dropdowns backgrounds using the $baseMenuBackground as parameter.

@mixin menu-background($color, $menu-gradient: true) {
	background-color: $color;
	@if $menu-gradient == true {
		@include background-image(linear-gradient(rgba(255, 255, 255, .2),
							  rgba(255, 255, 255, 0)));
	}
	@else {
		background-image: none;
	}	
}

Silent classes like the next one, differentiated by the percent sign, will not be generated in the resulted CSS output. Their purpose is to be used with @extend whenever needed.

%cf:after {
	content:"";
	display:table;
	clear:both;		
}

Animenu breakpoint

@media screen and (max-width: 480px) {...}

Animenu - responsive

On iPhone, with using the checkbox hack, the whole navigation becomes stacked and is triggered once the checkbox’s state is checked.

On large displays, the dropdowns lists reveal on hover by using a smooth transition but we all know the hover event is not available on touch devices. Still, to make it work on tablets, the trick is to use <a href="#"> for the navigation elements that trigger the adjacent dropdowns.

View demo
or download the .zip from GitHub

Final words

In the past, I experimented it with writing SASS just on quick demos. Basically, this is my first SASS project I’m showing to the world so be gentle with me if you’re spotting things that could have been made much better. :)

This is it, also I hope you dig the “Animenu” name I chose for this new responsive navigation and I’m looking forward to read your thoughts. Thanks for reading!

19 thoughts on “Animenu – a responsive dropdown navigation made with SASS

  1. Hello, great tutorial and thanks for sharing. I’ve put this menu on my new project and noticed something peculiar. With my media screen changed to –> media screen and (max-width: 767px) so my Galaxy Tab2 will show the collapsed version of the menu, clicking on the menu does not open up the menu. The collapsed menu works fine on iphone5. Any quick fix for this behavior in default android browser?

    Thanks in advance.

    • Thank you for fixing the issue on such short notice. The collapsed menu is working fine on my galaxy tab 2 now.

  2. First of all, well done on creating this excellent navigation system.

    Is there any way in which all of the top level navigation groups can be closed initially when viewing the menu in ‘mobile mode’ ?

    By default, all options are visible which could result in a very long scrolling list on a mobile device.

    Any advice would be very gratefully received.

    Thanks.

      • Hi Catalin,

        Thanks for your reply.

        I’m seeing this on your demonstration and even your screenshot shows all sub items visible at the same time when in ‘Mobile Mode’.

        When viewing the ‘Desktop’ version, I have to hover my mouse pointer over ‘Item 1′ before being able to see ‘Sub Item 1′, ‘Sub Item 2′ etc which is the expected behaviour.

        However, when viewing the ‘Mobile’ version, the menu is fully expanded, with every Sub Item of every Top Level item all visible when the menu first displays. I would expect to only see ‘Sub Item 1′, ‘Sub Item 2′ etc after clicking ‘Item 1′.

        That’s certainly the case in Chrome and Firefox anyway.

        Maybe some JQuery could enhance this behaviour.

          • @Nick and @Juanma,

            What you’re asking for is requiring some bits of JavaScript and I avoided this for Animenu for some reasons.

            On mobile, I’d prefer a quick way to access the menu items, instead of expanding/collapsing block in order to find what I’m interested in.

            Hope that make sense.

  3. Thanking you very kindly, love the menu, downloaded and will play about with the menu a little.

    A quick question: the responsive view (mobile devices) does the whole menu have to drop down? what I mean if you have lots of pages on your website and the whole lot drops down, then it`s not very user friendly.

    • Chris, I posted above a reply to a similar question.

      On mobile, I think it’s better to be able to access the menu items immediately instead of wasting time on expanding/collapsing sections.

      If you ask me, I think it’s pretty straightforward.

  4. 1. Useful!
    2. Is there anyone who would like enjoy mentoring me in learning javascript/jquery? Skype: superbrainmary. It would be much appreciated!

  5. I am curious how the Animenu can be used to show two separate columns where Item1 and Item2 are the two main items, and below that there are several submenu items. Any help will be highly appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>