How to change the width on hover using Tailwind CSS ?
Last Updated :
24 Jul, 2024
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 modify width on hover, you need to modify tailwind.config.js file. The below step is to add the tailwind.config.js file in your project folder in order to work on hover to change the width.
First, you have to install the Tailwind CSS. Given below are the steps to install tailwind CSS.
Prerequisite: Follow the below step to add your own utility class to the tailwind.
Step 1: Run the below code to your folder's terminal. This will create package.json file.
npm init

Step 2: Copy and paste the below code to your folder's terminal. This will create the required node module for tailwind.
npm install tailwindcss@latest postcss@latest autoprefixer@latest

Step 3: Create a public folder and add index.html, style.css, and tailwind.css inside the public folder.

Step 4: Add the below code in the tailwind.css file. Using this file, you can customize your tailwind CSS along with the default style. Tailwind will swap these directives out at build-time with all the styles. It generates based on your configured design system.
@tailwind base;
@tailwind components;
@tailwind utilities;
Step 5: open package.json file and under script tag add below code
"scripts": {
"build:css": "tailwind build public/tailwind.css -o public/style.css"
},

Step 6: Run the below code in the terminal. This will populate your style.css file with predefined Tailwind CSS code.
npm run build:css
Step 7: Finally, run the below code. This will Generate a Tailwind config file for your project using the Tailwind CLI utility included when you install the tailwindcss npm package:
npx tailwindcss init

Syntax:
variants: {
width: ["responsive", "hover", "focus"]
}
tailwind.config.js: The following code is the content for the tailwind config file. We simply want to extend the config to add new values.
JavaScript
module.exports = {
purge: [],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
width: ["responsive", "hover", "focus"]
},
plugins: [],
}
Example 1:
HTML
<!DOCTYPE html>
<html class="dark">
<head>
<link href=
"https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css"
rel="stylesheet">
</head>
<body>
<div class=" w-1/3 hover:w-4/5 bg-green-200 ">
<div>HOVER HERE</div>
</div>
</body>
</html>
Output:
Example 2: Again on hover, for changing both height and width, you have to add or modify the below code on tailwind.config.js
variants: {
width: ["responsive", "hover", "focus"],
height: ["responsive", "hover", "focus"]
},
HTML
<!DOCTYPE html>
<html class="dark">
<head>
<link href=
"https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css"
rel="stylesheet">
</head>
<body>
<div class=" w-1/3 hover:w-4/5
hover:h-20 bg-green-200 text-center">
<div>HOVER HERE</div>
</div>
</body>
</html>
Output:
Similar Reads
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 Stroke Width of an SVG Element in Tailwind CSS? Stroke Width of an SVG Element is a key property that defines the thickness of the outline for shapes like circles, rectangles, and paths. In Tailwind CSS, adjusting the stroke width is simple and can be done using utility classes. Tailwind provides a range of built-in classes, along with the flexib
3 min read
How to Modify Hover Effect using Tailwind CSS ? In Tailwind CSS, the term "modify hover" refers to changing the styles that are applied to an element when it is hovered over. With Tailwind CSS "hover" variation, you can quickly apply particular utility classes and custom classes to control how components appear when you hover over them, giving yo
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 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
How to use Tailwind CSS with esbuild ? Tailwind CSS is 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. An esbuild is a bundler for the web project that brings the build tool for performance enhancement, along with fa
2 min read
Create a Portfolio Website Template using Tailwind CSS In this tutorial, we'll guide you through the process of creating a stunning portfolio website using Tailwind CSS, a powerful CSS framework that makes styling web pages easy and efficient. Preview of Final Output: Let us have a look at how the final output will look like. Personal PortfolioPrerequis
6 min read
How to Set Width Transition in Tailwind CSS ? In Tailwind CSS, setting a width transition allows for smooth animation when an element's width changes. It enhances the user experience by gradually transitioning the width over a specified duration, creating a polished effect. Tailwind's utility classes make implementing transitions simple.Approac
2 min read
How to Creating Horizontal Rule Divider with Text in Tailwind CSS ? In this article, we will understand how to create a horizontal rule (HR) divider that contains text with Tailwind CSS. The problem here is that in an HR, there is no text, & only a divider is visible, and there is no default method to add text in Tailwind CSS. Tailwind CSS is a highly customizab
3 min read
How to use CSS Animations with Tailwind CSS ? Tailwind CSS classes are used to style elements and apply animations effortlessly. Utilize Tailwind's animation utility classes to add dynamic visual effects. Combine Tailwind CSS with custom CSS animations for versatile and engaging web designs. Table of Content Tailwind CSS Animation Utility Class
3 min read