hover OR focus in Tailwind CSS

Ed.an

So I want to change the color of the text on hover or focus

<div class="hover:text-green-500 focus:text-green-500">foo bar</div>

But I was wondering if is possible to compress all in one statement, so I would not need to repeat the text-green-500 for both. I tried the code below, but it becomes an and statement instead of or.

<div class="hover:focus:text-green-500">foo bar</div>

In pure css, what I'm looking for to do would be something like this:

div:hover, div:focus {
  color: green
}

Is that possible in TailwindCSS?

JavaTechnical

You can @apply to re-use existing tailwind styles so that you don't end-up writing color: green or whatever extra you would otherwise be writing in plain CSS>

https://tailwindcss.com/docs/functions-and-directives#apply

div:hover, div:focus {
  @apply text-green-500;
}

This way, you will also end up having less code in your class attribute.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

CSS: Target an element on hover OR focus, but not both

CSS hover, focus and "unfocus"

Hover , Focus and Blur conflict

Transition duration with tailwind css

CSS: hover, focus or active not working on mobile devices

Circle with text in Tailwind css

Performance of transition CSS on *:link,*:visited,*:hover,*:active,*:focus

Using on hover and out focus

React hover/active/focus style css selector not working for enclosed element

Use Sveltekit and Tailwind CSS

Use :hover, :focus to change another css outside the hovered class

Use Color in TailWind CSS

Tailwind css don't show hover state when input is in focus

css "tooltips" for both mouse hover & keyboard focus

Aligning Content in Tailwind CSS

Tailwind text color not changing on hover

how to darken the image on hover in tailwind

Inherit Hover state in Tailwind

Tailwind underline hover animation

how to make focus state working on div in tailwind-CSS

how to make width change of a div on hover that takes 2 seconds with tailwind css

How to focus a div tag with a tailwind?

Is there a way to chain multiple tailwind css classes on a single hover instance?

Tailwind CSS applying both pseudo class (e.g: hover OR active) in class name

Focus a set of buttons after clicked once in tailwind css

Is there a way to hover over a link in one class to trigger an animation in another using Tailwind CSS?

Breadcrumb and ellipsis with Tailwind CSS

How to add transition effect on group hover with tailwind css?

Why is the hover effect not working[Tailwind CSS]