How to Create a Responsive Navbar Using HTML, CSS & JavaScript

How to Create a Responsive Navbar Using HTML, CSS & JavaScript

Looking for a tutorial on how to create a responsive navbar using HTML, CSS & JavaScript? You’ve come to the right place! In this tutorial, we will guide you through creating a navbar that will look great on all devices.

A responsive navbar is an essential component of any modern website. It provides easy navigation and enhances the user experience across different devices and screen sizes. In this article, we will guide you step-by-step on how to create a responsive navbar using HTML, CSS, and JavaScript.

Understanding the Structure of a Navbar

Before diving into the implementation, it’s important to understand the typical structure of a navbar. A navbar usually consists of a container, a logo or brand name, and a list of navigation links. In our responsive navbar, we will also include a responsive menu icon for small screens.

Setting Up the HTML Markup

Let’s start by setting up the HTML markup for our navbar. Open your preferred text editor and create a new HTML file. Begin with the basic structure by adding the <!DOCTYPE> declaration, <html>, <head>, and <body> tags. Inside the <body> tag, create a <nav> element that will contain the navbar.

Styling the Navbar with CSS

Now it’s time to style our navbar using CSS. We’ll use CSS to define the layout, colors, and other visual aspects of the navbar. Apply appropriate CSS properties to the container, logo, navigation links, and the responsive menu icon. You can customize the styles according to your website’s design.

Implementing Responsive Behavior with JavaScript

To make our navbar responsive, we need to implement JavaScript functionality. This will allow the navbar to adapt to different screen sizes and provide a seamless user experience. We’ll use JavaScript to toggle a CSS class when the responsive menu icon is clicked, showing or hiding the navigation links.

Testing and Fine-tuning

After completing the HTML, CSS, and JavaScript implementation, it’s crucial to thoroughly test our responsive navbar. Open the HTML file in a web browser and check how it behaves across various screen sizes. Make any necessary adjustments to ensure that the navbar looks and functions perfectly on different devices.

Conclusion

In conclusion, creating a responsive navbar using HTML, CSS, and JavaScript is a fundamental skill for web developers. By following the steps outlined in this article, you should now have a good understanding of how to build a responsive navbar that enhances the user experience on your website.

FAQs

1. Can I use frameworks like Bootstrap to create a responsive navbar? Yes, frameworks like Bootstrap provide pre-built components, including responsive navbars. However, building a navbar from scratch using HTML, CSS, and JavaScript gives you more control and flexibility over its design and functionality.

2. Is it necessary to use JavaScript for a responsive navbar? While you can create a basic responsive navbar using only HTML and CSS, JavaScript allows for more dynamic and interactive behavior. It enables smooth navigation transitions and makes the navbar adaptable to different screen sizes.

3. How can I customize the appearance of the responsive menu icon? You can customize the appearance of the responsive menu icon by modifying its CSS properties. Change the color, size, and shape of the icon using CSS techniques such as pseudo-elements or background images.

4. Are there any browser compatibility issues with responsive navbars? Browser compatibility can sometimes be a concern when using CSS and JavaScript for responsive design. It’s important to test your navbar across different browsers and versions to ensure consistent behavior and appearance.

5. Where can I learn more about responsive web design? There are numerous online resources, tutorials, and courses available to learn more about responsive web design. Websites like MDN Web Docs and W3Schools offer comprehensive guides and examples to help you enhance your skills.

Index.html

				
					<!DOCTYPE html>
<!-- Coding By Dailywebdesign - dailywebdesigs.com -->
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Navigation Bar with Search Box</title>
    <link rel="stylesheet" href="style.css" />
    <!-- Unicons CSS -->
    <link rel="stylesheet" href="https://unicons.iconscout.com/release/v4.0.0/css/line.css" />
   <script src="script.js" defer></script>
  </head>
  <body>
    <nav class="nav">
      <i class="uil uil-bars navOpenBtn"></i>
      <a href="#" class="logo">Daily Web Design</a>

      <ul class="nav-links">
        <i class="uil uil-times navCloseBtn"></i>
        <li><a href="#">Home</a></li>
        <li><a href="#">Services</a></li>
        <li><a href="#">Products</a></li>
        <li><a href="#">About Us</a></li>
        <li><a href="#">Contact Us</a></li>
      </ul>
        
      <i class="uil uil-search search-icon" id="searchIcon"></i>
      <div class="search-box">
        <i class="uil uil-search search-icon"></i>
        <input type="text" placeholder="Search here..." />
      </div>
    </nav>
    
  </body>
</html>
				
			

style.css

				
					/* Google Fonts - Poppins */
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap");

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}
body {
  background: #FA5221;
}
.nav {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  padding: 15px 200px;
  background: #Ffff;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}
.nav,
.nav .nav-links {
  display: flex;
  align-items: center;
}
.nav {
  justify-content: space-between;
}
a {
  color: #FA5221;
  text-decoration: none;
  font-weight: 700;
  
}
.nav .logo {
  font-size: 22px;
  font-weight: 500;
}
.nav .nav-links {
  column-gap: 20px;
  list-style: none;
}
.nav .nav-links a {
  transition: all 0.2s linear;
  font-weight: 700; 
}

.nav .nav-links a:hover {
  background: #FA5221;
  color: #FFF;
}


.nav.openSearch .nav-links a {
  opacity: 0;
  pointer-events: none;
}
.nav .search-icon {
  color: #FA5221;
  font-size: 20px;
  cursor: pointer;
}
.nav .search-box {
  position: absolute;
  right: 250px;
  height: 45px;
  max-width: 555px;
  width: 100%;
  opacity: 0;
  pointer-events: none;
  transition: all 0.2s linear;
}
.nav.openSearch .search-box {
  opacity: 1;
  pointer-events: auto;
 
}
.search-box .search-icon {
  position: absolute;
  left: 15px;
  top: 50%;
  left: 15px;
  color: #FA5221;
  transform: translateY(-50%);
  
}
.search-box input {
  height: 100%;
  width: 100%;
  border: none;
  outline: none;
  border-radius: 6px;
  background-color: #FFF;
  padding: 0 15px 0 45px;
}

.nav .navOpenBtn,
.nav .navCloseBtn {
  display: none;
}




/* responsive */
@media screen and (max-width: 1160px) {
  .nav {
    padding: 15px 100px;
  }
  .nav .search-box {
    right: 150px;
  }
}
@media screen and (max-width: 950px) {
  .nav {
    padding: 15px 50px;
  }
  .nav .search-box {
    right: 100px;
    max-width: 400px;
  }
}
@media screen and (max-width: 768px) {
  .nav .navOpenBtn,
  .nav .navCloseBtn {
    display: block;
  }
  .nav {
    padding: 15px 20px;
  }
  .nav .nav-links {
    position: fixed;
    top: 0;
    left: -100%;
    height: 100%;
    max-width: 280px;
    width: 100%;
    padding-top: 100px;
    row-gap: 30px;
    flex-direction: column;
    background-color: #11101d;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    transition: all 0.4s ease;
    z-index: 100;
  }
  .nav.openNav .nav-links {
    left: 0;
  }
  .nav .navOpenBtn {
    color: #FA5221;
    font-size: 20px;
    cursor: pointer;
  }
  .nav .navCloseBtn {
    position: absolute;
    top: 20px;
    right: 20px;
    color: #FA5221;
    font-size: 20px;
    cursor: pointer;
  }
  .nav .search-box {
    top: calc(100% + 10px);
    max-width: calc(100% - 20px);
    right: 50%;
    transform: translateX(50%);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  }
}
				
			

main.js

				
					const nav = document.querySelector(".nav"),
  searchIcon = document.querySelector("#searchIcon"),
  navOpenBtn = document.querySelector(".navOpenBtn"),
  navCloseBtn = document.querySelector(".navCloseBtn");

searchIcon.addEventListener("click", () => {
  nav.classList.toggle("openSearch");
  nav.classList.remove("openNav");
  if (nav.classList.contains("openSearch")) {
    return searchIcon.classList.replace("uil-search", "uil-times");
  }
  searchIcon.classList.replace("uil-times", "uil-search");
});

navOpenBtn.addEventListener("click", () => {
  nav.classList.add("openNav");
  nav.classList.remove("openSearch");
  searchIcon.classList.replace("uil-times", "uil-search");
});
navCloseBtn.addEventListener("click", () => {
  nav.classList.remove("openNav");
});
				
			

Leave a Comment

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

Scroll to Top