Create Responsive Navbar in JavaScript HTML CSS

Create Responsive Navbar in JavaScript HTML CSS
Example of Responsive Navbar

Introduction:

Embarking on the exciting journey of crafting a Responsive Navbar using HTML, CSS, and JavaScript opens up a world of possibilities for coding enthusiasts. This tutorial is your comprehensive guide to creating a dynamic and adaptable navigation bar, providing insights into the synergy of these essential web development technologies.

Whether you’re a coding novice or a seasoned developer, mastering these fundamental skills is vital in today’s tech-driven world. This step-by-step tutorial ensures clarity in understanding how HTML, CSS, and JavaScript collaborate to produce a responsive and visually appealing navbar.

Let’s delve into the realm of code and design, where your proficiency will shine through the creation of an interactive and responsive navbar.

Join Our Telegram Channel to Download the Project Source Code: Click Here

3 Steps to Set Up: Responsive Navbar

Follow this structured guide to create and set up the Responsive Navbar project using HTML, CSS, and JavaScript —

Create Project Files: Start by establishing a project folder and name it according to your preference. Inside the folder, create three files: index.html for structure, style.css for styling, and script.js for interactivity. This foundational step lays the groundwork for your responsive navbar.

Step 1: HTML Structure

In the index.html file, incorporate the provided HTML code to define the basic structure of your responsive navbar. Use semantic tags for navigation, ensuring clarity and accessibility while optimizing for SEO.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Responsive Navbar</title>
</head>

<body>
<header class="header-area">
  <!-- site-navbar start -->
  <div class="navbar-area">
    <div class="container">
      <nav class="site-navbar">
        <!-- site logo -->
        <a href="#home" class="site-logo">logo</a>

        <!-- site menu/nav -->
        <ul>
          <li><a href="#">home</a></li>
          <li><a href="#">about</a></li>
          <li><a href="#">service</a></li>
          <li><a href="#">contact</a></li>
        </ul>

        <!-- nav-toggler for mobile version only -->
        <button class="nav-toggler">
          <span></span>
        </button>
      </nav>
    </div>
  </div><!-- navbar-area end -->

  <div class="intro-area">
    <div class="container">
      <h2>Responsive Navbar with pure JavaScript</h2>
      <p>Please resize your browser and see the result</p>
    </div>
  </div>
</header>
</body>
</html>
Step 2: Style with CSS

Enhance the visual appeal of your responsive navbar by adding the CSS code into your style.css file. This code offers a sleek and modern aesthetic, seamlessly adjusting for various screen sizes. Customize the styles to align with your project’s theme.

/* default css start */
*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body{
  font-family: 'Open Sans', sans-serif;
  font-size: 14px;
  font-weight: 400;
  line-height: 20px;
}
.container {
  width: 100%;
  max-width: 1440px;
  margin: 0 auto;
  padding: 0 15px;
}
.header-area {
  background: linear-gradient(rgba(0,0,0,.3), rgba(0,0,0,.5)),
  url(https://images.unsplash.com/photo-1528353518104-dbd48bee7bc4?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2089&q=80);
  background-position: center center;
  background-size: cover;
}
/* default css end */


/* navbar regular css start */
.navbar-area {
  background: rgba(0,0,0,.6);
  border-bottom: 1px solid #000;
}
.site-navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
a.site-logo {
  font-size: 26px;
  font-weight: 800;
  text-transform: uppercase;
  color: #fff;
  text-decoration: none;
}
.site-navbar ul {
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
}
.site-navbar ul li a {
  color: #fff;
  padding: 20px;
  display: block;
  text-decoration: none;
  text-transform: uppercase;
}
.site-navbar ul li a:hover {
  background: rgba(255,255,255,.1);
}
/* navbar regular css end */


/* nav-toggler css start */
.nav-toggler {
  border: 3px solid #fff;
  padding: 5px;
  background-color: transparent;
  cursor: pointer;
  height: 39px;
  display: none;
}
.nav-toggler span, 
.nav-toggler span:before, 
.nav-toggler span:after {
  width: 28px;
  height: 3px;
  background-color: #fff;
  display: block;
  transition: .3s;
}
.nav-toggler span:before {
  content: '';
  transform: translateY(-9px);
}
.nav-toggler span:after {
  content: '';
  transform: translateY(6px);
}
.nav-toggler.toggler-open span {
  background-color: transparent;
}
.nav-toggler.toggler-open span:before {
  transform: translateY(0px) rotate(45deg);
}
.nav-toggler.toggler-open span:after {
  transform: translateY(-3px) rotate(-45deg);
}
/* nav-toggler css start */


/* intro-area css start */
.intro-area {
  height: calc(100vh - 61px);
  display: flex;
  align-items: center;
  text-align: center;
  color: #fff;
}
.intro-area h2 {
  font-size: 50px;
  font-weight: 300;
  line-height: 50px;
  margin-bottom: 25px;
}
.intro-area p {
  font-size: 18px;
}
/* intro-area css end */


/* mobile breakpoint start */
@media screen and (max-width: 767px) {
  .container {
    max-width: 720px;
  }
  /* navbar css for mobile start */
  .nav-toggler{
    display: block;
  }
  .site-navbar {
    min-height: 60px;
  }
  .site-navbar ul {
    position: absolute;
    width: 100%;
    height: calc(100vh - 60px);
    left: 0;
    top: 60px;
    flex-direction: column;
    align-items: center;
    border-top: 1px solid #444;
    background-color: rgba(0,0,0,.75);
    max-height: 0;
    overflow: hidden;
    transition: .3s;
  }
  .site-navbar ul li {
    width: 100%;
    text-align: center;
  }
  .site-navbar ul li a {
    padding: 25px;
  }
  .site-navbar ul li a:hover {
    background-color: rgba(255,255,255,.1);
  }
  .site-navbar ul.open {
    max-height: 100vh;
    overflow: visible;
  }
  .intro-area h2 {
    font-size: 36px;
    margin-bottom: 15px;
  }  
  /* navbar css for mobile end */
}
/* mobile breakpoint end */
Step 3: Implement JavaScript

Integrate the provided JavaScript code into your script.js file to elevate user experience with interactive elements. The code includes a responsive burger menu functionality, ensuring smooth navigation on smaller screens. Feel free to modify the script to accommodate additional features or customizations for your navbar.

// define all UI variable
const navToggler = document.querySelector('.nav-toggler');
const navMenu = document.querySelector('.site-navbar ul');
const navLinks = document.querySelectorAll('.site-navbar a');

// load all event listners
allEventListners();

// functions of all event listners
function allEventListners() {
  // toggler icon click event
  navToggler.addEventListener('click', togglerClick);
  // nav links click event
  navLinks.forEach( elem => elem.addEventListener('click', navLinkClick));
}

// togglerClick function
function togglerClick() {
  navToggler.classList.toggle('toggler-open');
  navMenu.classList.toggle('open');
}

// navLinkClick function
function navLinkClick() {
  if(navMenu.classList.contains('open')) {
    navToggler.click();
  }
}

Conclusion & Final Thoughts:

By following these steps, you’ll create a responsive navbar that not only enhances user navigation but also showcases your coding prowess. Regularly preview your work to ensure a seamless experience across different devices.

Crafting a Responsive Navbar using HTML, CSS, and JavaScript is not just about coding skills; it’s about creating a user-friendly and visually appealing navigation experience. Adaptability to diverse screen sizes is crucial in today’s varied device landscape.

Embrace this tutorial as an opportunity to explore, experiment, and enjoy the process of building a project from scratch. Your responsive navbar will not only demonstrate your coding expertise but also stand as evidence of your ability to create functional and engaging web components.

Code by — https://codepen.io/naieem/

Join Our Telegram Channel to Download the Project Source Code: Click Here

For your convenience, the total source code of this “login and signup form” project instructional practice is accessible for download by clicking the Download Code button.

Note: Keep in mind that the way to dominate coding is practice. To enhance your skills in JavaScriptHTML & CSS, we recommend recreating other useful website elements such as Custom ButtonReviews CardContact PageNavigationLogin Forms, etc.

Leave a Comment

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

Scroll to Top