Published on

6 Tips To Make A Sticky Navbar Component With Tailwind CSS

Sticky Navbar Component

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 Sticky Navbar Component ui component

Also see on codepen.io/egoistdeveloper/pen/mdpeobr

Why use Tailwind CSS to create a Sticky Navbar Component ui component?

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

The preview of Sticky Navbar Component ui component

Free download of the Sticky Navbar Component's source code

The source code of Sticky Navbar Component ui component

<!-- AlpineJS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/alpinejs/3.8.1/cdn.min.js" defer></script>

<!-- Slider Component Container -->
<div class="flex flex-col items-center justify-center mt-32" x-cloak x-data="appData()" x-init="appInit()">
    <div class="flex flex-col">
        <!-- Page Scroll Progress -->
        <div class="fixed inset-x-0 top-0 z-50 h-0.5 mt-0.5
            bg-blue-500" :style="`width: ${percent}%`"></div>

        <!-- Navbar -->
        <nav class="flex justify-around py-4 bg-white/80
            backdrop-blur-md shadow-md w-full
            fixed top-0 left-0 right-0 z-10">

            <!-- Logo Container -->
            <div class="flex items-center">
                <!-- Logo -->
                <a class="cursor-pointer">
                    <h3 class="text-2xl font-medium text-blue-500">
                        <img class="h-10 object-cover"
                            src="https://stackoverflow.design/assets/img/logos/so/logo-stackoverflow.svg" alt="Store Logo">
                    </h3>
                </a>
            </div>

            <!-- Links Section -->
            <div class="items-center hidden space-x-8 lg:flex">
                <a class="flex text-gray-600 hover:text-blue-500
                    cursor-pointer transition-colors duration-300">
                    Home
                </a>

                <a class="flex text-gray-600 
                    cursor-pointer transition-colors duration-300
                    font-semibold text-blue-600">
                    Themes
                </a>

                <a class="flex text-gray-600 hover:text-blue-500
                    cursor-pointer transition-colors duration-300">
                    Developers
                </a>

                <a class="flex text-gray-600 hover:text-blue-500
                    cursor-pointer transition-colors duration-300">
                    Pricing
                </a>

                <a class="flex text-gray-600 hover:text-blue-500
                    cursor-pointer transition-colors duration-300">
                    Blog
                </a>

                <a class="flex text-gray-600 hover:text-blue-500
                    cursor-pointer transition-colors duration-300">
                    About Us
                </a>
            </div>

            <!-- Icon Menu Section -->
            <div class="flex items-center space-x-5">
                <!-- Register -->
                <a class="flex text-gray-600 hover:text-blue-500
                    cursor-pointer transition-colors duration-300">

                    <svg class="fill-current h-5 w-5 mr-2 mt-0.5" xmlns="http://www.w3.org/2000/svg"
                        xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="24" height="24"
                        viewBox="0 0 24 24">
                        <path
                            d="M12 0L11.34 .03L15.15 3.84L16.5 2.5C19.75 4.07 22.09 7.24 22.45 11H23.95C23.44 4.84 18.29 0 12 0M12 4C10.07 4 8.5 5.57 8.5 7.5C8.5 9.43 10.07 11 12 11C13.93 11 15.5 9.43 15.5 7.5C15.5 5.57 13.93 4 12 4M12 6C12.83 6 13.5 6.67 13.5 7.5C13.5 8.33 12.83 9 12 9C11.17 9 10.5 8.33 10.5 7.5C10.5 6.67 11.17 6 12 6M.05 13C.56 19.16 5.71 24 12 24L12.66 23.97L8.85 20.16L7.5 21.5C4.25 19.94 1.91 16.76 1.55 13H.05M12 13C8.13 13 5 14.57 5 16.5V18H19V16.5C19 14.57 15.87 13 12 13M12 15C14.11 15 15.61 15.53 16.39 16H7.61C8.39 15.53 9.89 15 12 15Z" />
                    </svg>

                    Register
                </a>

                <!-- Login -->
                <a class="flex text-gray-600 
                    cursor-pointer transition-colors duration-300
                    font-semibold text-blue-600">

                    <svg class="fill-current h-5 w-5 mr-2 mt-0.5" xmlns="http://www.w3.org/2000/svg"
                        xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="24" height="24"
                        viewBox="0 0 24 24">
                        <path
                            d="M10,17V14H3V10H10V7L15,12L10,17M10,2H19A2,2 0 0,1 21,4V20A2,2 0 0,1 19,22H10A2,2 0 0,1 8,20V18H10V20H19V4H10V6H8V4A2,2 0 0,1 10,2Z" />
                    </svg>

                    Login
                </a>
            </div>
        </nav>
    </div>

    <!-- Dummy Scrollable Content -->
    <div class="flex flex-wrap" style="width: 1000px;">
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
        <div class="bg-orange-200 h-52 w-52 m-5"></div>
    </div>

    <!-- Notes -->
    <span class="text-center font-bold my-20">
        MDI (npm i @mdi/font) reuqired for icons

        <hr class="my-4">

        <a href="https://egoistdeveloper.github.io/twcss-to-sass-playground/" target="_blank" class="text-blue-600">
            Convetert to SASS
        </a>
    </span>
</div>
<script>
    const appData = () => {
        return {
            percent: 0,

            appInit() {
                // source: https://codepen.io/A_kamel/pen/qBmmGKJ
                window.addEventListener('scroll', () => {
                    let winScroll = document.body.scrollTop || document.documentElement.scrollTop,
                        height = document.documentElement.scrollHeight - document.documentElement.clientHeight;

                    this.percent = Math.round((winScroll / height) * 100);
                });
            },
        };
    };
</script>

How to create a Sticky Navbar Component with Tailwind CSS?

Install tailwind css of verion 3.0.18

Use the script html tag to import the script of Tailwind CSS of the version 3.0.18

<script src="https://cdn.tailwindcss.com"></script>

All the unility class needed to create a Sticky Navbar Component component

  • flex
  • flex-col
  • mt-32
  • fixed
  • top-0
  • z-50
  • h-0.5
  • bg-blue-500
  • py-4
  • left-0
  • right-0
  • z-10
  • text-2xl
  • text-blue-500
  • h-10
  • hidden
  • lg:flex
  • text-gray-600
  • text-blue-600
  • h-5
  • w-5
  • mr-2
  • mt-0.5
  • flex-wrap
  • bg-orange-200
  • h-52
  • w-52
  • m-5
  • text-center
  • my-20
  • my-4

31 steps to create a Sticky Navbar Component component with Tailwind CSS

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

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

  3. Control the margin on top side of an element to 8rem using the mt-32 utilities.

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

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

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

  7. Use h-0.5 to set an element to a fixed height(0.5).

  8. Control the background color of an element to blue-500 using the bg-blue-500 utilities.

  9. Control the vertical padding of an element to 1rem using the py-4 utilities.

  10. Use the left-0 utilities to set the left position of a positioned element to 0rem.

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

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

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

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

  15. Use h-10 to set an element to a fixed height(2.5rem).

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

  17. Use flex to create a block-level flex container at only large screen sizes.

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

  19. Control the text color of an element to blue-600 using the text-blue-600 utilities.

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

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

  22. Control the margin on right side of an element to 0.5rem using the mr-2 utilities.

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

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

  25. Control the background color of an element to orange-200 using the bg-orange-200 utilities.

  26. Use h-52 to set an element to a fixed height(13rem).

  27. Use w-52 to set an element to a fixed width(13rem).

  28. Control the margin on all sides of an element to 1.25rem using the m-5 utilities.

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

  30. Control the vertical margin of an element to 5rem using the my-20 utilities.

  31. Control the vertical margin of an element to 1rem using the my-4 utilities.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to create a Sticky Navbar Component components, learn and follow along to implement your own components.