How to use CSS to achieve image rotation effects
CSS (Cascading Style Sheets) is a markup language used to set the style and layout of web pages. Through CSS, we can achieve many attractive web page effects, including image rotation effects. In this article, we’ll discuss how to use CSS to rotate images and provide some concrete code examples.
In CSS, we can use the transform
attribute to achieve the rotation effect of the image. transform
Attributes can apply various transformation functions, including rotation, scaling, translation, etc. The following is a simple HTML structure, containing an image and a button:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>图片旋转</title> <style> .image-container { position: relative; width: 300px; height: 300px; } .rotate-btn { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); padding: 10px 20px; background-color: #f00; color: #fff; border: none; cursor: pointer; } .image { width: 100%; height: 100%; object-fit: cover; } </style> </head> <body> <div class="image-container"> <img class="image lazy" src="/static/imghwm/default1.png" data-src="example.jpg" alt="example image"> <button class="rotate-btn" onclick="rotateImage()">旋转</button> </div> <script> function rotateImage() { var img = document.querySelector('.image'); img.style.transform = "rotate(45deg)"; } </script> </body> </html>
In the above example, we first define a container with the class name .image-container
, using to wrap the image and spin button. The container uses the position: relative
attribute to enable subsequent absolutely positioned buttons to be positioned relative to the container. In the container, we placed a picture and a button. The picture uses the .image
class name for style setting, and the button uses the .rotate-btn
class name for style setting.
It is worth noting that the image uses the object-fit: cover
attribute to maintain the proportion of the image in the container and fill the entire container.
In the button click event handling function rotateImage()
, we use JavaScript to select the image element and rotate the image through the style.transform
attribute . In this example, we set rotate(45deg)
to rotate the image 45 degrees clockwise.
In addition to JavaScript, we can also achieve image rotation effects through CSS animation. Here is an example of using CSS animation:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>图片旋转</title> <style> @keyframes rotateAnimation { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } .image { animation: rotateAnimation 5s infinite linear; } </style> </head> <body> <img class="image lazy" src="/static/imghwm/default1.png" data-src="example.jpg" alt="example image"> </body> </html>
In the above example, we used @keyframes
to define an animation named rotateAnimation
. The animation gradually rotates the image 360 degrees from 0% to 100%. Next, we applied this animation to the picture element .image
, and set the animation execution time to 5 seconds, the number of loops to unlimited, and the animation speed to linear.
Through the introduction of the above example, we can see that through the transform
property and animation properties of CSS, we can easily achieve the rotation effect of the image. Of course, there are many other transformation functions and animation features that allow us to create even more cool effects. I hope the introduction in this article can help you better use CSS to achieve web page effects.
Reference link:
- [MDN Web Document: CSS transform](https://developer.mozilla.org/zh-CN/docs/Web /CSS/transform)
- [MDN Web Documentation: CSS Animation](https://developer.mozilla.org/zh-CN/docs/Web/CSS/animation)
Note:
This article only briefly introduces how to use CSS to achieve image rotation effects, and provides some code examples. Readers can conduct further exploration and practice according to their own needs and actual situations.
The above is the detailed content of How to use CSS to achieve image rotation effect. For more information, please follow other related articles on the PHP Chinese website!

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more.

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1
Easy-to-use and free code editor

WebStorm Mac version
Useful JavaScript development tools
