Rotating Images with CSS Transitions

Is there a way to rotate an image 90 degrees on click and then back to 0 degrees with another click, using only CSS? I found a solution with jQuery on this website, but I'm looking for a pure CSS alternative.

Answer №1

To achieve rotation using CSS, you can utilize the transform:rotate() property. Below is a basic JavaScript function I created for this purpose.

JavaScript Function:

function rotateElement(elem, angle){
    //Cross-browser support:
    var css = ["-moz-","-webkit-","-ms-","-o-","",""]
               .join("transform:rotate(" + angle + "deg);")
    elem.style.cssText += css;
}

For demonstration purposes, here is an example with a live demo on Fiddle: http://jsfiddle.net/zvuyc/:

<script>
window.onload = function(){
    var angle = 0;
    document.getElementById("button").onclick = function(){
        angle += 90 % 360;
        rotateElement(document.getElementById("image"), angle);
    }
}
</script>
<input type="button" id="button" value="Rotate by 90deg">
<img src="http://simplyeng.com/wp-content/uploads/2008/11/indiana_tux.png" id="image" />

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

Retrieve the HTML code of a webpage, including any embedded iframes

Is there a way to extract the HTML content of a webpage, including any iframes present within <iframe> elements? I know that the Chrome Developer Tools "Elements" tab can display iframes in this manner. When I mention extracting the HTML contents, I ...

Designing Tables for User Interfaces

In a table with 4 rows, the first row must always be filled by the user. The remaining rows are optional. What is the best way to visually indicate this requirement? This should be achieved using only HTML, CSS, and jQuery. ...

Guidance on creating cookies using Angular

I have been attempting to set a cookie using the method outlined in this post. However, despite following the instructions, I seem to be missing a crucial step as the console only displays "undefined". Below is the complete HTML content that I am using: & ...

How to apply hover effect to an image within a div using TailwindCSS

Is there a way to animate only the image and not the entire card (div) when hovering over it? I want specifically for the image to perform an animation, such as bounce effect. I'm using next/image for displaying the image. Can this animation be achie ...

`Looking for help with transitioning CSS to JavaScript`

I'm facing an issue that needs to be resolved. Please check out the link provided below. pop up modal This is the HTML code snippet.... <div class="wrap"> <a href="#modal-one" class="btn btn-big">Modal!</a> </div> <!- ...

How can I use Moment.js to show a custom message based on the current day of the week?

Currently, I am utilizing Moment.js to dynamically showcase various messages based on the day of the week. For instance, if it's: If it's Monday, I want it to display "It's Monday!" If it's Tuesday, I'd like it to show "Happy Tu ...

sketch a portion of a picture on a canvas

My challenge involves a canvas with dimensions of 400 by 400, and an image sized at 3392 by 232. I am looking to crop the first 400 pixels of this image and display them within the canvas. How can I achieve this task? $(document).ready(function () { v ...

Limit Use of Special Characters in HTML and AngularJS

` ~ ! @ # $ % ^ & * ( ) _ + = { } | [ ] \ : ' ; " < > ? , . / I need to limit the use of special characters and numbers in the input field. To achieve this, I have employed the following pattern: ng-pattern="/^[a-zA-Z ]*$/" This patt ...

Adding a border to the <area> element: A step-by-step guide

Can a border be added around an <area> element? This would be useful for testing an imagemap, however the following code does not achieve the desired effect: area { outline: 1px solid red; border: 1px solid red; } ...

Unwrapping the Angular ngForm Mystery: Resolving

I was attempting to retrieve values using ngOnInit and initializing the form with default values, but for some reason the form is returning as undefined. I also tried using patchValue, but since the form is undefined, it doesn't work. It's intere ...

Angular's ng-disabled feature seems to be experiencing a delay in functionality

While editing the file, I have the ability to modify the filename and add additional files for versions. Once a file has been chosen, the filename edit field should be disabled immediately. Despite attempting the following code, the field remains enabled u ...

Adding color to characters, digits in an HTML file

Is it possible to individually style each letter, number, and symbol in an HTML document with a unique color? I am interested in creating a text editor that applies specific colors to each glyph for those who have grapheme-color synesthesia. While there ar ...

What is the resulting height when both a <p> and <span> tag are styled with em font sizes?

When working on designing in the browser with CSS, it is important to note that the font-size set on a <span> tag is relative and based on the <p> tag. Exploration After conducting some research, I came across a recent question related to nes ...

What methods can I use in JavaScript to modify input values and update them in my HTML output?

How can I retrieve the input number value in HTML, perform a mathematical formula with the value when it changes, and display the updated data on the page without reloading? This is my HTML: <script src="https://ajax.googleapis.com/ajax/libs/jquery/3. ...

Using DataTables with jQuery, the CSS background color setting does not function properly

I've attempted various solutions, however, it still doesn't seem to be working. I am attempting to present data from a database in a table using the jQuery DataTables plugin. Everything appears to be correct technically, but the background color ...

Is there a way to adjust the transparency of an image without affecting any overlaid text while using Bootstrap's carousel feature?

I am currently working with Bootstrap v4 to build a carousel featuring an image with caption text. My goal is to have the image faded while leaving the text unaffected. Despite trying several non-Bootstrap solutions, I have been unsuccessful in achieving t ...

Tips for modifying the HTML code of a WordPress v4.9.5 page

I've been trying to make some changes to a page on my website. I followed this link on how to edit the HTML code of a WordPress page, but it seems that the version I'm using is different and doesn't have an Editor tab available. I'm una ...

Styling the background of the endAdornment in a material-ui textfield using React

I tried following the instructions from this source: Unfortunately, the example code provided doesn't seem to be functioning properly now. Is there a way to achieve the same result without having that right margin so that it aligns better with the r ...

What is the process for restricting access to folders in Magento?

For instance: I desire that instead of my tree, visitors see the following: Access Denied Unauthorized access to /skin on this server. ...

What steps should I take to ensure that clicking this button triggers the API request and returns the data in JSON format?

I'm attempting to have the button with id 'testp' return an api request in json format, however it seems to not be functioning properly. You can find the HTML code link here: https://github.com/atullu1234/REST-API-Developer-1/blob/main/js-bu ...