Published on

Surprisingly Effective Ways To Make A Notifications Dropdown With Tailwind CSS

Notifications dropdown

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 Notifications dropdown ui component

Why use Tailwind CSS to build a Notifications dropdown ui component?

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

The preview of Notifications dropdown ui component

Free download of the Notifications dropdown's source code

The source code of Notifications dropdown ui component

<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.min.js" defer></script>


<div class="flex justify-center h-screen">
    <div x-data="{ dropdownOpen: true }" class="relative my-32">
        <button @click="dropdownOpen = !dropdownOpen" class="relative z-10 block rounded-md bg-white p-2 focus:outline-none">
            <svg class="h-5 w-5 text-gray-800" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
                <path d="M10 2a6 6 0 00-6 6v3.586l-.707.707A1 1 0 004 14h12a1 1 0 00.707-1.707L16 11.586V8a6 6 0 00-6-6zM10 18a3 3 0 01-3-3h6a3 3 0 01-3 3z" />
            </svg>
        </button>

        <div x-show="dropdownOpen" @click="dropdownOpen = false" class="fixed inset-0 h-full w-full z-10"></div>

        <div x-show="dropdownOpen" class="absolute right-0 mt-2 bg-white rounded-md shadow-lg overflow-hidden z-20" style="width:20rem;">
            <div class="py-2">
                <a href="#" class="flex items-center px-4 py-3 border-b hover:bg-gray-100 -mx-2">
                    <img class="h-8 w-8 rounded-full object-cover mx-1" src="https://images.unsplash.com/photo-1494790108377-be9c29b29330?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80" alt="avatar">
                    <p class="text-gray-600 text-sm mx-2">
                        <span class="font-bold" href="#">Sara Salah</span> replied on the <span class="font-bold text-blue-500" href="#">Upload Image</span> artical . 2m
                    </p>
                </a>
                <a href="#" class="flex items-center px-4 py-3 border-b hover:bg-gray-100 -mx-2">
                    <img class="h-8 w-8 rounded-full object-cover mx-1" src="https://images.unsplash.com/photo-1531427186611-ecfd6d936c79?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80" alt="avatar">
                    <p class="text-gray-600 text-sm mx-2">
                        <span class="font-bold" href="#">Slick Net</span> start following you . 45m
                    </p>
                </a>
                <a href="#" class="flex items-center px-4 py-3 border-b hover:bg-gray-100 -mx-2">
                    <img class="h-8 w-8 rounded-full object-cover mx-1" src="https://images.unsplash.com/photo-1450297350677-623de575f31c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80" alt="avatar">
                    <p class="text-gray-600 text-sm mx-2">
                        <span class="font-bold" href="#">Jane Doe</span> Like Your reply on <span class="font-bold text-blue-500" href="#">Test with TDD</span> artical . 1h
                    </p>
                </a>
                <a href="#" class="flex items-center px-4 py-3 hover:bg-gray-100 -mx-2">
                    <img class="h-8 w-8 rounded-full object-cover mx-1" src="https://images.unsplash.com/photo-1580489944761-15a19d654956?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=398&q=80" alt="avatar">
                    <p class="text-gray-600 text-sm mx-2">
                        <span class="font-bold" href="#">Abigail Bennett</span> start following you . 3h
                    </p>
                </a>
            </div>
            <a href="#" class="block bg-gray-800 text-white text-center font-bold py-2">See all notifications</a>
        </div>
    </div>
</div>

How to build a Notifications dropdown with Tailwind CSS?

Install tailwind css of verion 1.7.0

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

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

All the unility class needed to build a Notifications dropdown component

  • flex
  • h-screen
  • relative
  • my-32
  • z-10
  • block
  • bg-white
  • p-2
  • h-5
  • w-5
  • text-gray-800
  • fixed
  • h-full
  • w-full
  • absolute
  • right-0
  • mt-2
  • overflow-hidden
  • z-20
  • py-2
  • px-4
  • py-3
  • border-b
  • hover:bg-gray-100
  • -mx-2
  • h-8
  • w-8
  • mx-1
  • text-gray-600
  • text-sm
  • mx-2
  • text-blue-500
  • bg-gray-800
  • text-white
  • text-center

35 steps to build a Notifications dropdown component with Tailwind CSS

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

  2. Use h-screen to make an element span the entire height of the viewport.

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

  4. Control the vertical margin of an element to 8rem using the my-32 utilities.

  5. Control the stack order (or three-dimensional positioning) of an element to 10 in Tailwind, regardless of order it has been displayed, using the z-10 utilities.

  6. Use inline utilities to put the element on its own line and fill its parent.

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

  8. Control the padding on all sides of an element to 0.5rem using the p-2 utilities.

  9. Use h-5 to set an element to a fixed height(1.25rem).

  10. Use w-5 to set an element to a fixed width(1.25rem).

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

  12. Use fixed to position an element relative to the browser window.

  13. Use h-full to set an element’s height to 100% of its parent, as long as the parent has a defined height.

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

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

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

  17. Control the margin on top side of an element to 0.5rem using the mt-2 utilities.

  18. Use overflow-hidden to clip any content within an element that overflows the bounds of that element.

  19. Control the stack order (or three-dimensional positioning) of an element to 20 in Tailwind, regardless of order it has been displayed, using the z-20 utilities.

  20. Control the vertical padding of an element to 0.5rem using the py-2 utilities.

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

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

  23. Control the border color of an element to b using the border-b utilities.

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

  25. Control the horizontal margin of an element to -0.5rem using the -mx-2 utilities.

  26. Use h-8 to set an element to a fixed height(2rem).

  27. Use w-8 to set an element to a fixed width(2rem).

  28. Control the horizontal margin of an element to 0.25rem using the mx-1 utilities.

  29. Control the text color of an element to gray-600 using the text-gray-600 utilities.

  30. Control the text color of an element to sm using the text-sm utilities.

  31. Control the horizontal margin of an element to 0.5rem using the mx-2 utilities.

  32. Control the text color of an element to blue-500 using the text-blue-500 utilities.

  33. Control the background color of an element to gray-800 using the bg-gray-800 utilities.

  34. Control the text color of an element to white using the text-white utilities.

  35. Control the text color of an element to center using the text-center utilities.

Conclusion

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