Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Tailwind CSS Tutorial
  • Tailwind Interview Questions
  • Tailwind Layout
  • Tailwind Flexbox
  • Tailwind Grid
  • Tailwind Alignment
  • Tailwind Spacing
  • Tailwind Sizing
  • Tailwind Typography
  • Tailwind Backgrounds
  • Tailwind Borders
  • Tailwind Effects
  • Tailwind Filters
  • Tailwind Tables
  • Tailwind Transitions and Animation
  • Tailwind Transforms
  • Tailwind Interactivity
  • CSS Frameworks
  • Web Technology
Open In App
Next Article:
Create Navbars UI using React and Tailwind CSS
Next article icon

How to Create A Sticky NavBar Using Tailwind CSS?

Last Updated : 29 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Sticky Navbar in Tailwind CSS is mostly used in applications where maintaining quick access to navigation links is essential as users scroll through content. It ensures that the navbar remains visible at the top of the screen, enhancing user experience by providing constant access to key sections of the website.

This feature is particularly useful for long, content-heavy pages, making it easier for users to navigate without needing to scroll back to the top.

Prerequisites

  • HTML
  • CSS

Approach

  • Set up the basic HTML structure with <!DOCTYPE html>, head, and body elements.
  • Include necessary meta tags and the Tailwind CSS CDN in the head section using <script src="https://cdn.tailwindcss.com"></script>.
  • Create a <nav> element for the navbar and use fixed, top-0, and w-full classes to make it sticky and span the full width.
  • Apply background color to the navbar with bg-black and add padding using p-4 for spacing.
  • Add a container inside the navbar using container mx-auto flex justify-between items-center to manage layout and alignment.
  • Include the logo or brand name with text classes like text-white text-2xl font-bold to style it appropriately.
  • Create a <ul> element with list items representing the navigation links, applying classes like space-x-6, hover:text-yellow-300 to manage spacing and hover effects.
  • Define content sections for the page, using distinct background colors and layout classes to differentiate between sections and test the sticky behavior of the navbar as you scroll.

Example: The below example demonstrates a sticky navbar using TailwindCSS with attractive styling.

HTML
<!DOCTYPE html>  <head>     <title>Sticky Navbar</title>     <script src="https://cdn.tailwindcss.com"></script> </head>  <body class="bg-gray-100">      <nav class="bg-black p-4 fixed w-full z-50 top-0 shadow-lg">         <div class="container mx-auto flex justify-between items-center">             <div class="text-white text-2xl font-bold">                 GeeksforGeeks             </div>             <ul class="flex space-x-6 text-white">                 <li><a href="#home" class="hover:text-yellow-300">Home</a></li>                 <li><a href="#courses" class="hover:text-yellow-300">Courses</a></li>                 <li><a href="#articles" class="hover:text-yellow-300">Articles</a></li>                 <li><a href="#about" class="hover:text-yellow-300">About Us</a></li>                 <li><a href="#contact" class="hover:text-yellow-300">Contact</a></li>             </ul>         </div>     </nav>     <section id="home" class=      "h-screen flex items-center justify-center bg-green-500 text-white text-4xl font-bold">         <div class="text-center">             <h1 class="text-5xl mb-4">Welcome to GeeksforGeeks</h1>             <p class="text-xl">Your one-stop destination for programming and tech-related content!</p>         </div>     </section>     <section id="courses" class="py-20 px-4 bg-yellow-300 text-green-800">         <div class="container mx-auto">             <h2 class="text-4xl font-bold text-center mb-10">Our Courses</h2>             <div class="grid grid-cols-1 md:grid-cols-3 gap-8">                 <div class="bg-white p-6 rounded-lg shadow-lg">                     <h3 class="text-2xl font-bold mb-4">Data Structures</h3>                     <p>Learn the fundamentals of data structures and algorithms to ace your coding                       interviews.</p>                 </div>                 <div class="bg-white p-6 rounded-lg shadow-lg">                     <h3 class="text-2xl font-bold mb-4">Web Development</h3>                     <p>Master front-end and back-end technologies to become a                        full-stack web developer.</p>                 </div>                 <div class="bg-white p-6 rounded-lg shadow-lg">                     <h3 class="text-2xl font-bold mb-4">Machine Learning</h3>                     <p>Dive into the world of AI and machine learning with our                        comprehensive course.</p>                 </div>             </div>         </div>     </section>      <section id="articles" class="py-20 px-4 bg-blue-500 text-white">         <div class="container mx-auto">             <h2 class="text-4xl font-bold text-center mb-10">Articles & Tutorials</h2>             <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">                 <div class="bg-white p-6 rounded-lg shadow-lg text-green-800">                     <h3 class="text-2xl font-bold mb-4">Understanding Recursion</h3>                     <p>A deep dive into the concept of recursion with examples and                        practice problems.</p>                 </div>                 <div class="bg-white p-6 rounded-lg shadow-lg text-green-800">                     <h3 class="text-2xl font-bold mb-4">CSS Grid vs Flexbox</h3>                     <p>An in-depth comparison between CSS Grid and Flexbox for modern web layouts.</p>                 </div>                 <div class="bg-white p-6 rounded-lg shadow-lg text-green-800">                     <h3 class="text-2xl font-bold mb-4">Dynamic Programming</h3>                    <p>Learn the techniques of dynamic programming to solve complex                       problems efficiently.</p>                 </div>                 <div class="bg-white p-6 rounded-lg shadow-lg text-green-800">                     <h3 class="text-2xl font-bold mb-4">JavaScript ES6 Features</h3>                     <p>Explore the new features introduced in ECMAScript 6 and how to use them                        effectively.</p>                 </div>                 <div class="bg-white p-6 rounded-lg shadow-lg text-green-800">                     <h3 class="text-2xl font-bold mb-4">Introduction to Git</h3>                     <p>A beginner's guide to Git and GitHub for version control and collaboration.</p>                 </div>                 <div class="bg-white p-6 rounded-lg shadow-lg text-green-800">                     <h3 class="text-2xl font-bold mb-4">Python for Data Science</h3>                     <p>Leverage Python's powerful libraries for data analysis and visualization.</p>                 </div>             </div>         </div>     </section>      <section id="about" class="py-20 px-4 bg-red-500 text-white">         <div class="container mx-auto text-center">             <h2 class="text-4xl font-bold mb-10">About Us</h2>             <p class="text-xl leading-relaxed">GeeksforGeeks is a leading platform for learning and               sharing knowledge in                 the field of computer science. We provide a wide range of resources, including               tutorials, coding                 challenges, and courses, to help you excel in your tech career.</p>         </div>     </section>     <section id="contact" class="py-20 px-4 bg-purple-500 text-white">         <div class="container mx-auto">             <h2 class="text-4xl font-bold text-center mb-10">Contact Us</h2>             <form class="max-w-lg mx-auto">                 <div class="mb-4">                     <label for="name" class="block text-xl mb-2">Name</label>                     <input type="text" id="name"      				class="w-full p-3 rounded-lg border border-gray-300 focus:outline-none focus:ring-2                            focus:ring-yellow-300"                         placeholder="Your Name">                 </div>                 <div class="mb-4">                     <label for="email" class="block text-xl mb-2">Email</label>                     <input type="email" id="email"                         class="w-full p-3 rounded-lg border border-gray-300 focus:outline-none                                 focus:ring-2 focus:ring-yellow-300"                         placeholder="Your Email">                 </div>                 <div class="mb-4">                     <label for="message" class="block text-xl mb-2">Message</label>                     <textarea id="message"                         class="w-full p-3 rounded-lg border border-gray-300 focus:outline-none                                 focus:ring-2 focus:ring-yellow-300"                         rows="5" placeholder="Your Message"></textarea>                 </div>                 <button type="submit"                     class="w-full p-3 bg-yellow-300 text-green-800 rounded-lg font-bold                             hover:bg-yellow-400">Send                     Message</button>             </form>         </div>     </section>  </body>  </html> 

Output:


Next Article
Create Navbars UI using React and Tailwind CSS

A

anjalibo6rb0
Improve
Article Tags :
  • Web Technologies
  • Tailwind CSS

Similar Reads

  • Create Navbars UI using React and Tailwind CSS
    A UI plays an important role because a clean, well-designed interface creates a positive first impression and if the UI is good then users can stay on our website some more time and if the UI is bad then they can not stay on our site for more time. we will see how to Create Navbars UI using React an
    5 min read
  • How to create a Chevron using Tailwind CSS ?
    A Chevron is a basic geometric shape that is used in websites to indicate directional movements or navigation. By utilizing specific border properties and transformations, a chevron shape can be achieved without the need for custom CSS. Tailwind's utility-first approach allows for a straightforward
    3 min read
  • How to Change Style of Scrollbar using Tailwind CSS?
    By default, Tailwind CSS does not include built-in utilities for styling scrollbars. However, you can customize the appearance of scrollbars using traditional CSS in combination with Tailwind's utility classes. This is achieved by using the scrollbar-* classes to customize aspects like scrollbar wid
    3 min read
  • How to Create a Frosted Navbar with TailwindCSS ?
    A frosted navbar is a type of navigation bar that has a translucent appearance and when the contents of the page are scrolled it appears to blur behind the navbar. It is a popular design element in modern web development. Creating a frosted navbar effect with Tailwind CSS involves utilizing Tailwind
    3 min read
  • How to create fixed/sticky footer on the bottom using Tailwind CSS ?
    In this article, we are going to create a fixed/sticky footer on the bottom using Tailwind CSS. Tailwind CSS is a highly customizable, utility-first CSS framework from which we can use utility classes to build any design. With Tailwind CSS we can create a design by simply adding classes. Installatio
    2 min read
  • How to create a top navigation bar using CSS?
    A top navigation bar is a horizontal bar that typically contains links to different sections of a website. It is a fundamental part of a website's layout and helps users navigate easily. In this article, we will explore different approaches to creating a top navigation bar using CSS. These are the f
    3 min read
  • How to create a split navigation bar using CSS ?
    The article will demonstrate how to create a split navigation bar using CSS. There are two ways to make the split navigation bar these are with the help of flexbox properties and CSS position property. A split navigation bar is a popular layout where navigation links are divided into two sections. I
    4 min read
  • How to Create a Sticky Element using CSS?
    A sticky element is a positioning technique in CSS that allows an element to behave like a relatively positioned element until a specific scroll position is met, after which it behaves like a fixed element. This is particularly useful for creating sticky headers, sidebars, or any other UI elements t
    3 min read
  • Create Header using React and Tailwind CSS
    In modern web development building responsive and customizable user interfaces is crucial. One of the essential elements of any web application is the header which typically contains navigation links branding or other important controls. we will create a responsive header section using React and Tai
    4 min read
  • How to Create Multiple Themes using Tailwind CSS ?
    In this article, we will discuss how to create multiple themes using Tailwind CSS. Tailwind CSS is a popular utility-first CSS framework that provides a lot of pre-designed CSS classes for designing websites. With Tailwind CSS, you can easily create multiple themes for your website. This means that
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences