Published on

Best Ways To Build A Multi Level Dropdown Menu With Animation With Tailwind CSS

Multi Level Dropdown Menu with Animation

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 with Animation ui component

A nestable dropdown menu. useful when displaying multi level content. delightful animated.

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

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

The preview of Multi Level Dropdown Menu with Animation ui component

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

The source code of Multi Level Dropdown Menu with Animation ui component

<div class="group inline-block">
  <button
    class="outline-none focus:outline-none border px-3 py-1 bg-white rounded-sm flex items-center min-w-32"
  >
    <span class="pr-1 font-semibold flex-1">Dropdown</span>
    <span>
      <svg
        class="fill-current h-4 w-4 transform group-hover:-rotate-180
        transition duration-150 ease-in-out"
        xmlns="http://www.w3.org/2000/svg"
        viewBox="0 0 20 20"
      >
        <path
          d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"
        />
      </svg>
    </span>
  </button>
  <ul
    class="bg-white border rounded-sm transform scale-0 group-hover:scale-100 absolute 
  transition duration-150 ease-in-out origin-top min-w-32"
  >
    <li class="rounded-sm px-3 py-1 hover:bg-gray-100">Programming</li>
    <li class="rounded-sm px-3 py-1 hover:bg-gray-100">DevOps</li>
    <li class="rounded-sm relative px-3 py-1 hover:bg-gray-100">
      <button
        class="w-full text-left flex items-center outline-none focus:outline-none"
      >
        <span class="pr-1 flex-1">Langauges</span>
        <span class="mr-auto">
          <svg
            class="fill-current h-4 w-4
            transition duration-150 ease-in-out"
            xmlns="http://www.w3.org/2000/svg"
            viewBox="0 0 20 20"
          >
            <path
              d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"
            />
          </svg>
        </span>
      </button>
      <ul
        class="bg-white border rounded-sm absolute top-0 right-0 
  transition duration-150 ease-in-out origin-top-left
  min-w-32
  "
      >
        <li class="px-3 py-1 hover:bg-gray-100">Javascript</li>
        <li class="rounded-sm relative px-3 py-1 hover:bg-gray-100">
          <button
            class="w-full text-left flex items-center outline-none focus:outline-none"
          >
            <span class="pr-1 flex-1">Python</span>
            <span class="mr-auto">
              <svg
                class="fill-current h-4 w-4
                transition duration-150 ease-in-out"
                xmlns="http://www.w3.org/2000/svg"
                viewBox="0 0 20 20"
              >
                <path
                  d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"
                />
              </svg>
            </span>
          </button>
          <ul
            class="bg-white border rounded-sm absolute top-0 right-0 
      transition duration-150 ease-in-out origin-top-left
      min-w-32
      "
          >
            <li class="px-3 py-1 hover:bg-gray-100">2.7</li>
            <li class="px-3 py-1 hover:bg-gray-100">3+</li>
          </ul>
        </li>
        <li class="px-3 py-1 hover:bg-gray-100">Go</li>
        <li class="px-3 py-1 hover:bg-gray-100">Rust</li>
      </ul>
    </li>
    <li class="rounded-sm px-3 py-1 hover:bg-gray-100">Testing</li>
  </ul>
</div>

<style>
  /* since nested groupes are not supported we have to use 
     regular css for the nested dropdowns 
  */
  li>ul                 { transform: translatex(100%) scale(0) }
  li:hover>ul           { transform: translatex(101%) scale(1) }
  li > button svg       { transform: rotate(-90deg) }
  li:hover > button svg { transform: rotate(-270deg) }

  /* Below styles fake what can be achieved with the tailwind config
     you need to add the group-hover variant to scale and define your custom
     min width style.
  	 See https://codesandbox.io/s/tailwindcss-multilevel-dropdown-y91j7?file=/index.html
  	 for implementation with config file
  */
  .group:hover .group-hover\:scale-100 { transform: scale(1) }
  .group:hover .group-hover\:-rotate-180 { transform: rotate(180deg) }
  .scale-0 { transform: scale(0) }
  .min-w-32 { min-width: 8rem }
</style>

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

Install tailwind css of verion 1.2.0

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

<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 with Animation component

  • inline-block
  • px-3
  • py-1
  • bg-white
  • flex
  • min-w-32
  • pr-1
  • flex-1
  • h-4
  • w-4
  • absolute
  • hover:bg-gray-100
  • relative
  • w-full
  • text-left
  • mr-auto
  • top-0
  • right-0

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

  1. Use inline-block utilities to wrap the element to prevent the text inside from extending beyond its parent.

  2. Control the horizontal padding of an element to 0.75rem using the px-3 utilities.

  3. Control the vertical padding of an element to 0.25rem using the py-1 utilities.

  4. Control the background color of an element to white using the bg-white utilities.

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

  6. Set the minimum width/height of an element using the min-w-32 utilities.

  7. Control the padding on right side of an element to 0.25rem using the pr-1 utilities.

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

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

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

  11. 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.

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

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

  14. Use w-full to set an element to a 100% based width.

  15. Control the text color of an element to left using the text-left utilities.

  16. Control the margin on right side of an element to auto using the mr-auto utilities.

  17. Use the top-0 utilities to set the top position of a positioned element to 0rem.

  18. Use the right-0 utilities to set the right position of a positioned element to 0rem.

Conclusion

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