How to usea ‘fixed’ class in CSS
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" > w3wiki </ h1 > < h2 class="text-4xl text-green-600 mt-4 mb-4"> Fixed 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