Home > Web Front-end > Front-end Q&A > How to achieve image rotation effect with css

How to achieve image rotation effect with css

PHPz
Release: 2023-04-23 17:33:13
Original
3979 people have browsed it

CSS is one of the essential technologies in front-end development. Its powerful style control function can often help us optimize and beautify the effect. Among them, the new features of CSS3 have brought many conveniences to front-end development. For example, CSS3 can be used to achieve 3D effects, animation effects, transition effects, etc.

This article will introduce to you how to use CSS3 to achieve the effect of image rotation. Here, we will use the transform attribute to implement image rotation. Below, we will implement it through the following steps:

1. Add HTML structure and style

First, we need to add an img tag to display the image and add A CSS style that specifies parameters such as the width, height, and position of the image. The code is as follows:

<img src="http://example.com/image.jpg" class="rotate-img">
Copy after login
.rotate-img {
    width: 200px;
    height: 200px;
    position: relative;
    top: 100px;
    left: 100px;
}
Copy after login

2. Add CSS3 style code

Next, add the style code for the rotation effect in CSS. We use the CSS3 transform property to achieve the rotation effect. This attribute contains multiple values ​​to control the rotation, scaling, displacement and other changes of the element. The following is a style code to achieve the image rotation effect:

.rotate-img:hover {
    -webkit-transform: rotate(360deg);
    -moz-transform: rotate(360deg);
    -o-transform: rotate(360deg);
    transform: rotate(360deg);
}
Copy after login

In the above code, we use the :hover selector to specify the effect when the mouse is hovering, and then use the rotate() function in the transform attribute to control the element of rotation angle. Among them, -webkit-, -moz-, -o- prefixes are for compatibility with different browsers.

3. Effect display

Finally, we can view the effect in the browser. When the mouse hovers over the picture, the picture will rotate 360 ​​degrees based on the center point.

The complete code is as follows:




    
    图片旋转
    

    <img src="http://example.com/image.jpg" class="rotate-img">
Copy after login

In actual development, we can control the rotation angle and direction of the element by adjusting the parameters passed in the rotate() function to achieve richer rotations Effect.

In short, the transform attribute of CSS3 can help us achieve many interesting special effects, including image rotation, 3D effects, etc. Understanding and mastering these CSS effects is one of the essential skills for front-end development.

The above is the detailed content of How to achieve image rotation effect with css. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template