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/[email protected]/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 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 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 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 How to add scale animation on hover using Tailwind CSS in React ? In this article, we'll see how to add scale animation on hover using tailwind CSS in a ReactJS app. The hover effect appears when a user positions the cursor over an element. In tailwind CSS, the scale utility class helps in getting the zoom effect over an element. PrerequisitesReact JSTailwind CSSA 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 Full-Page Tabs using CSS & JavaScript? Tabs are a common UI pattern used to organize content into multiple sections within a single page. In this article, we'll explore how to create full-page tabs using CSS and JavaScript. Full-page tabs allow users to switch between different sections of content while maintaining a clean and intuitive 3 min read How to use hover, focus and active variants in Tailwind CSS ? In this article, we will see how to use hover, focus, and active variants in Tailwind CSS. Tailwind CSS uses the Hover, Focus, and Active variants to style an element when the user mouses move over it, focuses it, or actively clicks/tapped it. These variants allow you to create interactive and dynam 4 min read Like