How To Make Active Navbar In HTML CSS And JavaScript ? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report An active navbar refers to a navigation bar on a website where the current or active page is visually highlighted or distinguished from the other menu items. In this article, we are going to learn how to make an Active Navbar in HTML, CSS, and JavaScript. Prerequisites:HTMLCSSJavaScriptApproachUse HTML to give structure using an unordered list (<ul>) and list items (<li>) for each navigation element, assigning unique IDs or classes. Design the navigation bar using CSS and give it a special look when a menu item is selected. In JavaScript, Select all navigation items with the class '.nav-item' using const navItems = document.querySelectorAll('.nav-item');.Iterate through each navigation item using navItems.forEach(item => {...}).Add a click event listener to each navigation item to respond when a user clicks on it.Example: In this example, we have implemented the above approach. JavaScript //script.js document.addEventListener('DOMContentLoaded', function () { const navItems = document .querySelectorAll('.nav-item'); navItems.forEach(item => { item.addEventListener('click', function () { navItems.forEach(navItem => navItem .classList.remove('active')); this.classList.add('active'); }); }); }); HTML <!-- Index.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="styles.css"> <title>Active Navbar</title> </head> <body> <nav> <ul> <li class="nav-item" id="home"> <a href="#">Home</a> </li> <li class="nav-item" id="about"> <a href="#">About</a> </li> <li class="nav-item" id="services"> <a href="#">Services</a> </li> <li class="nav-item" id="contact"> <a href="#">Contact</a> </li> </ul> </nav> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20231226123149/gfg-image.jpg"> <script src="script.js"></script> </body> </html> CSS /* styles.css */ body { margin: 0; font-family: Arial, sans-serif; } img{ display: block; margin-left: auto; margin-right: auto; width: 50%; } nav { background-color: #333; } ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; } .nav-item { float: left; } .nav-item a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } .nav-item.active { background-color: #4CAF50; } Output: Comment More infoAdvertise with us Next Article How To Make Active Navbar In HTML CSS And JavaScript ? A amanv09 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to make curved active tab in navigation menu using HTML CSS & JavaScript ? In this article, we will learn about the curved outside in the active tab used in the navigation menu using HTML, CSS & JavaScript. One of the most beautiful and good-looking designs of a navigation menu is the 'Curve outside in Active Tab' design. With the help of the CSS border-radius property 6 min read How to create a revealing sidebar using HTML CSS and JavaScript? A revealing sidebar is a hidden UI element that becomes visible upon user interaction, such as clicking or swiping, providing navigation links. The content of the page will rotate and the navigation bar will reveal itself when the menu button is clicked. ApproachCreate an HTML file with headings and 3 min read How to create a Landing page using HTML CSS and JavaScript ? A landing page, also referred to as a lead capture page, static page, or destination page, serves a specific purpose and typically appears as a result of online advertising or search engine optimization efforts. Unlike a homepage, a landing page is stripped of distractions and focuses on capturing v 7 min read Design a Navigation Bar with Toggle Dark Mode in HTML CSS & JavaScript One can create a navigation bar with a toggle switch for dark mode using HTML, CSS, and JavaScript. If we use the dark mode effect in our website then it can attract user attention and users can use our website in any situation. With the help of JavaScript, we'll implement the functionality to toggl 6 min read Create A Responsive Navbar with Icons using HTML CSS and JavaScript The navigation bar, or Navbar is an essential component in modern web design. It allows users to navigate through a website easily. Adding icons to the navbar is not only enhances visual appeal but also improves user experience. The Responsive Navigation bar means the Navbar elements are visible on 2 min read Design a Toggleable Sidebar using HTML CSS and JavaScript In this article, we will create a toggleable sidebar navigation menu using HTML, CSS, and JavaScript. The navigation menu allows users to open and close a sidebar to access various sections of a website. When the user clicks on a navigation item, the corresponding content is displayed in the main co 3 min read Create a Hoverable Side Navigation with HTML, CSS and JavaScript To create hoverable side navigation with icon on any website there is a need for two things HTML and CSS. If you want to attach the icons on the navigation bar then you need a font-awesome CDN link. These features make the website looks cool than a normal website where the nav-bar is old-school desi 4 min read How to Create Navigable Tabs in HTML Navigable tabs in HTML are interactive elements that allow users to switch between different content sections without reloading the page. Tabs organize content into panels, and users can navigate between them by clicking the tab headers, enhancing user experience through better content management.He 2 min read Create a Mobile Toggle Navigation Menu using HTML, CSS and JavaScript To create a Mobile Toggle Navigation Menu you need HTML, CSS, and JavaScript. If you want to attach the icons with the menu then you need a font-awesome CDN link. This article is divided into two sections: Creating Structure and Designing Structure.A glimpse of complete Navigation:Â Creating Structur 3 min read Design a Business Agency Website in HTML CSS & JavaScript A business agency website can be used to showcase any kind of business in an attractive and interactive way to the users. It contains different sections that represent the services offered by your business. Approach:The HTML structure includes various sections such as the header, navigation, main co 12 min read Like