How to Change Tabs Horizontally on Hover with Tailwind CSS and JavaScript ? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Changing tabs on hover in Tailwind CSS enhances the user experience by offering interactive hover navigation. Users can quickly preview different sections without clicking which reduces the time it takes to explore different sections and increase engagement. We can also add CSS transitions for visual effects. Preview:OutputApproach:The body element contains a flex container (div.flex) at the top of the page with three tabs (div.tab elements) for HTML, Tailwind, and JavaScript.The content for each tab is placed in div.tab-content elements, and initially, all content is hidden (class="tab-content hidden").The script selects all elements with the class .tab and .tab-content. These elements represent the tabs and their corresponding content, respectively.For each tab element, an event listener is added for the mouseenter event. This means when the mouse hovers over a tab, the associated content will be displayed.When a tab is hovered over, the script hides all tab contents by removing the active class from each content element.The script then identifies the ID of the content associated with the hovered tab using the data-tab attribute. It adds the active class to the corresponding content element, making it visible.Example: Illustration of changing Tabs horizontally on Hover with Tailwind CSS and JavaScript. JavaScript // Get all tab elements const tabs = document.querySelectorAll('.tab'); const tabContents = document.querySelectorAll('.tab-content'); // Loop through each tab tabs.forEach((tab) => { tab.addEventListener('mouseenter', () => { // Hide all tab contents tabContents.forEach((content) => { content.classList.remove('active') }) // Show the corresponding tab content const tabId = tab.getAttribute('data-tab') document.getElementById(tabId).classList.add('active') }) }) HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Horizontal Tab Hover with Tailwind CSS and JavaScript </title> <link rel="stylesheet" href="style.css"> <!-- Tailwind CSS CDN link --> <link href= "https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet" /> </head> <body> <div class="flex bg-green-500 py-3 top-0"> <div class="tab cursor-pointer px-8 py-4 transition duration-300 hover:bg-gray-100 transform hover:translate-y-1" data-tab="tab1">HTML</div> <div class="tab cursor-pointer px-8 py-4 transition duration-300 ease-in-out hover:bg-gray-100 transform hover:translate-y-1" data-tab="tab2">Tailwind</div> <div class="tab cursor-pointer px-8 py-4 transition duration-300 ease-in-out hover:bg-gray-100 transform hover:translate-y-1" data-tab="tab3">Javascript</div> </div> <!-- Tab contents --> <div class="mt-4"> <div class="tab-content hidden" id="tab1"> HTML stands for HyperText Markup Language. It creates a complete website structure of web pages. HTML is a combination of Hypertext and Markup language. Hypertext defines the link between the web pages and markup language defines the text document within the tag. This HTML Tutorial provides basic to advanced concepts for beginners and professionals. </div> <div class="tab-content hidden" id="tab2"> Tailwind CSS is basically a Utility first CSS framework for building rapid custom UI. It is a highly customizable, low-level CSS framework that gives you all of the building blocks that you need. Also, it is a cool way to write inline styling and achieve an awesome interface without writing a single line of your own CSS. </div> <div class="tab-content hidden" id="tab3"> JavaScript (JS) is the most popular lightweight, interpreted compiled programming language. It can be used for both Client-side as well as Server-side developments. It is also known as a scripting language for web pages. This free JavaScript Tutorial is designed to help both beginners and experienced professionals master the fundamentals of JavaScript and unleash their creativity to build powerful web applications. From basic syntax and data types to advanced topics such as object-oriented programming and DOM manipulation. </div> </div> <script src="index.js"></script> </body> </html> CSS .tab-content.active { display: block; } .tab:not(:hover) { /* Back to Original background color on mouse remove */ background-color: #10B981; } Output: Output Comment More infoAdvertise with us Next Article How to Change Tabs Horizontally on Hover with Tailwind CSS and JavaScript ? bishalpaul34 Follow Improve Article Tags : CSS Tailwind CSS-Questions Geeks Premier League 2023 Similar Reads How to Create Horizontal and Vertical Tabs using JavaScript ? In this article, we will create Horizontal and Vertical Tabs using JavaScript.Tabs can be used for displaying large amounts of content on a single page in an organized manner. We can design single-page tabs using HTML, CSS, and JavaScript. HTML elements are used to design the structure of the tabs a 7 min read How to Install Tailwind CSS with Create React App? We will see how to set up Tailwind CSS in a React project using the Create React App. Tailwind CSS is a utility-first CSS framework that allows for rapid development of custom user interfaces. When combined with Create React App, it offers a flexible approach to build modern React applications.Prere 2 min read How to change the width on hover using Tailwind CSS ? In this article, we will change the width on hover using Tailwind. There is no inbuilt method in Tailwind, so you have to customize the tailwind.config.js file. Let's discuss the whole process further in this article.By default, tailwind CSS only generates responsive variants for width utilities. To 3 min read How to Change Image on Hover using Tailwind CSS? One common effect is changing an image when the user hovers over it. We use Tailwind CSS, a utility-first CSS framework, to accomplish this without any additional JavaScript logic for the hover effect. By utilizing Tailwind's built-in classes we can create smooth transitions between two images where 2 min read How to Create Event Management Card using JavaScript and Tailwind CSS ? An event management website is a platform designed to help users plan, organize, and promote events. It typically includes features such as event scheduling, ticket sales, attendee registration, and venue management. Building such a website with the Tailwind CSS can result in a sleek and modern user 4 min read How to Access Tailwind Colors from JavaScript ? Tailwind CSS is a "Utility first CSS framework". The feature that makes Tailwind has a variety of classes and provides a huge set of predefined colors that can be used in our stylesheets without the need to define them manually. These predefined colors are categorized into different color palettes w 4 min read How to Change the Direction of a Gradient in Tailwind CSS ? In Tailwind CSS, you can change the direction of a gradient using the bg-gradient-to utility class. For example, bg-gradient-to-r sets the gradient from left to right, while bg-gradient-to-b sets it from top to bottom. Choose the appropriate direction class to achieve the desired gradient orientatio 3 min read How to make Animated Click Effect using Tailwind CSS & JavaScript ? It refers to the technique of implementing animations that respond to user clicks, utilizing the Tailwind CSS for styling and layout, and JavaScript for adding dynamic behavior to the elements. This enhances user engagement and improves the overall user experience of the websites. Table of Content U 2 min read How to Create a Navigation Bar with Icons in Tailwind CSS ? A navigation bar, commonly known as a nav bar, serves as a fundamental component of user interface design, facilitating seamless navigation across a website or application. Using the icons in the navigation bar gains a distinctive advantage in visual communication. Icons streamline navigation by usi 2 min read How to Create Tab Headers using CSS & JavaScript? Tab headers are a commonly used user interface element in web development. They provide a way to organize content into distinct sections, allowing users to switch between them easily. Each tab represents a different category or topic, and clicking on a tab displays the corresponding content while hi 2 min read Like