Categories
Web development

How to create a responsive Navigation Bar?

We know now how to create a navigation bar and dropdown menu. Now, we can learn how to create a responsive navigation bar with CSS and JavaScript. What does it mean responsive?

It means, mobile-friendly. So when your site is viewed on smaller screens – make sure it’s easy to navigate.

How to create a responsive Navigation Bar?

Step 1: Add HTML:

<!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">
</head>
<body>
<div class="topnav" id="myTopnav">
  <a href="#home" class="active">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <div class="dropdown">
    <button class="dropbtn">Dropdown
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>
  <a href="#about">About</a>
  <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">☰</a>
</div>
</body>
</html>

Step 2: Add CSS:

body {margin:0;font-family:Arial}

.topnav {
  overflow: hidden;
  background-color: #264653;
}

.topnav a {
  float: left;
  display: block;
  color: #fff;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.active {
  background-color: #2a9d8f;
  color: white;
}

.topnav .icon {
  display: none;
}

.dropdown {
  float: left;
  overflow: hidden;
}

.dropdown .dropbtn {
  font-size: 17px;   
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.topnav a:hover, .dropdown:hover .dropbtn {
  background-color: #e9c46a;
  color: white;
}

.dropdown-content a:hover {
  background-color: #f4a261;
  color: black;
}

.dropdown:hover .dropdown-content {
  display: block;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child), .dropdown .dropbtn {
    display: none;
  }
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
  .topnav.responsive .dropdown {float: none;}
  .topnav.responsive .dropdown-content {position: relative;}
  .topnav.responsive .dropdown .dropbtn {
    display: block;
    width: 100%;
    text-align: left;
  }
}

Step 3: Add JavaScript:

<script>
function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
</script>

Output:

Resize the browser window to see how the responsive navigation works.

Enjoy coding!

Read also:

How to create a responsive zig-zag layout?

How to create an expanding grid with CSS & JavaScript?

CSS Background Stripes

Categories
Web development

How to create a Dropdown Menu?

In my previous post, you learned how to create a navigation bar with CSS. Today I would like to show you how you can create a dropdown menu using CSS.

CSS Dropdown Menu

The CSS dropdown menu is a toggleable menu that allows the user to choose one of the values from a preferred list.

Example1. Hoverable Dropdown Menu:

Step 1: Add HTML

<div class="drop-down">
  <button class="drop-button">Dropdown Menu</button>
  <div class="drop-down-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>

Step 2: Add CSS

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.drop-button {
  background-color: #2a9d8f;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
}

.drop-down {
  position: relative;
  display: inline-block;
}

.drop-down-content {
  display: none;
  position: absolute;
  background-color: #f4a261;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.drop-down-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.drop-down-content a:hover {background-color: #e76f51;}

.drop-down:hover .drop-down-content {display: block;}

.drop-down:hover .drop-button {background-color: #e9c46a;}
</style>
</head>
<body>

<h2>Hoverable Dropdown Menu:</h2>
<p>Hover over the button to open the dropdown menu.</p>

<div class="drop-down">
  <button class="drop-button">Dropdown Menu</button>
  <div class="drop-down-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

</body>
</html>

Output:

Hoverable Dropdown Menu:

Hover over the button to open the dropdown menu.





You can create a dropdown menu that appears when the user moves the mouse over an element inside a navigation bar.  

Example2. Dropdown Menu inside a Navigation Bar:

Step 1: Add HTML

<div class="nav-bar">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <div class="dropDown">
    <button class="dropButton">Dropdown
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropDown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>
</div>

Step 2: Add CSS

<!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>

.nav-bar {
  overflow: hidden;
  background-color: #2a9d8f;
}

.nav-bar a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.dropDown {
  float: left;
  overflow: hidden;
}

.dropDown .dropButton {
  font-size: 16px; 
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}

.nav-bar a:hover, .dropDown:hover  .dropButton {
  background-color: #e76f51;
}

.dropDown-content {
  display: none;
  position: absolute;
  background-color: #e9c46a;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropDown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropDown-content a:hover {
  background-color: #f4a261;
}

.dropDown:hover .dropDown-content {
  display: block;
}
</style>
</head>
<body>

<div class="nav-bar">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <div class="dropDown">
    <button class="dropButton">Dropdown
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropDown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>
</div>

</body>
</html>

Output:

Hover over the “Dropdown” link to see the dropdown menu.





Enjoy coding!

Read also:

CSS Fixed Sidebar Menu

CSS Fallback Fonts

How to create a responsive Navigation Bar?