Are you looking to create a stylish drop-down navigation menu in HTML with icons? This code provides a sleek and functional menu that enhances user experience. By clicking on menu items, you can expand and collapse submenus, facilitating easy navigation through your website.
This code leverages HTML, CSS, and JavaScript to create an interactive and visually appealing drop-down navigation menu, making it helpful for you to improve your website’s usability.
How to Create Drop-Down Navigation Menu HTML with Icons
1. First of all, load Reset CSS and Font Awesome CSS by adding the following CDN links into the head tag of your HTML document.
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css'>
2. Now, let’s set up the basic HTML structure for the navigation menu. Copy and paste the following code into your HTML file:
<ul class="fmenu" id="mymenu"> <li class="fmenu-item"> <div class="trigger-menu expanded"> <i class="fa-regular fa-circle-user"></i> <span class="text">Hello, Lionel</span> </div> <ul class="floating-menu"> <li><a href='#'><i class="fa-regular fa-address-card"></i>Profile</a></li> <li><a href='#'><i class="fa-regular fa-comment-dots"></i>Contact support</a></li> <li><a href='#'><i class="fa-regular fa-money-bill-1"></i>Transactions</a></li> <li><a href='#'><i class="fa-solid fa-right-from-bracket"></i>Logout</a></li> </ul> </li> <li class="fmenu-item"> <div class="trigger-menu"> <i class="fa-solid fa-gear"></i> <span class="text">Settings</span> </div> <ul class="floating-menu"> <li><a href='#'><i class="fa-solid fa-palette"></i>Customize theme</a></li> <li><a href='#'><i class="fa-solid fa-language"></i>Language & Timezone</a></li> <li><a href='#'><i class="fa-solid fa-clock-rotate-left"></i>Restore defaults</a></li> <li><a href='#'><i class="fa-regular fa-file-lines"></i>Inspect logs</a></li> <li><a href='#'><i class="fa-regular fa-floppy-disk"></i>Backup your profile</a></li> </ul> </li> <li class="fmenu-item"> <div class="trigger-menu"> <i class="fa-regular fa-message"></i> <span class="text">Messages</span> </div> <ul class="floating-menu"> <li><a href='#'><i class="fa-solid fa-inbox"></i>Inbox</a></li> <li><a href='#'><i class="fa-solid fa-thumbtack"></i>Pinned</a></li> <li><a href='#'><i class="fa-solid fa-shapes"></i>Templates</a></li> </ul> </li> <li class="fmenu-item"> <div class="trigger-menu"> <i class="fa-regular fa-circle-right"></i> <span class="text">Navigate</span> </div> <ul class="floating-menu"> <li><a href='#'><i class="fa-solid fa-link"></i>Page link 1</a></li> <li><a href='#'><i class="fa-solid fa-link"></i>Page link 2</a></li> <li><a href='#'><i class="fa-solid fa-link"></i>Page link 3</a></li> <li><a href='#'><i class="fa-solid fa-link"></i>Page link 4</a></li> </ul> </li> </ul>
3. Style the navigation menu using the following CSS code. You can include it in your HTML file or link to an external CSS file.
@import url("https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap"); html, body { width: 100%; height: 100%; } body { font-family: Montserrat, "sans-serif" !important; background: #434343; background: linear-gradient(135deg, #434343 0%, black 100%); padding: 3rem; } /********************************************* Floating Menu: **********************************************/ ul.fmenu { color: #fff; display: inline-block; list-style: none; padding: 0; margin: 0; white-space: nowrap; } ul.fmenu > li.fmenu-item { display: inline-block; margin-right: 1rem; position: relative; } ul.fmenu .trigger-menu { display: flex; align-content: center; align-items: center; box-sizing: border-box; min-width: 3rem; max-width: 3rem; height: 3rem; border: 0.45rem solid transparent; border-radius: 3rem; overflow: hidden; text-align: center; background-color: #505050; cursor: pointer; transition: max-width ease 0.4s; } ul.fmenu .trigger-menu i { display: block; margin: 0 0.2925rem; color: white; font-size: 1.5rem; transition: color ease 0.3s; } ul.fmenu .trigger-menu span { display: block; font-size: 1rem; padding: 0 1ch; } ul.fmenu .trigger-menu.expanded { max-width: unset !important; } ul.fmenu .trigger-menu.open i { color: #ffd53e; } ul.fmenu .trigger-menu:hover:not(.expanded) i { color: #ffd53e; } ul.fmenu .floating-menu { display: block; position: absolute; top: 3.75rem; min-width: 100%; max-width: 250px; list-style: none; padding: 0; margin: 0; background-color: #505050; border-radius: 0.75rem; overflow: hidden; max-height: 0px; z-index: 100; opacity: 0; transition: max-height ease 0.6s, opacity ease 0.3s; } ul.fmenu .floating-menu > li { padding: 0 0.5rem; } ul.fmenu .floating-menu > li a { color: #fff; font-size: 0.85rem; text-decoration: none; display: block; padding: 0.75rem 0.5rem; } ul.fmenu .floating-menu > li a i { margin-right: 1ch; } ul.fmenu .floating-menu > li a:hover { color: #ffd53e; } ul.fmenu .floating-menu > li:not(:last-child) a { border-bottom: 1px solid rgba(0, 0, 0, 0.15); }
4. The magic behind the interactive menu is JavaScript. This code creates a sikFloatingMenu
class that handles the menu’s functionality. You can add this JavaScript code to your HTML file or link it from an external file.
//The menu js class: class sikFloatingMenu { menuEl = null; constructor(_menu) { //The menu element: this.menuEl = typeof _menu === 'string' ? document.querySelector(_menu) : _menu; //Attach handlers: this.attachHandlers(); } attachHandlers() { if (this.menuEl) { this._on(this.menuEl, 'click', '.trigger-menu', this._handler.bind(this)); } } _open(item) { let opened = item.closest('.fmenu').querySelectorAll('.trigger-menu.open'); for (const ele of opened) { this._close(ele); } item.classList.add('open'); //expand: let list = item.closest('li').querySelector(".floating-menu"); list.style.setProperty("max-height", this._measureExpandableList(list)); list.style.setProperty("opacity", "1"); item.style.setProperty("max-width", this._measureExpandableTrigger(item)); } _close(item) { let list = item.closest('li').querySelector(".floating-menu"); item.classList.remove('open'); //shrink: list.style.removeProperty("max-height"); list.style.removeProperty("opacity"); item.style.removeProperty("max-width"); } _measureExpandableList(list) { const items = list.querySelectorAll('li'); return (items.length * this._getHeight(items[0], "outer") + 10) + 'px'; } _measureExpandableTrigger(item) { const textEle = item.querySelector('.text'); const sizeBase = this._getWidth(item, "outer"); const sizeExpandLabel = this._getWidth(textEle, "outer"); return (sizeBase + sizeExpandLabel + 6) + 'px'; } _handler(el, ev) { if (el.classList.contains('open')) { this._close(el); } else { this._open(el); } } _on(ele, type, selector, handler) { ele.addEventListener(type, function(ev) { let el = ev.target.closest(selector); if (el) handler.call(this, el, ev); //The element is bind to this }); } _getWidth(el, type) { if (type === 'inner') return el.clientWidth; else if (type === 'outer') return el.offsetWidth; return 0; } _getHeight(el, type) { if (type === 'inner') return el.clientHeight; else if (type === 'outer') return el.offsetHeight; return 0; } } //Intialize menu: window.sik_menu = new sikFloatingMenu("#mymenu");
That’s all! hopefully, you have successfully created a dropdown navigation menu on your website. If you have any questions or suggestions, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.