Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Rotate :before pseudo element when parent is hovered

I have added font icons to a list like this:

a:before {
    font-family: FontAwesome;
    content: "\f015";
    .. etc..
}

When the parent anchor tag is hovered, I want this :before pseudo to be rotated. I tried this, but it is not working:

a:hover:before {
            -webkit-transform:rotate(360deg);
            -moz-transform:rotate(360deg);
            -o-transform:rotate(360deg);
}

Doesnt this work? What can I do to get the rotate effect on the :before element only when the parent is hovered?

like image 583
Neel Avatar asked Jul 03 '14 12:07

Neel


People also ask

Can you hover a pseudo element?

To clarify, you CAN NOT give :hover to a pseudo element. There's no such thing as ::after:hover in CSS as of 2018.

How do you use hover before and?

The :before and :after selectors in CSS is used to add content before and after an element. The :hover is pseudo-class and :before & :after are pseudo-elements. In CSS, pseudo-elements are written after pseudo-class.


1 Answers

Try this out:

a:before {
    -webkit-transition: all 300ms 0s ease-in-out;
    transition: all 300ms 0s ease-in-out;

    content: "\f015";
    display: inline-block; //as @Rohit Azad suggested
    font-family: FontAwesome;

    // .. etc ..
}

a:hover:before {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
}

Have a look at this fiddle.

like image 136
Nicolae Olariu Avatar answered Nov 15 '22 10:11

Nicolae Olariu