Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a child or another depending on parent hover state (CSS Only)

I'm building a Menu component in react that shows a list of options. Each option has a text on the left and the '>' icon on the right. But, when I hover on the parent I want the '>' symbol to change to '>>'. How can I do this? I have added the current state of the source. I just need to hide BsChevronRight on parent hover and show BsChevronDoubleRight and viceversa.

function Menu({data}:{data:IMenuItemData[]}) {
    return (
        <div className="w-2/12 flex flex-col absolute top-2/4">
            {data.map(el => {
                return <a href="#" className="flex row justify-between items-center group">
                        {el.text}
                        <BsChevronRight></BsChevronRight>
                        <BsChevronDoubleRight></BsChevronDoubleRight>
                        </a>
                    
            })}
        </div>    
        
    )
}

I'm looking for a CSS only solution.

like image 615
Notbad Avatar asked Oct 31 '25 21:10

Notbad


2 Answers

There's a quick way to do this in Tailwind:

1.) Add a "group" class to the parent e.g.

<div class="group i-am-parent">

2.) Add the hover effect on the child, prefixing with group e.g.

<div class="i-am-child group-hover:scale-110">

More info in the tailwind docs: https://tailwindcss.com/docs/hover-focus-and-other-states

like image 84
jacobedawson Avatar answered Nov 03 '25 11:11

jacobedawson


Add hidden group-hover:block classes for your second chevron (that should appear) or any fadeIn effect like: invisible group-hover:visible or opacity-0 group-hover:opacity-100. Your parent a element should have group class to work.

Note: if you're using TailwindCSS without jit mode enabled you need to extend some properties to support group-hover variant

// tailwind.config.js
module.exports = {
  variants: {
      extend: {
        display: ['group-hover'],
        visibility: ['group-hover'],
      }
   }
}

DEMO: https://play.tailwindcss.com/XifyCwLkFV

like image 26
Ihar Aliakseyenka Avatar answered Nov 03 '25 10:11

Ihar Aliakseyenka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!