Skip to content

Instantly share code, notes, and snippets.

@johnmorris
Created August 8, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnmorris/ae44621e2520d2676258 to your computer and use it in GitHub Desktop.
Save johnmorris/ae44621e2520d2676258 to your computer and use it in GitHub Desktop.
Build a responsive menu with HTML, CSS and jQuery (full tutorial here: http://youtu.be/h30qPvATvEA)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Responsive Navigation</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<style>
body {
margin: 0;
padding: 0;
font-family: sans-serif;
}
nav {
background: #000;
overflow: hidden;
}
.header {
float: left;
color: #fff;
padding: 15px;
overflow: hidden;
}
.logo {
font-size: 24px;
}
.handle {
float: right;
background: #222;
color: #fff;
border: none;
font-size: 18px;
padding: 5px 10px;
border-radius: 3px;
cursor: pointer;
display: none;
}
nav a {
text-decoration: none;
color: inherit;
}
nav ul {
overflow: hidden;
color: #fff;
margin: 0;
padding: 0;
text-align: center;
transition: max-height 0.5s;
-webkit-transition: max-height 0.5s;
-ms-transition: max-height 0.5s;
-moz-transition: max-height 0.5s;
-o-transition: max-height 0.5s;
}
nav ul li {
float: right;
display: inline-block;
padding: 20px;
}
nav ul li:hover {
background-color: #222;
}
section {
box-sizing: border-box;
line-height: 1.5em;
width: 70%;
margin: 0 auto;
padding: 20px;
}
@media screen and (max-width: 767px) {
.header {
float: none;
}
nav ul {
background: #222;
max-height: 0;
width: 100%;
}
nav ul.show {
max-height: 20em;
}
nav ul li {
box-sizing: border-box;
width: 100%;
padding: 15px;
}
.handle {
display: inline;
}
section {
width: 100%;
}
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$('.handle').on('click', function(){
$('nav ul').toggleClass('show');
});
});
</script>
</head>
<body>
<nav>
<div class="header">
<a class="logo">Demo</a>
<button class="handle">
<i class="fa fa-bars"></i>
</button>
</div>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<section id="content">
<h1>Responsive navigation is da bomb!</h1>
<p>Mobile users around the world thank you!</p>
</section>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment