How TO – Mobile Navigation Menu


Previous


Next

Mobile Navigation Bar

Vertical (recommended):

Example

<!DOCTYPE html>
<html>
<head>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<link rel=”stylesheet” href=”https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css”>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}

.mobile-container {
max-width: 480px;
margin: auto;
background-color: #555;
height: 500px;
color: white;
border-radius: 10px;
}

.topnav {
overflow: hidden;
background-color: #333;
position: relative;
}

.topnav #myLinks {
display: none;
}

.topnav a {
color: white;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
display: block;
}

.topnav a.icon {
background: black;
display: block;
position: absolute;
right: 0;
top: 0;
}

.topnav a:hover {
background-color: #ddd;
color: black;
}

.active {
background-color: #04AA6D;
color: white;
}
</style>
</head>
<body>

<!– Simulate a smartphone / tablet –>
<div>

<!– Top Navigation Menu –>
<div>
<a href=”#home”>Logo</a>
<div id=”myLinks”>
<a href=”#news”>News</a>
<a href=”#contact”>Contact</a>
<a href=”#about”>About</a>
</div>
<a href=”javascript:void(0);” onclick=”myFunction()”>
<i></i>
</a>
</div>

<div style=”padding-left:16px”>
<h3>Vertical Mobile Navbar</h3>
<p>This example demonstrates how a navigation menu on a mobile/smart phone could look like.</p>
<p>Click on the hamburger menu (three bars) in the top right corner, to toggle the menu.</p>
</div>

<!– End smartphone / tablet look –>
</div>

<script>
function myFunction() {
var x = document.getElementById(“myLinks”);
if (x.style.display === “block”) {
x.style.display = “none”;
} else {
x.style.display = “block”;
}
}
</script>

</body>
</html>

Horizontal:

Example

<!DOCTYPE html>
<html>
<head>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<link rel=”stylesheet” href=”https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css”>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}

.mobile-container {
max-width: 480px;
margin: auto;
background-color: #555;
height: 500px;
color: white;
border-radius: 10px;
}

.topnav {
overflow: hidden;
background-color: #333;
position: relative;
}

.topnav #myLinks {
display: none;
}

.topnav a {
float: left;
color: white;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}

.topnav a.icon {
float: right;
}

.topnav a:hover {
background-color: #ddd;
color: black;
}

.active {
background-color: #04AA6D;
color: white;
}
</style>
</head>
<body>

<!– Simulate a smartphone / tablet look –>
<div>

<!– Top Navigation Menu –>
<div>
<a href=”#home”>Logo</a>
<div id=”myLinks”>
<a href=”#news”>News</a>
<a href=”#contact”>Contact</a>
<a href=”#about”>About</a>
</div>
<a href=”javascript:void(0);” onclick=”myFunction()”>
<i></i>
</a>
</div>

<div style=”padding-left:16px”>
<h3>Horizontal Mobile Navbar</h3>
<p>This example demonstrates how a navigation menu on a mobile/smart phone could look like.</p>
<p>Click on the hamburger menu (three bars) in the top right corner, to toggle the menu.</p>
<p><strong>Note that this example should’nt be used if you have a lot of links, as they will “break” the navbar when there’s too many (especially on very small screens).</strong></p>
</div>

<!– End smartphone / tablet look –>
</div>

<script>
function myFunction() {
var x = document.getElementById(“myLinks”);
if (x.style.display === “block”) {
x.style.display = “none”;
} else {
x.style.display = “block”;
}
}
</script>

</body>
</html>

Create A Mobile Navigation Menu

Step 1) Add HTML:

Example

<!– Load an icon library to show a hamburger menu (bars) on small screens –>
<link rel=”stylesheet” href=”https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css”>

<!– Top Navigation Menu –>
<div class=”topnav”>
  <a href=”#home” class=”active”>Logo</a>
  <!– Navigation links (hidden by default) –>
  <div id=”myLinks”>
    <a href=”#news”>News</a>
    <a href=”#contact”>Contact</a>
    <a href=”#about”>About</a>
  </div>
  <!– “Hamburger menu” / “Bar icon” to toggle the navigation links –>
  <a href=”javascript:void(0);” class=”icon” onclick=”myFunction()”>
    <i class=”fa fa-bars”></i>
  </a>
</div>

Step 2) Add CSS:

Example

/* Style the navigation menu */
.topnav {
  overflow: hidden;
  background-color: #333;
  position: relative;
}

/* Hide the links inside the navigation menu (except for logo/home) */
.topnav #myLinks {
  display: none;
}

/* Style navigation menu links */
.topnav a {
  color: white;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  display: block;
}

/* Style the hamburger menu */
.topnav a.icon {
  background: black;
  display: block;
  position: absolute;
  right: 0;
  top: 0;
}

/* Add a grey background color on mouse-over */
.topnav a:hover {
  background-color: #ddd;
  color: black;
}

/* Style the active link (or home/logo) */
.active {
  background-color: #04AA6D;
  color: white;
}


 

Step 3) Add JavaScript:

Example

<!DOCTYPE html>
<html>
<head>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<link rel=”stylesheet” href=”https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css”>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}

.mobile-container {
max-width: 480px;
margin: auto;
background-color: #555;
height: 500px;
color: white;
border-radius: 10px;
}

.topnav {
overflow: hidden;
background-color: #333;
position: relative;
}

.topnav #myLinks {
display: none;
}

.topnav a {
color: white;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
display: block;
}

.topnav a.icon {
background: black;
display: block;
position: absolute;
right: 0;
top: 0;
}

.topnav a:hover {
background-color: #ddd;
color: black;
}

.active {
background-color: #04AA6D;
color: white;
}
</style>
</head>
<body>

<!– Simulate a smartphone / tablet –>
<div>

<!– Top Navigation Menu –>
<div>
<a href=”#home”>Logo</a>
<div id=”myLinks”>
<a href=”#news”>News</a>
<a href=”#contact”>Contact</a>
<a href=”#about”>About</a>
</div>
<a href=”javascript:void(0);” onclick=”myFunction()”>
<i></i>
</a>
</div>

<div style=”padding-left:16px”>
<h3>Vertical Mobile Navbar</h3>
<p>This example demonstrates how a navigation menu on a mobile/smart phone could look like.</p>
<p>Click on the hamburger menu (three bars) in the top right corner, to toggle the menu.</p>
</div>

<!– End smartphone / tablet look –>
</div>

<script>
function myFunction() {
var x = document.getElementById(“myLinks”);
if (x.style.display === “block”) {
x.style.display = “none”;
} else {
x.style.display = “block”;
}
}
</script>

</body>
</html>


Previous


Next

Scroll to Top