VOOZH about

URL: https://www.geeksforgeeks.org/css/tailwind-css-fixed-navbar/

⇱ Tailwind CSS Fixed NavBar - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Tailwind CSS Fixed NavBar

Last Updated : 23 Jul, 2025

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.

Output:

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.

Output:

Comment