- Image rotation with HTML and CSS
- Image rotation and flip operations using CSS and HTML
- Image Rotation using rotate() Function
- Continuously rotating the image using CSS for an animated effect
- Dynamic Image Rotation using ImageKit
- Rotating images in real-time with the rotate parameter
- Conclusion
- Rotate Image in HTML
- Use the transform CSS Property to Rotate an Image in HTML
- How to Rotate Image in HTML?
- Syntax
- How to Rotate Image in HTML?
- Using CSS Transform Property
- Example 1
- Normal Image
- Rotated image by 90 degrees using css transform property
- Using CSS Hover Effects
- Example 2
- Hover me to rotate by 60 degrees
- Using CSS Animations
- Example 3
- To rotate an image in a circular way using CSS
- Conclusion
Image rotation with HTML and CSS
Images are crucial for conveying information and enhancing the appearance of your website. You can use them for creating stunning visuals such as product images, banners, illustrations, and more.
Sometimes you might want to rotate or flip images on your page to fit specific visual requirements for your business.
This article will discuss how to rotate and flip images in your HTML using the CSS transform operations.
Later we will look at ImageKit’s real-time image transformations that will help you achieve similar effects across platforms without relying on CSS.
Image rotation and flip operations using CSS and HTML
You can apply CSS functions to an image to create transformations such as rotation, flip, and other effects. We will utilize the following CSS functions and build different effects —
For demonstration, we have picked up the following image by Helena Lopes from Pexels.
This image has been stored in the free integrated media library offered by ImageKit.io.
ImageKit is a complete image and video management and delivery product that helps us optimize image delivery in real-time with no effort. The image stored on ImageKit is accessible here —
https://ik.imagekit.io/ikmedia/rotation-blog/suv-image_CydtbqUYa.jpg
You can sign up on ImageKit for free forever and store your images and videos there. You can also deliver optimized content with a CDN directly on your website and apps using ImageKit URLs.
Using the img tag, we can embed this image on our website.
Let’s use different CSS functions to achieve different visual effects on these images.
Image Rotation using rotate() Function
As the name suggests, you can use the rotate(arg) function to rotate an image in a two-dimensional space. The image is rotated based on the argument passed to this function in CSS-supported units, such as degree, gradian, turn, or radian.
The CSS sample below rotates the image by 180 degrees:
The resultant image is shown below —
We can also provide a negative value to rotate the image in a counter-clockwise direction. Here we are trying to rotate an image by 40 degrees in the counter-clockwise direction.
Similarly, you can use other values in the rotate function to rotate the image according to your requirements.
Continuously rotating the image using CSS for an animated effect
We can also use CSS’s animation and keyframes properties to create an animation without using any Javascript or other third-party library.
Let’s look at a simple animation where we will rotate an image continuously and set the image to complete one rotation in 6 seconds. The keyframes definition defines the rotation, whereas the animation property defines the animation’s timing and repetition.
img < width: 300px; animation: rotation 6s infinite linear; >@keyframes rotation < from < transform: rotate(0deg); >to < transform: rotate(360deg); >>
The resulting animation looks like this —
Similarly, passing a value of -1 to the scaleY function will flip the image vertically.
Dynamic Image Rotation using ImageKit
In the above examples, we rotated the image using CSS properties. CSS is restricted to use in web browsers only.
Therefore, if we have to rotate an image on any other platform, send it out in an email that doesn’t support advanced CSS functions, or do not rely on CSS in general, then we need an alternative way to rotate the image.
ImageKit is a real-time transformation, optimization, and storage service that allows you to manipulate images and videos in real-time using an easy-to-use URL-transformation API.
Rotating images in real-time with the rotate parameter
Using ImageKit’s transformations is simple. You just need to specify the transformation in the specified format in the URL.
To rotate the image, ImageKit provides the rt parameter. To rotate the image by 180 degrees, we need to add rt-180 transformation parameter to the URL, as shown below.
https://ik.imagekit.io/ikmedia/rotation-blog/suv-image_CydtbqUYa.jpg?tr=rt-180
The resulting image is the same as passing 180deg to the rotate function in CSS.
We can then use this URL directly in our HTML, our apps, or an email without having to worry about whether the end device supports CSS or not.
We can also pass a negative value to the rt parameter to rotate the image in a counter-clockwise direction.
To rotate the image by 40 degrees in the counter-clockwise direction, we need to add rt-N40 parameter to the URL. The resulting image is the same as that obtained by passing a negative value to the rotate function in CSS.
https://ik.imagekit.io/ikmedia/rotation-blog/suv-image_CydtbqUYa.jpg?tr=rt-N40
We can also resize the image in real-time using the same URL-based image transformation API. To resize the image to 500px width while rotating it, we need to add w-500 to the above URL.
https://ik.imagekit.io/ikmedia/rotation-blog/suv-image_CydtbqUYa.jpg?tr=rt-N40,w-500
Our resulting image is rotated by 40 degrees and is resized to 500px width.
Note: Resizing with ImageKit is different than resizing using the width property in CSS. With ImageKit’s resizing, the image downloaded over the network is already resized and is hence smaller in size. With CSS-based resizing, the full resolution image is downloaded on the user’s device resulting in unnecessary bandwidth consumption and then resized on the user’s device.
Conclusion
Image rotation can come in handy when the original image needs to be modified to create the right visual effects on your website or app.
You can use CSS transformation functions to achieve this with minimal effort. But this makes the image rotation specific to web and mobile web platforms that support the required CSS.
ImageKit is a real-time image transformation, optimization, and management product that allows us to manipulate images in real-time using a URL-based API. It provides the rt parameter to rotate the image and more than 50 other URL-based parameters to manipulate images and videos on websites and apps in real-time.
It also offers ample media storage and optimized delivery bandwidth for free every month. You can get started with ImageKit by signing up for a free account and taking advantage of its real-time optimizations and transformations to deliver media on your website.
Rotate Image in HTML
This article will introduce a method to rotate an image in HTML.
Use the transform CSS Property to Rotate an Image in HTML
The CSS transform property implies modifying a particular element in our code. We need an external resource, the image we transform in this case, to transform it. This property applies a 2D or 3D transformation to an element. Furthermore, this property also gives the authorization to rotate, scale, move, skew, etc., to the details. It also alters the coordinate space of the CSS visual formatting model. The elements whose layout is governed by the CSS box model can only be transformed. We can set the transform property to a rotate() function mentioning the unit in the function to rotate an image in HTML. The value by which the image is to be rotated should be provided in the parenthesis of the function. The deg unit is used to specify the rotation value. We can use the inline CSS for this purpose. The CSS will be used in the tag.
For example, insert a regular image with the tag in HTML. Set the src attribute to https://loremflickr.com/320/320 . Then, for a 90-degree rotation, insert the image and add some styles to it. In the style attribute, set the transform property to rotate(90deg) . Then, add a
tag. Similarly, rotate the images to 180deg and 360deg .
The example below illustrates a method to rotate an image in an image source in HTML. We have styled the tag, meaning the image inserted can be rotated with the transform:rotate() property. We inserted the original image and rotated the image to 90 , 180 and 360 degrees. We can write the desired degree values in the rotate() function. The second image in the example rotates up to half of the axis. The third image looks upside down because it is rotated to 180 degrees. When the image completes a rotation of 360 degrees, it looks like the original image. Thus, we can use the transform() property and rotate() value to rotate the image using HTML and CSS.
html> body> Original Image img src="https://loremflickr.com/320/320" /> br> 90 degree Rotation img src="https://loremflickr.com/320/320" style="transform:rotate(90deg);"> br> 180 degree Rotation img src="https://loremflickr.com/320/320" style="transform:rotate(180deg);"> br> 360 degree Rotation img src="https://loremflickr.com/320/320" style="transform:rotate(360deg);"> br> body> html>
How to Rotate Image in HTML?
Images are an important part of the web page, and they need to be rotated sometimes to make them look better or fit on a web page. Image rotation in HTML is a relatively simple process that can be completed using CSS.
The process of changing the orientation of an image from a specific angle is called image rotation. The CSS transform property is a common and easy way to rotate an image. This property is used to move, rotate, scale, and perform several kinds of transformations of elements.
Syntax
Following is the syntax to rotate image in HTML −
Here «angle» is the amount of rotation to apply to the element, specified in degrees.
How to Rotate Image in HTML?
Images are a crucial part of web design, because this adds visual appeal to the website and provides information to the users. There are multiple methods to rotate an image in HTML, including CSS transform property, hover effects, and animations.
Here we will explore these methods one by one −
Using CSS Transform Property
The transform property is the most common way to rotate an image or element in CSS. The rotate() function is used to rotate an element. The rotate() function takes a degree value as its argument, which specifies the angle of rotation. For example, to rotate an image by 90 degrees, we can use the following CSS code −
The above code will rotate the image by 90 degrees using the transform property.
Example 1
Here is a full code example to rotate an image by 90 degrees using the CSS transform property.
body < text-align:center; >.my-img < transform: rotate(90deg); margin-top: 10px; padding: 5px; border: 5px solid #555; >imgNormal Image
Rotated image by 90 degrees using css transform property
Using CSS Hover Effects
To create dynamic and interactive web pages CSS hover effects are a popular way to rotate an image. In CSS, we can easily add a hover effect that rotates an image when the user hovers over it. To do this, we use the :hover pseudo-class in CSS. Here is an example −
In the above example, we are using the :hover pseudo-class to apply the rotation when the user hovers over the image. The transform property is used to rotate the image by 60 degrees.
Example 2
Here is a full code example to rotate an image by 60 degrees using the :hover pseudo-class.
body < text-align: center; >img < border: 5px solid #555; transition: transform 0.5s ease; >.my-img:hoverHover me to rotate by 60 degrees
Using CSS Animations
Rotating an image using CSS animation is one another way to create dynamic and interactive web pages. In CSS, we can easily add animation to the image that rotates it continuously or for a specified duration. For doing this we can use the following code.
In the above code, we are using the animation property to create a rotation animation that repeats continuously. The @keyframes rule is used to define the rotation animation. The from keyword sets the starting point of the animation, and the to keyword sets the end point.
Example 3
Here is a full code example to rotate an image in a circular way using CSS animation method.
To rotate an image in a circular way using CSS