Open In App

Tailwind CSS Fixed NavBar

Last Updated : 04 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we’ll see how to fix a navbar in Tailwind CSS. A navigation bar (navbar) is a user interface element used for website menu navigation and links, we are using fixed class and sticky class to fix the nav bar at the top of the page.

We will explore two approaches to creating a fixed navbar using Tailwind CSS, along with complete HTML examples for each approach.

  • Using a fixed class
  • Using a sticky class

Approach 1: Using a ‘fixed’ class

In this approach, we will use the ‘fixed’ class to fix the navbar. When an element has a fixed position, it stays in the same position even if the user scrolls the page. Fixed elements do not move when scrolling, effectively creating a fixed element that remains visible regardless of the scroll position.

Tailwind CSS Used Classes

  • fixed: This class is used to create an element with a fixed position. It is positioned relative to the viewport, meaning it stays in the same position even if the user scrolls the page.
  • top-*: This class is used to set the distance of an element from the top edge of its containing element or the viewport, depending on its positioning context.
  • left-*: This class is used to set the distance of an element from the left edge of its containing element or the viewport, depending on its positioning context.

 

Syntax:

<nav class="fixed w-full top-0 left-0">
. . .
</nav>

Example: Below example demonstrates the fixing of the navbar in tailwind CSS.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0" />
    <script src=
        "https://cdn.tailwindcss.com">
    </script>
    <title>Tailwind CSS Fixed Navbar</title>
</head>
  
<body>
    <nav class="bg-green-800 
                py-4 text-white fixed 
                w-full top-0 left-0">
        <div class="container mx-auto">
            <ul class="ml-8 space-x-4">
                <li class="inline-block">
                    <a href="/" 
                       class="hover:text-gray-300">
                        Home
                    </a>
                </li>
                <li class="inline-block">
                    <a href=
                       class="hover:text-gray-300">
                        Practice
                    </a>
                </li>
                <li class="inline-block">
                    <a href=
                        class="hover:text-gray-300">
                        Tutorials
                    </a>
                </li>
                <li class="inline-block">
                    <a href=
                      class="hover:text-gray-300">
                        Contact
                    </a>
                </li>
            </ul>
        </div>
    </nav>
  
    <div class="mt-28 text-center">
        <h1 class="text-5xl font-bold">
            GeeksforGeeks
        </h1>
        <h2 class="text-4xl text-green-600 
                   mt-4 mb-4">
            Fixed Navbar Tailwind CSS
        </h2>
        <div>
            <p class="text-2xl">
                GeeksforGeeks is a widely recognized and
                popular online learning platform that
                focuses on providing quality programming
                tutorials and coding challenges. It is one
                of the largest and most comprehensive
                computer science portals globally,
                catering to students, software
                developers, and tech enthusiasts alike.
                <br />
            </p>
            <div class="text-3xl 
                        font-bold py-5">
                Key features of GeeksforGeeks include:
            </div>
            <br />
            <p class="text-2xl">
                Coding Practice: GeeksforGeeks offers an
                extensive collection of coding challenges
                and practice problems across various
                programming languages, data structures,
                algorithms, and other computer science
                topics. These practice exercises help
                individuals improve their
                problem-solving and coding skills.
                <br /><br />
                Technical Articles and Tutorials:
                GeeksforGeeks provides a vast repository
                of technical articles and tutorials on
                a wide range of topics, including
                programming languages
                (such as C++, Java, Python), algorithms,
                data structures, web development, machine
                learning, and more. These articles are
                written in a concise and easy-to-understand
                manner, making complex concepts
                accessible to learners of all levels.
            </p>
        </div>
    </div>
</body>
  
</html>


Output:

ezgifcom-crop-(50)

Approach 2: Using the ‘sticky’ Class

In this approach, we will use the ‘sticky’ class to fix the navbar. When an element has a sticky position, it behaves as ‘relative’ until it reaches a specific scroll position, after which it becomes ‘fixed’. It then remains fixed until the user scrolls past a specified element or position.

Tailwind CSS Used Classes

  • sticky: This class is used to create an element with a “sticky” position. When an element has a sticky position, it behaves as relative until it reaches a specific scroll position, after which it becomes fixed.

Syntax:

<nav class="sticky top-0">
. . .
</nav>

Example: Below example demonstrates the fixing of the navbar using the sticky class in tailwind CSS.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0" />
    <script src=
        "https://cdn.tailwindcss.com">
    </script>
    <title>Tailwind CSS Fixed Navbar</title>
</head>
  
<body>
    <nav class="bg-green-600 
                sticky top-0 z-50 
                p-4 text-white">
        <div class="container 
                    mx-auto">
            <ul class="ml-8 space-x-4">
                <li class="inline-block">
                    <a href="/" class="hover:text-gray-300">
                        Home
                    </a>
                </li>
                <li class="inline-block">
                    <a href=
                       class="hover:text-gray-300">
                        Practice
                    </a>
                </li>
                <li class="inline-block">
                    <a href=
                        class="hover:text-gray-300">
                        Tutorials
                    </a>
                </li>
                <li class="inline-block">
                    <a href=
                       class="hover:text-gray-300">
                        Contact
                    </a>
                </li>
            </ul>
        </div>
    </nav>
  
    <div class="mt-28 text-center">
        <h1 class="text-5xl font-bold">
            GeeksforGeeks
        </h1>
        <h2 class="text-4xl 
                   text-green-600 
                   mt-4 mb-4">
            sticky Navbar Tailwind CSS</h2>
        <div>
            <p class="text-2xl">
                GeeksforGeeks is a widely recognized
                and popular online learning platform
                that focuses on providing quality
                programming tutorials and coding
                challenges. It is one of the largest
                and most comprehensive computer science
                portals globally, catering to students,
                software developers, and tech
                enthusiasts alike.
                <br />
            </p>
            <div class="text-3xl 
                        font-bold 
                        py-5">
                Key features of GeeksforGeeks include:
            </div>
            <br />
            <p class="text-2xl">
                Coding Practice: GeeksforGeeks offers an
                extensive collection of coding challenges
                and practice problems across various
                programming languages, data structures,
                algorithms, and other computer science
                topics. These practice exercises help
                individuals improve their
                problem-solving and coding skills.
                <br /><br />
                Technical Articles and Tutorials:
                GeeksforGeeks provides a vast repository
                of technical articles and tutorials on a
                wide range of topics, including
                programming languages
                (such as C++, Java, Python),
                algorithms, data structures, web
                development, machine learning, and more.
                These articles are written in a concise
                and easy-to-understand manner, making
                complex concepts accessible to learners
                of all levels.
            </p>
        </div>
    </div>
</body>
  
</html>


Output:

ezgifcom-crop-(51)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads