How to usethe ‘sticky’ Class in CSS
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" > w3wiki </ h1 > < h2 class="text-4xl text-green-600 mt-4 mb-4"> sticky Navbar Tailwind CSS</ h2 > < div > < p class = "text-2xl" > w3wiki 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 w3wiki include: </ div > < br /> < p class = "text-2xl" > Coding Practice: w3wiki 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: w3wiki 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:
Tailwind CSS Fixed NavBar
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