Responsive navigation with Superfish

After googling and reading many of the responsive navigation designs, I couldn’t find one that I really liked.

I like Superfish because it works like a charm in almost every browser I’ve used without a fuss.

The responsive navigation from Webdesignerwall is nice but don’t work quite well with IE7 I guess.

Therefore, I came up with a mix of both and created mine.

In the main html/header file:

<!doctype html>
<html lang="en">
	<head>
		<title>A very basic responsive Superfish menu example</title>
		<meta http-equiv="content-type" content="text/html;charset=utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
		<!--link rel="stylesheet" type="text/css" href="css/superfish.css" media="screen"-->
		<link rel="stylesheet" type="text/css" href="css/style.css" media="screen">
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
		<script type="text/javascript" src="js/hoverIntent.js"></script>
		<script type="text/javascript" src="js/superfish.js"></script>
		<script type="text/javascript">

		// initialise plugins
		jQuery(document).ready(function($){
			jQuery('ul.sf-menu').superfish();
				
			/* prepend menu icon */
			jQuery('#nav-wrap').prepend('<div id="menu-icon">Menu</div>');
			//alert ('test');
			/* toggle nav */
			$("#menu-icon").on("click", function(){
				jQuery(".sf-menu").slideToggle();
				jQuery(this).toggleClass("active");
			});
		});

		</script>
	</head>
	<body>
	  <nav id="nav-wrap">

		<ul class="sf-menu">
			<li class="current">
				<a href="#a">menu item A</a>
				<ul>
					<li>
						<a href="#aa">menu item that is quite long</a>
					</li>
					<li class="current">
						<a href="#ab">menu item AA</a>
						<ul>
							<li class="current"><a href="#">menu item AA.1</a></li>
							<li><a href="#aba">menu item AA.2</a></li>
							<li><a href="#abb">menu item AA.3</a></li>
							<li><a href="#abc">menu item AA.4</a></li>
							<li><a href="#abd">menu item AA.5</a></li>
						</ul>
					</li>
					<li>
						<a href="#">menu item AB</a>
						<ul>
							<li><a href="#">menu item AB.1</a></li>
							<li><a href="#">menu item AB.2</a></li>
							<li><a href="#">menu item AB.3</a></li>
							<li><a href="#">menu item AB.4</a></li>
							<li><a href="#">menu item AB.5</a></li>
						</ul>
					</li>
					<li>
						<a href="#">menu item AC</a>
						<ul>
							<li><a href="#">menu item AC.1</a></li>
							<li><a href="#">menu item AC.2</a></li>
							<li><a href="#">menu item AC.3</a></li>
							<li><a href="#">menu item AC.4</a></li>
							<li><a href="#">menu item AC.5</a></li>
						</ul>
					</li>
				</ul>
			</li>
			<li>
				<a href="#">menu item B</a>
			</li>
			<li>
				<a href="#">menu item C</a>
				<ul>
					<li>
						<a href="#">menu item CA</a>
						<ul>
							<li><a href="#">short CA.1</a></li>
							<li><a href="#">short CA.2</a></li>
							<li><a href="#">short CA.3</a></li>
						</ul>
					</li>
					<li>
						<a href="#">menu item CB</a>
						<ul>
							<li><a href="#">menu item CB.1</a></li>
							<li><a href="#">menu item CB.2</a></li>
							<li><a href="#">menu item CB.3</a></li>
						</ul>
					</li>
				</ul>
			</li>
			<li>
				<a href="#">menu item D</a>
			</li>	
		</ul>
	  </nav>	
	</body>
</html>

In the style.css

/************************************************************************************
NAV
*************************************************************************************/
#nav-wrap {
	margin-top: 20px;
}

/* menu icon */
#menu-icon {
	display: none; /* hide menu icon initially */
}

.sf-menu, 
.sf-menu li {
	margin: 0;
	padding: 0;
}
.sf-menu li {
	list-style: none;
	float: left;
	margin-right: 5px;
}

/* nav link */
.sf-menu a {
	padding: 4px 15px;
	display: block;
	color: #000;
	background: #ecebeb;
}
.sf-menu a:hover {
	background: #f8f8f8;
}

/* nav dropdown */
.sf-menu ul {
	background: #fff;
	padding: 2px;
	position: absolute;
	border: solid 1px #ccc;
	display: none; /* hide dropdown */
	width: 200px;
}
.sf-menu ul li {
	float: none;
	margin: 0;
	padding: 0;
}
.sf-menu li:hover > ul {
	display: block; /* show dropdown on hover */
}
ul.sf-menu li li:hover ul,
ul.sf-menu li li.sfHover ul {
	left:			200px; /* match ul width */
	top:			0;
}






/*** ESSENTIAL STYLES ***/
.sf-menu, .sf-menu * {
	margin:			0;
	padding:		0;
	list-style:		none;
}



/************************************************************************************
MOBILE
*************************************************************************************/
@media screen and (max-width: 600px) {

	/* nav-wrap */
	#nav-wrap {
		position: relative;
	}

	/* menu icon */
	#menu-icon {
		color: #000;
		width: 42px;
		height: 30px;
		background: #ecebeb url(images/menu-icon.png) no-repeat 10px center;
		padding: 8px 10px 0 42px;
		cursor: pointer;
		border: solid 1px #666;
		display: block; /* show menu icon */
	}
	#menu-icon:hover {
		background-color: #f8f8f8;
	}
	#menu-icon.active {
		background-color: #bbb;
	}
	
	/* main nav */
	.sf-menu {
		clear: both;
		position: absolute;
		top: 38px;
		width: 160px;
		z-index: 10000;
		padding: 5px;
		background: #f8f8f8;
		border: solid 1px #999;
		display: none; /* visibility will be toggled with jquery */
	}
	.sf-menu li {
		clear: both;
		float: none;
		margin: 5px 0 5px 10px;
	}
	.sf-menu a, 
	.sf-menu ul a {
		font: inherit;
		background: none;
		display: inline;
		padding: 0;
		color: #666;
		border: none;
	}
	.sf-menu a:hover, 
	.sf-menu ul a:hover {
		background: none;
		color: #000;
	}
	
	/* dropdown */
	.sf-menu ul {
		width: auto;
		position: static;
		display: block;
		border: none;
		background: inherit;
	}
	.sf-menu ul li {
		margin: 3px 0 3px 15px;
	}
	


}

@media screen and (min-width: 600px) {

	/* ensure #nav is visible on desktop version */
	.sf-menu {
		display: block !important;
	}

}

BTW, download the superfish code and place it in the necessary directory for this menu to work properly.

Leave a Reply