The CSS rotation effect causes the element to spin and twirl

I've been struggling once again to grasp the concept of CSS animations, particularly the rotate animation. I'm trying to create an effect where an image rotates back and forth on hover, but for some reason it only starts rotating after shifting to the bottom left. Despite my efforts to find a solution online, I haven't come across any answers explaining why this occurs or how to fix it.

img {

  position: absolute;

  top: 50%;

  left: 50%;

  transform: translate(-50%, -50%);

}

img:hover {

  animation-name: freakout;

  animation-duration: .5s;

  animation-iteration-count: infinite;

}

@keyframes freakout {

  0%, 100% {

    -ms-transform: rotate(0deg);

    /* IE 9 */

    -webkit-transform: rotate(0deg);

    /* Chrome, Safari, Opera */

    transform: rotate(0deg);

  }

  50% {

    -ms-transform: rotate(7deg);

    /* IE 9 */

    -webkit-transform: rotate(7deg);

    /* Chrome, Safari, Opera */

    transform: rotate(7deg);

  }

}
<img src="http://www.swimwithamanatee.biz/normal_ian-symbol-trichechus-spp22_left.png" />

Answer №1

One of the reasons behind this issue is that the Transform property in the img tag is using the Translate function to center align the image, but when you hover over the image, the Transform property changes to Rotate, causing a shift in position. I have made some modifications to the code to resolve this problem.

   img {

  position: absolute;

  top: 50%;

  left: 50%;

  transform: translate(-50%, -50%);

}

img:hover {

  animation-name: freakout;

  animation-duration: .5s;

  animation-iteration-count: infinite;

}

@keyframes freakout {

  0%, 100% {

    -ms-transform: rotate(0deg)translate(-50%, -50%);

    /* IE 9 */

    -webkit-transform: rotate(0deg)translate(-50%, -50%);

    /* Chrome, Safari, Opera */

    transform: rotate(0deg)translate(-50%, -50%);

  }

  50% {

    -ms-transform: rotate(7deg)translate(-50%, -50%);

    /* IE 9 */

    -webkit-transform: rotate(7deg)translate(-50%, -50%);

    /* Chrome, Safari, Opera */

    transform: rotate(7deg)translate(-50%, -50%);

  }

}
<img src="http://www.swimwithamanatee.biz/normal_ian-symbol-trichechus-spp22_left.png" />

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to select an HTML tag element using the :v-deep() selector?

When I utilize the following: ::v-deep img { ... } it functions but triggers a deprecation warning: [@vue/compiler-sfc] ::v-deep usage as a combinator has been deprecated. Use :deep(<inner-selector>) instead. How can I achieve the same effect using ...

``Sorry, the link redirection feature is currently experiencing technical

First and foremost, take a look at this example View here Based on the example provided, I would like to create a similar page using the Telrik Text Editor, with a list of links and their corresponding paragraphs. Here is the HTML I am using in the Telr ...

Efficient Ways to Present and Personalize Indian Date and Time using JavaScript

Hey there, currently creating my website and need some assistance. I'm looking to display the date and time in different sections of the page. Having some trouble with getting Indian time to work properly using PHP. If you could help me out with Ja ...

Could someone offer some assistance in grasping the order of operations in jQuery syntax

Until now, my approach has been deciphering and analyzing various codes and techniques for effectively utilizing jQuery, but I often find myself overwhelmed by the choices presented. This often leads to unnecessary complexity in my code. What I aim to ach ...

Having difficulty locating the value in the input search field

I am attempting to customize my search bar so that it automatically includes wildcards on both ends when a user performs a search. Although the code below almost does what I want by adding the wildcards on both sides, it adds them to an empty search term ...

Modifying the HTML content of a span element does not always initialize all of its properties

I've been working on setting up a comments-reply system for my website. I have all the necessary PHP files in place, but I'm facing an issue with updating the vote counts dynamically. In the image provided, you can see that upon voting once, I a ...

Even after setting [attr.disabled]="false" in Angular, the Textarea still remains disabled

I am currently utilizing ngModel for a textarea, and I would like to be able to enable and disable the textarea based on a certain condition. Even though I have bound it correctly and the value is changing to [attr.disabled]="false", the textarea remains d ...

Curious about learning the Xpath?

This is the HTML content <dd id="_offers2" itemprop="offers" itemscope="" itemtype="http://schema.org/Offer" class="wholesale nowrap "> <span itemprop="price" class="Hover Hover Hover">$46.29</span> / each <meta itempr ...

What is causing the extra whitespace on the right side with container-fluid?

Could someone assist me with an issue I'm experiencing when using the container-fluid in Bootstrap? It seems to be leaving some space on the right side of my web page, and as a newcomer to Bootstrap, any help would be greatly appreciated. <link ...

How to dismiss a modal using the second button in Bootstrap 4

When trying to open and close a modal using the normal Bootstrap 4 method, everything seemed to be working fine. But in my specific scenario, I needed a second button to close the modal, and simply duplicating the first button wasn't doing the trick. ...

The vertical scrollbar appears to be malfunctioning

I'm experiencing an issue where the scrollbar is visible but not functioning as expected. I am utilizing CSS for styling the scrollbar and other layouts, along with HTML to apply the styling. #sub_menu, #content{ display: inline-block; } #sub_ ...

What is the approach for capturing system or website sound using JavaScript?

After creating an online drumkit and guitar player, I am now looking to enhance my website by adding a feature that allows users to record their sessions and download them. Since I am new to web development and familiar only with HTML, CSS, JavaScript, and ...

The issue with 'DemoCtrl' defined in Angular JS is that it does not correspond to a valid argument

signup.html <div ng-controller="UniqueCtrl" layout="column" ng-cloak="" class="md-inline-form inputdemoBasicUsage" ng-app="inputBasicDemo"> <md-content md-theme="docs-dark" layout-gt-sm="row" layout-padding=""> <div> <md- ...

JavaScript multiline variable declaration

In the context of a multi-lined variable in AJAX Chat, I am attempting to add an image before other elements are set. However, the issue I am encountering is that the image and text are always displayed on separate lines. It seems to be more of an issue wi ...

Tips for choosing the dropdown selection on an ASPX website page

I have a specific dropdown menu set up, and I need to select the appropriate option from the viewdata object in my .aspx page. Here is what my menu looks like: <td headers="Vehicle" style="background-color:#EFF3FB;font-family: Verdana; font-size: 10px ...

Using ASP.NET MVC to generate dynamic CSS styles

I am seeking a solution that will allow me to achieve the following objectives: Retrieve dynamically generated CSS from an action method Select a CSS file based on request parameters or cookies Utilize a tool for combining and compressing (minifying) CS ...

Implementing a three-column layout with images using Bootstrap

I am currently utilizing a Bootstrap layout that features two columns. However, I am now looking to include a small image to the right of an input field. This image needs to be displayed next to the input field snippet. Here is the HTML structure I am wor ...

I am looking to develop a customizable table where the user can input their desired information

Looking to create an HTML page featuring a 10x10 table with alternating red and green squares. After loading the page, a pop-up window will prompt the user to input a word, which will then appear only in the red squares of the table. While I've succes ...

flash animation on an HTML webpage (fancy pop-up)

Hey, I'm interested in creating a similar effect to this: Check out this example >> Simply click on "Start PicLens Lite Slideshow PicLens". Did you notice how the swf/flash loads on top of the regular page? I want to achieve that :-D If there ...

What is the method for encoding and decoding a query string within an HTML object tag?

Within my code, I am utilizing an object tag that utilizes a data attribute to call a servlet. When passing a parameter to the URL, if the value is not in English but instead Arabic, for example, the value appears distorted when retrieved in the specified ...