Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 rotation when hover a font awesome icon?

Tags:

font-awesome

What is the right css to make a css3 rotation when hover a font awesome icon.

I have this code to show a Fontawesome icon

<span class="fa-stack fa-5x">
  <i class="fa fa-circle fa-stack-2x">::before</i>
  <i class="fa fa-flag fa-stack-1x fa-inverse">::before</i>
</span>

Then I have this css to animate the icon

.fa-stack:hover{
color: red;
transition: 0.9s;
transform: rotateY(180deg);
}

The problem is that the animation is not done in a smothly movement and it returns to normal state even if the pointer is inside the icon. It seems to me that the problem are the <i> elements inside .fa-stack

I don't know how to show this in Fiddle.

You can view it in JS Bin: http://jsbin.com/wohubuwaliho/1/

like image 459
JPashs Avatar asked Sep 11 '14 11:09

JPashs


People also ask

Can you rotate a font awesome icon?

To arbitrarily rotate and flip icons, use the fa-rotate-* and fa-flip-* classes when you reference an icon.

Can we use an awesome font icon with CSS?

To use font awesome icons as CSS content code follow the below steps. Add a unique CSS class name to the icon element you want to use. Set the font-weight css property as 900 (For Solid), 400 (Regular or Brands), 300 (Light for pro icons). Set the content css property to the unicode value font awesome icon.


1 Answers

This behavior happens because you hover the element that is animated. When the rotation is happening, the element which your mouse is hover change it's size. Consequence: for a short time the cursor is no more in the hover position. (to see by yourself, set a colored border to the icon and hover it). This follow to a reset of the event if the mouse doesn't stay perfectly immobile...

To avoid this, set your icon in a container and do the animation when the container is hovered.

HTML/CSS

.wrapper:hover .fa-stack{
    color: red;
    transition: 0.9s;
    transform: rotateY(180deg);
}
.wrapper{
    display: inline-block;
    width: 200px;
    border: 1px solid red;
    overflow: hidden;
    text-align: center;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="wrapper">
<span class="fa-stack fa-5x">
  <i class="fa fa-circle fa-stack-2x"></i>
  <i class="fa fa-flag fa-stack-1x fa-inverse"></i>
</span></div>
like image 58
ylerjen Avatar answered Oct 21 '22 18:10

ylerjen