Published on

Best Ways To Make A Multi Level Dropdown Menu With Tailwind CSS

Multi Level Dropdown Menu

What is Tailwind CSS?

Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.

The description of Multi Level Dropdown Menu ui component

Multi level dropdown menu using popper.js

Why use Tailwind CSS to build a Multi Level Dropdown Menu ui component?

  • It can make the building process of Multi Level Dropdown Menu ui component faster and more easily.
  • Enables building complex responsive layouts and components freely.
  • Minimum lines of CSS code in Multi Level Dropdown Menu component file.

The preview of Multi Level Dropdown Menu ui component

Free download of the Multi Level Dropdown Menu's source code

The source code of Multi Level Dropdown Menu ui component

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>FrontendFunn - Responsive Multi Level Dropdown Tailwind CSS</title>
 
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/material-design-icons/3.0.1/iconfont/material-icons.min.css"
      integrity="sha256-x8PYmLKD83R9T/sYmJn1j3is/chhJdySyhet/JuHnfY="
      crossorigin="anonymous"
    />
  <style>
    @import url("https://fonts.googleapis.com/css2?family=Rubik:wght@300;400;500;700;900&display=swap");
html,
body {
  font-family: "Rubik", sans-serif;
  height: 100%;
}

.dropdown-menu[data-show] {
  display: flex;
}
    </style>
  
  </head>

  <body>
    <div class="container">
      <!------------------ LEVEL 1 ----------------------->
      <div class="dropdown relative inline-flex cursor-pointer">
        <!-- dropdown text -->
        <span
          class="dropdown-text flex px-4 py-3 bg-gray-400 rounded text-gray-800 items-center hover:bg-gray-800 hover:text-white whitespace-no-wrap"
          >Dropdown Level 1
          <i
            class="material-icons ml-2 h-4 w-4 text-2xl inline-flex items-center justify-center"
            >keyboard_arrow_down</i
          >
        </span>
        <!-- dropdown text -->
        <!-- dropdown menu -->
        <div class="dropdown-menu flex flex-col hidden absolute">
          <div
            class="rounded flex flex-col flex-grow bg-gray-400 divide-y divide-gray-200"
          >
            <div class="dropdown-item flex">
              <a
                href="#"
                class="dropdown-link flex flex-grow px-4 py-3 text-gray-800 whitespace-no-wrap rounded hover:bg-gray-800 hover:text-white"
                >Link One</a
              >
            </div>
            <div class="dropdown-item flex">
              <a
                href="#"
                class="dropdown-link flex flex-grow px-4 py-3 text-gray-800 whitespace-no-wrap rounded hover:bg-gray-800 hover:text-white"
                >Link Two</a
              >
            </div>
            <div class="dropdown-item">
              <!------------------ LEVEL 2 ----------------------->
              <div class="dropdown relative flex">
                <!-- dropdown text -->
                <span
                  class="dropdown-text flex px-4 py-3 bg-gray-400 rounded text-gray-800 items-center hover:bg-gray-800 hover:text-white whitespace-no-wrap"
                  >Dropdown Level 2
                  <i
                    class="material-icons ml-2 h-4 w-4 text-2xl inline-flex items-center justify-center"
                    >keyboard_arrow_down</i
                  >
                </span>
                <!-- dropdown text -->
                <!-- dropdown menu -->
                <div class="dropdown-menu flex flex-grow hidden absolute">
                  <div
                    class="rounded flex flex-col flex-grow bg-gray-400 divide-y divide-gray-200"
                  >
                    <div class="dropdown-item flex flex-grow">
                      <a
                        href="#"
                        class="dropdown-link flex flex-grow px-4 py-3 text-gray-800 whitespace-no-wrap rounded hover:bg-gray-800 hover:text-white"
                        >Level Two Link</a
                      >
                    </div>
                    <div class="dropdown-item flex flex-grow">
                      <!------------------ LEVEL 3 ----------------------->
                      <div class="dropdown relative flex">
                        <!-- dropdown text -->
                        <span
                          class="dropdown-text flex px-4 py-3 bg-gray-400 rounded text-gray-800 items-center hover:bg-gray-800 hover:text-white whitespace-no-wrap"
                          >Dropdown Level 3
                          <i
                            class="material-icons ml-2 h-4 w-4 text-2xl inline-flex items-center justify-center"
                            >keyboard_arrow_down</i
                          >
                        </span>
                        <!-- dropdown text -->
                        <!-- dropdown menu -->
                        <div
                          class="dropdown-menu flex flex-grow hidden absolute"
                        >
                          <div
                            class="rounded flex flex-col flex-grow bg-gray-400 divide-y divide-gray-200"
                          >
                            <div class="dropdown-item flex flex-grow">
                              <a
                                href="#"
                                class="dropdown-link flex flex-grow px-4 py-3 text-gray-800 whitespace-no-wrap rounded hover:bg-gray-800 hover:text-white"
                                >Level Three Link</a
                              >
                            </div>
                            <div class="dropdown-item flex flex-grow">
                              <!------------------ LEVEL 4 ----------------------->
                              <div class="dropdown relative flex">
                                <!-- dropdown text -->
                                <span
                                  class="dropdown-text flex px-4 py-3 bg-gray-400 rounded text-gray-800 items-center hover:bg-gray-800 hover:text-white whitespace-no-wrap"
                                  >Dropdown Level 4
                                  <i
                                    class="material-icons ml-2 h-4 w-4 text-2xl inline-flex items-center justify-center"
                                    >keyboard_arrow_down</i
                                  >
                                </span>
                                <!-- dropdown text -->
                                <!-- dropdown menu -->
                                <div
                                  class="dropdown-menu flex flex-grow hidden absolute"
                                >
                                  <div
                                    class="rounded flex flex-col flex-grow bg-gray-400 divide-y divide-gray-200"
                                  >
                                    <div class="dropdown-item flex flex-grow">
                                      <a
                                        href="#"
                                        class="dropdown-link flex flex-grow px-4 py-3 text-gray-800 whitespace-no-wrap rounded hover:bg-gray-800 hover:text-white"
                                        >Level Four Link</a
                                      >
                                    </div>
                                    <div class="dropdown-item flex flex-grow">
                                      <!------------------ LEVEL 5 ----------------------->
                                      <div class="dropdown relative flex">
                                        <!-- dropdown text -->
                                        <span
                                          class="dropdown-text flex px-4 py-3 bg-gray-400 rounded text-gray-800 items-center hover:bg-gray-800 hover:text-white whitespace-no-wrap"
                                          >Dropdown Level 5
                                          <i
                                            class="material-icons ml-2 h-4 w-4 text-2xl inline-flex items-center justify-center"
                                            >keyboard_arrow_down</i
                                          >
                                        </span>
                                        <!-- dropdown text -->
                                        <!-- dropdown menu -->
                                        <div
                                          class="dropdown-menu flex flex-grow hidden absolute"
                                        >
                                          <div
                                            class="rounded flex flex-col flex-grow bg-gray-400 divide-y divide-gray-200"
                                          >
                                            <div
                                              class="dropdown-item flex flex-grow"
                                            >
                                              <a
                                                href="#"
                                                class="dropdown-link flex flex-grow px-4 py-3 text-gray-800 whitespace-no-wrap rounded hover:bg-gray-800 hover:text-white"
                                                >Level Five Link</a
                                              >
                                            </div>
                                          </div>
                                        </div>
                                        <!-- dropdown menu -->
                                      </div>
                                      <!------------------ LEVEL 5 ----------------------->
                                    </div>
                                  </div>
                                </div>
                                <!-- dropdown menu -->
                              </div>
                              <!------------------ LEVEL 4 ----------------------->
                            </div>
                          </div>
                        </div>
                        <!-- dropdown menu -->
                      </div>
                      <!------------------ LEVEL 3 ----------------------->
                    </div>
                  </div>
                </div>
                <!-- dropdown menu -->
              </div>
              <!------------------ LEVEL 2 ----------------------->
            </div>
          </div>
        </div>
        <!-- dropdown menu -->
      </div>
      <!------------------ LEVEL 1 ----------------------->
    </div>
    <script
      src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"
      integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
      crossorigin="anonymous"
    ></script>
    <script
      src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/2.4.0/umd/popper.min.js"
      integrity="sha256-FT/LokHAO3u6YAZv6/EKb7f2e0wXY3Ff/9Ww5NzT+Bk="
      crossorigin="anonymous"
    ></script>
    <script>
      $(document).ready(function () {
  $(".dropdown").each(function (_, dropdown) {
    const dropdownMenu = $(dropdown).find("> .dropdown-menu")[0];
    let popperInstance = null;

    function create() {
      popperInstance = Popper.createPopper(dropdown, dropdownMenu, {
        placement: "auto-start",
        strategy: "absolute",
        modifiers: [
          {
            name: "flip",
            options: {
              fallbackPlacements: ["top", "bottom", "left", "right"],
            },
          },
        ],
      });
    }
    function destroy() {
      if (popperInstance) {
        popperInstance.destroy();
        popperInstance = null;
      }
    }

    function show() {
      $(dropdownMenu).attr("data-show", "");
      create();
    }

    function hide() {
      $(dropdownMenu).removeAttr("data-show");
      destroy();
    }

    $(dropdown).on("mouseenter focus", show);
    $(dropdown).on("mouseleave blur", hide);
  });
});
    </script>
  </body>
</html>

How to build a Multi Level Dropdown Menu with Tailwind CSS?

Install tailwind css of verion 1.4.6

Use the link html tag to import the stylesheet of Tailwind CSS of the version 1.4.6

<link href=https://unpkg.com/[email protected]/dist/tailwind.min.css rel="stylesheet">

All the unility class needed to build a Multi Level Dropdown Menu component

  • relative
  • inline-flex
  • flex
  • px-4
  • py-3
  • bg-gray-400
  • text-gray-800
  • hover:bg-gray-800
  • hover:text-white
  • ml-2
  • h-4
  • w-4
  • text-2xl
  • flex-col
  • hidden
  • absolute
  • flex-grow

17 steps to build a Multi Level Dropdown Menu component with Tailwind CSS

  1. Use relative to position an element according to the normal flow of the document.

  2. Use inline-flex to create an inline flex container that flows with text.

  3. Use flex to create a block-level flex container.

  4. Control the horizontal padding of an element to 1rem using the px-4 utilities.

  5. Control the vertical padding of an element to 0.75rem using the py-3 utilities.

  6. Control the background color of an element to gray-400 using the bg-gray-400 utilities.

  7. Control the text color of an element to gray-800 using the text-gray-800 utilities.

  8. Control the background color of an element to gray-800 using the hover:bg-gray-800 utilities on hover.

  9. Control the text color of an element to white on hover using the hover:text-white utilities.

  10. Control the margin on left side of an element to 0.5rem using the ml-2 utilities.

  11. Use h-4 to set an element to a fixed height(1rem).

  12. Use w-4 to set an element to a fixed width(1rem).

  13. Control the text color of an element to 2xl using the text-2xl utilities.

  14. Use flex to create a block-level flex container.

  15. Use hidden to set an element to display: none and remove it from the page layout.

  16. Use absolute to position an element outside of the normal flow of the document, causing neighboring elements to act as if the element doesn’t exist.

  17. Use flex to create a block-level flex container.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to build a Multi Level Dropdown Menu components, learn and follow along to implement your own components.