Why rotate images with CSS?
Have you ever found the perfect image, only to need it facing a different way? In the past you'd open an image editor and save a second, rotated copy. Today CSS can rotate any image, icon or element for you, using the transform property.
Imagine you have an arrow pointing right, but you also need arrows pointing left, up and down. Instead of creating and loading four separate images, you can use one and let CSS turn it. That means fewer files to manage and fewer downloads, so your page loads faster.
Here's one arrow, shown four ways using nothing but CSS:
Rotating with transform
The transform property changes how an element is drawn: you can move it, resize it, skew it or rotate it. To rotate, use the rotate() function, with the angle in brackets:
.arrow {
width: 32px;
height: 32px;
}
.arrow--left { transform: rotate(180deg); }
.arrow--down { transform: rotate(90deg); }
.arrow--up { transform: rotate(-90deg); }
Add the matching class to your image in the HTML:
<img class="arrow" src="arrow.svg" alt="Next">
<img class="arrow arrow--left" src="arrow.svg" alt="Previous">
Which way does it turn?
Positive angles turn clockwise, like the hands of a clock. Negative angles turn anticlockwise. So rotate(-90deg) gives the same result as rotate(270deg), but is easier to picture: "a quarter turn back".
You're not limited to quarter turns. Any angle works, which is great for a playful, slightly tilted look:
.polaroid {
transform: rotate(-4deg);
}
Angle units
| Unit | Meaning | Quarter turn |
|---|---|---|
deg | Degrees: 360 in a full circle | 90deg |
turn | Turns: 1 is a full circle | 0.25turn |
rad | Radians, used in maths: about 6.28 in a circle | 1.5708rad |
grad | Gradians: 400 in a circle | 100grad |
Degrees are the most common, but turn is very readable for spinning animations: 1turn is simply "one full spin".
No more prefixes: the original version of this post repeated every rule five times, with -webkit-, -moz-, -ms- and -o- versions. These vendor prefixes were temporary names used before browsers agreed on the standard. Plain transform has worked in every browser for years, so you can delete the prefixed lines.
The newer rotate property
Modern CSS also has a standalone rotate property. It does the same job, but on its own line:
.arrow--left {
rotate: 180deg;
}
Why does this matter? Because transform holds all transformations in one value. If an element is already moved or scaled with transform, adding a rotation means repeating everything else:
/* With transform, you must repeat the scale */
.card { transform: scale(1.1); }
.card:hover { transform: scale(1.1) rotate(5deg); }
/* With separate properties, each one stands alone */
.card { scale: 1.1; }
.card:hover { rotate: 5deg; }
The separate rotate, scale and translate properties work in all modern browsers and make your code easier to read and change.
Rotating versus flipping
The original question was about displaying an image "flipped the other way". For a simple arrow, rotating it 180 degrees works perfectly. But for most images, rotating and flipping give very different results.
- Rotating turns the image around its centre, so it ends up upside down.
- Flipping mirrors the image, like a reflection, so left and right swap but it stays the right way up.
To flip an element, use scale with a negative number:
.flip-horizontal { transform: scaleX(-1); } /* mirror left to right */
.flip-vertical { transform: scaleY(-1); } /* mirror top to bottom */
The difference is easy to see with text:
Tip: flipping is perfect for photos of people or products that face the wrong way. Just check there's no text or logo in the image, which would appear backwards.
Changing the pivot point
By default, elements rotate around their centre. The transform-origin property moves that pivot point, a bit like moving the pin in a spinning paper windmill.
.swing {
transform-origin: top left;
rotate: 20deg;
}
You can use keywords such as top, bottom, left, right and center, or exact positions like 20px 50%. Here the same 20-degree rotation is applied with three different pivot points. The dot marks the pivot, and the faint outline shows the original position:
Rotation and the page layout
Here's something that surprises many beginners: rotating an element doesn't change the space it takes up on the page. The browser lays out the page first, then draws the rotated version on top, so nearby content stays exactly where it was.
For square images like our arrow, that's no problem. But rotate a wide image by 90 degrees, and it will spill over the content above and below it:
Text above the image, now partly covered.
Text below the image, now partly covered.
To fix this, give the container enough room for the rotated size, for example by setting its height to match the image's width. If you're rotating photos that came out sideways from a camera, there's often a better option:
Good to know: phone cameras store an "orientation" setting inside each photo. Modern browsers read it automatically and display photos the right way up, thanks to image-orientation: from-image, which is now the default. If a photo still looks sideways, it's usually best to fix the image file itself.
Animating a rotation
Rotations really come to life when they move. Add a transition, which animates a property smoothly from one value to another, and the rotation happens gradually instead of instantly.
Rotate on hover
This button's arrow turns to point downwards when you hover over it or tab to it with the keyboard:
.button svg {
transition: rotate 0.3s ease;
}
.button:hover svg,
.button:focus-visible svg {
rotate: 90deg;
}
Including :focus-visible means keyboard users see the same effect as mouse users.
A spinning loader
For continuous movement, use a @keyframes animation, which describes how an element changes over time. This classic loading spinner is just a circle with one coloured edge, rotating forever:
.spinner {
width: 40px;
height: 40px;
border: 4px solid #bfdbfe;
border-top-color: #1d4ed8;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { rotate: 1turn; }
}
linear keeps the speed steady, and infinite repeats the animation without stopping.
Respecting reduced motion
Some people find movement on screen uncomfortable or distracting, and switch on a "reduce motion" setting on their device. You can respect that choice with a media query:
@media (prefers-reduced-motion: reduce) {
.button svg { transition: none; }
.spinner { animation-duration: 3s; } /* slower, but still shows activity */
}
Tip: rotating isn't just for images. You can rotate text, buttons and whole sections too. Check out our CSS rotate generator that will write the code for you.
Wrapping up
With rotate() or the standalone rotate property, one image can face any direction you need. Use scaleX(-1) when you want a mirror image instead of an upside-down one, transform-origin to change the pivot, and a transition or animation to bring it all to life.