HTML Multilevel Accordion Navigation Menu

HTML Multilevel Accordion Navigation Menu
Code Snippet:Multi-Level Accordion Menu (WIP)
Author: Russell
Published: January 18, 2024
Last Updated: January 22, 2024
Downloads: 2,682
License: MIT
Edit Code online: View on CodePen
Read More

This HTML code snippet helps you to create a Multilevel Accordion Navigation Menu. It creates a collapsible menu with multiple levels of submenus. It uses Bootstrap icons and CSS to style the menu. This menu is designed to work both with and without JavaScript, making it versatile for different website setups.

You can use this code for website navigation menus, especially if your site has multiple sections or pages. It helps improve user experience by organizing content into collapsible levels.

How to Create Multilevel Accordion Navigation Menu Using HTML CSS & JS

1. First of all, load the Bootstrap Icons by adding the following CDN link into the head tag of your HTML document.

  1. <link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.2/font/bootstrap-icons.css'>

2. Copy and paste the following HTML code into your HTML document. Place it where you want the menu to appear. Replace the sample links with your website links:

  1. <!-- Note works with or without JS -->
  2. <nav id='accordion' class='dropdown'>
  3. <input
  4. type='checkbox'
  5. id='toggle-01'
  6. class='toggle burger-toggle'
  7. hidden
  8. >
  9. <label for='toggle-01' class='toggle-button'>
  10. <div class='burger'>
  11. <div class='line1'></div>
  12. <div class='line2'></div>
  13. <div class='line3'></div>
  14. </div>
  15. </label>
  16. <ul class='dropdown-menu lvl-1'>
  17. <li class='item'><a><i class='bi bi-house-fill'></i> Home</a></li>
  18. <li class='item'><a>About</a></li>
  19. <li class='dropdown'>
  20. <input type='checkbox' id='toggle-02' class='toggle' hidden>
  21. <label for='toggle-02' class='toggle-button caret'><i class='bi bi-image'></i> Gallery </label>
  22. <ul class='dropdown-menu lvl-2'>
  23. <li class='dropdown'>
  24. <input type='checkbox' id='toggle-03' class='toggle' hidden>
  25. <label for='toggle-03' class='toggle-button caret'>3D Graphics </label>
  26. <ul class='dropdown-menu lvl-3'>
  27. <li class='item'><a>Characters</a></li>
  28. <li class='item'><a>Landscapes</a></li>
  29. </ul>
  30. </li>
  31. <li class='item'><a>2D Illustration</a></li>
  32. </ul>
  33. </li>
  34. <li class='dropdown'>
  35. <input type='checkbox' id='toggle-04' class='toggle' hidden>
  36. <label for='toggle-04' class='toggle-button caret'><i class='bi bi-code-slash'></i> Web Development </label>
  37. <ul class='dropdown-menu lvl-2'>
  38. <li class='item'><a>Articles</a></li>
  39. <li class='dropdown'>
  40. <input type='checkbox' id='toggle-05' class='toggle' hidden>
  41. <label for='toggle-05' class='toggle-button caret'>Projects </label>
  42. <ul class='dropdown-menu lvl-3'>
  43. <li class='item'><a>JS</a></li>
  44. <li class='item'><a>CSS</a></li>
  45. </ul>
  46. </li>
  47. <li class='item'><a>Tutorials</a></li>
  48. </ul>
  49. </li>
  50. <li class='item'><a><i class='bi bi-envelope-fill'></i> Contact</a></li>
  51. </ul>
  52. </nav>

3. Similarly, copy and paste the CSS code into your HTML document or include it in a separate CSS file. This code provides styling for the menu. You can modify the CSS variables to change the menu’s appearance.

  1. @charset "UTF-8";
  2. @import "https://assets.codepen.io/3351103/__reset.css";
  3. @import url("https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500&display=swap");
  4. :root {
  5. --nav-bg-color: rgb(21, 21, 21);
  6. --toggle-color: rgb(221, 221, 221);
  7. --toggle-bg-color: rgb(40 40 40);
  8. --nav-link-bg-color-01: rgb(50 50 50);
  9. --nav-link-bg-color-02: rgb(30 30 30);
  10. --nav-link-bg-color-03: rgb(15 15 15);
  11. --nav-link-color: hsl(0 0% 90%);
  12. --nav-secondary-color: rgb(255, 193, 7);
  13. --nav-caret-color: var(--nav-secondary-color);
  14. --nav-hover-color: hsl(0 0% 90%);
  15. --nav-bg-hover-color-01: rgb(209, 59, 45);
  16. --ff-body: "Poppins", sans-serif;
  17. --ff-nav: "Poppins", sans-serif;
  18. --fw-200: 200;
  19. --fw-300: 300;
  20. --fw-400: 400;
  21. --fw-500: 500;
  22. --fs-300: .9rem
  23. --fs-400: .95rem;
  24. --fs-500: 1.2rem;
  25. --calc-height: auto;
  26. --calc-transition-delay: 500ms;
  27. --dropdown-indent: 0px;
  28. --burger-width: 40px;
  29. --mobile-break: 480px;
  30. }
  31.  
  32. body {
  33. display: grid;
  34. justify-items: center;
  35. height: 100vh;
  36. padding: 1rem;
  37. font-family: var(--ff-body);
  38. font-weight: var(--fw-300);
  39. font-size: var(--fs-400);
  40. background-color: var(--nav-bg-color);
  41. }
  42.  
  43. ul {
  44. list-style: none;
  45. margin: 0;
  46. }
  47.  
  48. a {
  49. cursor: pointer;
  50. }
  51.  
  52. #accordion {
  53. width: 100%;
  54. max-width: var(--mobile-break);
  55. height: fit-content;
  56. color: var(--nav-link-color);
  57. font-size: var(--fs-300);
  58. /* dropdown links level 1 */
  59. /* dropdown links level 2 */
  60. /* dropdown links level 3 */
  61. }
  62. #accordion [hidden] {
  63. margin: 0;
  64. }
  65. #accordion a {
  66. display: block;
  67. padding: 0.75rem 0;
  68. font-family: var(--ff-nav);
  69. text-align: center;
  70. color: var(--nav-link-color);
  71. transition: color 150ms, background-color 150ms;
  72. }
  73. #accordion ul > li > a {
  74. background-color: var(--nav-link-bg-color-01);
  75. }
  76. #accordion ul ul > li > a {
  77. background-color: var(--nav-link-bg-color-02);
  78. }
  79. #accordion ul ul ul > li > a {
  80. background-color: var(--nav-link-bg-color-03);
  81. }
  82. #accordion ul > li > a:hover {
  83. color: var(--nav-hover-color);
  84. background-color: var(--nav-bg-hover-color-01);
  85. }
  86. #accordion .toggle-button {
  87. display: block;
  88. padding: 0.75rem 1rem;
  89. color: var(--toggle-color);
  90. background-color: var(--toggle-bg-color);
  91. cursor: pointer;
  92. user-select: none;
  93. }
  94. #accordion .dropdown-menu {
  95. position: relative;
  96. height: 0;
  97. overflow: hidden;
  98. padding-left: var(--dropdown-indent);
  99. transition: height var(--calc-transition-delay);
  100. }
  101. #accordion .toggle:checked ~ .dropdown-menu {
  102. height: var(--calc-height);
  103. }
  104. #accordion .caret:after {
  105. content: "▼";
  106. font-size: 0.9rem;
  107. line-height: 1;
  108. display: inline-block;
  109. color: var(--nav-caret-color);
  110. transform: rotatez(-90deg);
  111. transition: transform 150ms linear;
  112. }
  113. #accordion .toggle:checked + .caret:after {
  114. transform: rotatez(0deg);
  115. }
  116.  
  117. /* ---- burger toggle ----- */
  118. nav .burger {
  119. display: grid;
  120. place-content: center;
  121. width: var(--burger-width);
  122. height: calc(var(--burger-width) * .8);
  123. border: 2px solid var(--nav-secondary-color);
  124. border-radius: 4px;
  125. cursor: pointer;
  126. }
  127. nav .burger [class^=line] {
  128. width: 26px;
  129. height: 2px;
  130. margin-bottom: 5px;
  131. background-color: var(--nav-secondary-color);
  132. transition: transform 0.2s ease-out;
  133. }
  134. nav .burger .line3 {
  135. margin-bottom: 0px;
  136. }
  137. nav .burger:hover .line1 {
  138. transform: translate(0px, 2px);
  139. }
  140. nav .burger:hover .line3 {
  141. transform: translate(0px, -2px);
  142. }
  143. nav .burger-toggle:checked + .toggle-button .burger .line1 {
  144. transform: rotate(45deg) translate(5px, 5px);
  145. }
  146. nav .burger-toggle:checked + .toggle-button .burger .line2 {
  147. opacity: 0;
  148. }
  149. nav .burger-toggle:checked + .toggle-button .burger .line3 {
  150. transform: rotate(-45deg) translate(5px, -5px);
  151. }
  152.  
  153. /* ---- END burger toggle ----- */
  154. /* ---- some custom styling ---- */
  155. #accordion {
  156. --nav-link-bg-color-01: hsl(200 80% 13%);
  157. --nav-link-bg-color-02: hsl(200 80% 11%);
  158. --nav-link-bg-color-03: hsl(200 80% 9%);
  159. --nav-link-color: hsl(0 0% 90%);
  160. --nav-secondary-color: hsl(11 100% 65%);
  161. --nav-caret-color: hsl(11 100% 65%);
  162. --toggle-bg-color: hsl(200 80% 15%);
  163. --fs-300: .95rem;
  164. --burger-width: 36px;
  165. --mobile-break: 400px;
  166. }
  167. #accordion .burger {
  168. margin: 0.15rem 0;
  169. }
  170. #accordion a {
  171. text-align: left;
  172. }
  173. #accordion .bi {
  174. margin-right: 5px;
  175. }
  176. #accordion .toggle:checked + .toggle-button .bi {
  177. color: var(--nav-secondary-color);
  178. }
  179. #accordion .lvl-1 a, #accordion .lvl-1 .toggle-button {
  180. padding-left: 1.5rem;
  181. }
  182. #accordion .lvl-2 a, #accordion .lvl-2 .toggle-button {
  183. padding-left: 2.5rem;
  184. }
  185. #accordion .lvl-3 a {
  186. padding-left: 3.5rem;
  187. }

4. The code includes JavaScript for handling menu interactions. If you want to enable this functionality, ensure that the JavaScript code is placed at the end of the <body> section of your HTML document.

  1. window.CP.PenTimer.MAX_TIME_IN_LOOP_WO_EXIT = 6000;
  2.  
  3. // searches next siblings for a target element
  4. const findNextSibling = (elem, selector) => {
  5. while (elem !== null) {
  6. elem = elem.nextElementSibling
  7. if (elem.matches(selector)) return elem
  8. }
  9. return elem
  10. }
  11.  
  12. // if unable to toggle between a and b or b and a
  13. // will return false
  14. const toggleBetween = (elem, a, b) => {
  15. return elem.classList.replace(a, b) || elem.classList.replace(b, a)
  16. }
  17.  
  18. // A recursive function to set the total height of an open dropdown
  19. // based on the heights of its child elements
  20. const calcHeights = function (elems) {
  21. let height = 0
  22. // argument for first call will be a single root element
  23. // recursive calls will be children
  24. if (elems instanceof Element) elems = [elems]
  25.  
  26. for (const elem of elems) {
  27. // only include calculations for opened dropdowns
  28. if (elem.matches('.dropdown, .open')) {
  29. const parentHeight = calcHeights(elem.children)
  30.  
  31. if (elem.matches('.open')) {
  32. elem.style.setProperty('--calc-height', parentHeight + 'px')
  33. }
  34.  
  35. height += parentHeight
  36. } else {
  37. // getBoundingClientRect gives a more accurate height
  38. const elemHeight = elem.getBoundingClientRect().height
  39. height += (elem.matches('.closed')) ? 0 : elemHeight
  40. }
  41. }
  42. return height
  43. }
  44.  
  45. const toggleMenu = (dropdown, rootElem) => {
  46. if (!toggleBetween(dropdown, 'open', 'closed')) {
  47. // first time clicking on this dropdown
  48. dropdown.classList.add('open')
  49. }
  50. calcHeights(rootElem)
  51. }
  52.  
  53. window.addEventListener('DOMContentLoaded', () => {
  54. const rootElem = document.querySelector('#accordion .dropdown-menu')
  55. // change --calc-height from auto to 0 for initial transition
  56. document
  57. .documentElement
  58. .style.setProperty('--calc-height', '0px')
  59. document
  60. .querySelectorAll('#accordion .toggle')
  61. .forEach((toggle) => {
  62. const dropdown = findNextSibling(toggle, '.dropdown-menu')
  63. toggle.addEventListener('click', (event) => {
  64. toggleMenu(dropdown, rootElem)
  65. })
  66. })
  67. document.querySelector('#toggle-01').click()
  68. })

That’s all! hopefully, you have successfully created the Multilevel Accordion Navigation Menu using HTML, CSS, and JavaScript. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X