Navigation Bar

How to create a responsive navigation bar – 02

Create a responsive navigation bar

Before We Start

A navigation bar is one of the main components of a website,It contains links to the other sections and a user interface element within a website. The most important one and the first section that the user sees when he/she enters a website is the navigation bar and it links to the other main parts.

In a website the navigation bar will be an important part and it helps the users to find the pages quickly If you have ever seen a website which doesn’t has a navigation bar, you may have found it’s not easy to locate the exact page what you need. So if you are a person who wishes to develop a website, you have to know how to create a navigation bar properly.

How to create a responsive navigation bar ? We are here to solve your problem.
In this article we discuss how to create a responsive navigation bar. Before we start please read the below articles which easier you to understand the process, if you are new to development.

Step 1 – Add HTML

It’s too easy and simple. Just copy and paste below code in your HTML editor between <body> </body> tag.

<html>
<body>
    <div style="height:10vh"></div>
    <h1 style="text-align:center">Right Aligned Menu</h1>
    <div id="menu">
      <div class="menu-right">
        <ul>
          <li>Home</li>
          <li>About</li>
          <li>Contact-Us</li>
        </ul>
        <div class="menu-marker">
          <input type="checkbox">
          <ul>
            <li class="first-bar"></li>
            <li class="second-bar"></li>
            <li class="third-bar"></li>
          </ul>
        </div>
      </div>
    </div>
    <h1 style="text-align:center">Left Aligned Menu</h1>
    <div id="menu">
      <div class="menu-left">
        <div class="menu-marker">
          <input type="checkbox">
          <ul>
            <li class="first-bar"></li>
            <li class="second-bar"></li>
            <li class="third-bar"></li>
          </ul>
        </div>
        <ul>
          <li>Home</li>
          <li>About</li>
          <li>Contact-Us</li>
        </ul>
      </div>
    </div>
    <div style="height:10vh"></div>
</body>
</html>

Step 2 – Add CSS

We use some external CSS link to this code. No need to worry about this, copy and paste below code between <head></head> tag.

<head>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link href="https://fonts.googleapis.com/css?family=Source+Code+Pro&;display=swap">
</head>

Copy and paste below code in your HTML editor between <style></style> tag.

<style>
html{
  --size:40px;
  --t-1:9.5px;
  --t-3:-9.5px;
  --c-box: 4;
  font-family: "Source Code Pro", sans-serif;
  background: rgb(0, 0, 0);
  color: rgb(255, 0, 170);
}
a{color:inherit;}
#menu{
  width:80%;
  margin: auto;
  padding: 10px;
  border: 1px solid rgb(255, 0, 170);
}
div.menu-right,div.menu-left{
  width:100%;
  height:40px;
  padding: 0;
  margin:0;
}
div.menu-left{text-align:left}
div.menu-right{text-align:right}
div.menu-right > ul,div.menu-left > ul{
  display:inline-block;
  list-style-type:none;
  padding: 0;
  margin:0;
  min-width:40%;
  text-align: center;
  font-weight: 700;
  overflow: hidden;
  color: rgb(255, 0, 200);
  z-index: 100;
}
div.menu-right > ul > li,div.menu-left > ul > li{
  padding: 10px 16px;
  display:inline-block;
  margin:0;
  overflow:hidden;
  vertical-align:middle;
}
.menu-marker{
  display:inline-block;
  position: relative;
  width:var(--size);
  height:var(--size);
  padding:0 !important;
  transform: translate(0,-20%);
}
.menu-marker > input[type=checkbox]{
  position:absolute;
  top:15px;
  left:15px;
  transform: scale(var(--c-box));
  opacity:0;
  z-index:2;
  cursor: pointer;
}
.menu-marker ul{
  list-style-type: none;
  padding: 0;
  text-align: left;
  z-index:1;
  vertical-align:middle;
}
.menu-marker li{
  background: white;
  border:white solid 1px;
  margin:calc(var(--size)/5) 0;
  transition: ease-in-out 0.5s all;
}
li.first-bar{
  width:80%;
  height:var(--size)/20;
}
li.second-bar{
  width:100%;
  height:var(--size)/20;
}
li.third-bar{
  width:60%;
  height:var(--size)/20;
}
.menu-marker > input[type=checkbox]:checked ~ ul li.first-bar, .menu-marker > input[type=checkbox]:checked ~ ul li.third-bar{
  width:100%;
}
.menu-marker > input[type=checkbox]:checked ~ ul li.second-bar{
  opacity: 0;
}
.menu-marker > input[type=checkbox]:checked ~ ul  li.first-bar{
  transform: translateY(var(--t-1)) rotate(225deg);
}
.menu-marker > input[type=checkbox]:checked ~ ul  li.third-bar{
  transform: translateY(var(--t-3)) rotate(315deg);
}
.menu-right > ul{transform: translateX(5%);}
.menu-left > ul{transform: translateX(-5%);}
.menu-right > ul,.menu-left > ul{
  opacity: 0;
  transition: ease-in-out 0.5s all;
}
.menu-active{
  transform: translateX(0%) !important;
  opacity: 1 !important;
}
    </style>

Step 3 – Add JavaScript

We use some external JavaScript link to this code. No need to worry about this, copy and paste below code between <head></head> tag.

<head>
<link href="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</head>

And also copy and paste below JavaScript code between <script></script> tag.

<script>
$('.menu-right .menu-marker > input[type=checkbox]').on("click",function(){
  if($(this).is(":checked")){
    $('.menu-right > ul').addClass(" menu-active");
  }
  else{
    $('.menu-right > ul').removeClass(" menu-active");
  }
});
$('.menu-left .menu-marker > input[type=checkbox]').on("click",function(){
  if($(this).is(":checked")){
    $('.menu-left > ul').addClass(" menu-active");
  }
  else{
    $('.menu-left > ul').removeClass(" menu-active");
  }
});
</script>

At the end we will have something like this. You can change fonts, colors, backgrounds and all things that you want.Enjoy it.

Create a responsive navigation bar

Video Tutorial

Please watch this video to better understand this tutorial and don’t forget to subscribe to our channel.


Help others to find out about this article on Social Media sites. If you have any doubt or any problem, don’t hesitate to contact us. Thereafter we will be able to help you and also make sure you bookmark our site on your browser.

Related Articles

Leave a Reply

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

Check Also
Close
Back to top button