- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to make active tab in navigation menu using HTML CSS & JavaScript?
With the help of HTML, CSS, and JavaScript, it is possible to create a navigation menu with a curved active tab. This can be done by using the ::before and ::after pseudo-elements to create the desired shape, and then using JavaScript to add the active class to the element.
HTML
The HTML for this navigation menu is very simple. There is a ul element with a class of "nav" and within that, there are six li elements, each with a class of "nav-item".
<ul class="nav"> <li class="nav-item"><a href="#">Home</a></li> <li class="nav-item"><a href="#">About</a></li> <li class="nav-item"><a href="#">Services</a></li> <li class="nav-item"><a href="#">Portfolio</a></li> <li class="nav-item"><a href="#">Contact</a></li> </ul>
CSS
The CSS for this navigation menu is fairly straightforward. The ::before and ::after pseudo-elements are used to create the two halves of the active tab. These are then positioned absolutely so that they sit on top of the other tabs.
The .nav-item class is used to style the rest of the navigation menu. The a elements are given a display of block so that they fill the entire space of the li elements.
.nav { position: relative; list-style: none; padding: 0; margin: 0; } .nav::before, .nav::after { content: ""; position: absolute; top: 0; width: 50%; height: 100%; background: #fff; z-index: 1; } .nav::before { left: 0; border-radius: 0 0 0 50%; } .nav::after { right: 0; border-radius: 0 0 50% 0; } .nav-item { position: relative; float: left; width: 16.66667%; } .nav-item a { display: block; padding: 10px 20px; border: 1px solid #ccc; text-decoration: none; color: #333; position: relative; z-index: 2; }
JavaScript
The JavaScript for this navigation menu is very simple. It just needs to add the active class to the element when it is clicked. This can be done with the help of the classList property.
var navItems = document.querySelectorAll(".nav-item"); for (var i = 0; i < navItems.length; i++) { navItems[i].addEventListener("click", function() { this.classList.add("active"); }); }
Example
Below is the full working program to create active tab in navigation menu. We use above discussed HTML, CSS and JavaScript to achieve this goal.
<!DOCTYPE HTML> <html> <head> <style>HTML .nav { position: relative; list-style: none; padding: 0; margin: 0; } .nav::before{ content: ""; position: absolute; top: 0; width: 50%; height: 100%; background: #fff; z-index: 1; } .active{ background: rgb(177, 177, 177); } .nav::after { content: ""; position: absolute; top: 0; width: 50%; height: 100%; background: rgb(0, 0, 0); z-index: 1; } .nav::before { left: 0; border-radius: 0 0 0 50%; } .nav::after { right: 0; border-radius: 0 0 50% 0; } .nav-item { position: relative; float: left; width: 16.66667%; } .nav-item a { display: block; padding: 10px 20px; border: 1px solid #ccc; text-decoration: none; color: #333; position: relative; z-index: 2; } </style> </head> <body> <ul class="nav"> <li class="nav-item"><a href="#">Home</a></li> <li class="nav-item"><a href="#">About</a></li> <li class="nav-item"><a href="#">Services</a></li> <li class="nav-item"><a href="#">Portfolio</a></li> <li class="nav-item"><a href="#">Contact</a></li> </ul> <script> var navItems = document.querySelectorAll(".nav-item"); for (var i = 0; i < navItems.length; i++) { navItems[i].addEventListener("click", function() { this.classList.add("active"); }); } </script> </body> </html>
In conclusion, it is possible to create a navigation menu with a curved active tab using HTML, CSS, and JavaScript. This can be done by using the ::before and ::after pseudo-elements to create the desired shape, and then using JavaScript to add the active class to the element.
- Related Articles
- How to create a vertical tab menu with CSS and JavaScript?
- How to create a curtain navigation menu with CSS and JavaScript?
- How to create a mega menu (full-width dropdown menu in a navigation bar) with HTML and CSS?
- How to create a responsive bottom navigation menu with CSS and JavaScript?
- Creating a Navigation Menu using CSS Image Sprite
- How to create a responsive navigation menu with icons, using CSS?\n
- How to create a side navigation menu with icons using CSS?\n
- How to create a full screen overlay navigation menu with CSS and JavaScript?
- How to hide a navigation menu on scroll down with CSS and JavaScript?
- How to create a bottom navigation menu with CSS?
- How to create a pill navigation menu with CSS?
- How to create a top navigation menu for smartphones / tablets with CSS and JavaScript?
- How to create a fixed side navigation menu with CSS?
- How to create a responsive side navigation menu with CSS?
- Creating Breadcrumbs navigation using HTML and CSS
