CSS

Flip an Image with CSS (Horizontally and Vertically)

To flip an image horizontally (mirror it) in CSS, use transform: scaleX(-1). To flip it vertically, use scaleY(-1). A negative scale draws the element mirrored without changing its size or layout.

Demo:

➜ A
original
➜ A
scaleX(-1)
➜ A
scaleY(-1)
➜ A
flip on hover

Flip horizontally and vertically

scaleX(-1) mirrors left to right, scaleY(-1) top to bottom, and scale(-1, -1) does both, which is the same as rotating it 180°. Modern browsers also accept the standalone scale property.

css
.flip-horizontal { transform: scaleX(-1); }
.flip-vertical   { transform: scaleY(-1); }
.flip-both       { transform: scale(-1, -1); }

/* Same thing with the individual property */
.flip-horizontal { scale: -1 1; }

Flip on hover

Add a transition and the image flips smoothly when the cursor is over it.

css
img.flip-on-hover {
  transition: transform 0.4s ease;
}

img.flip-on-hover:hover {
  transform: scaleX(-1);
}

3D card flip

For a card that turns over to show its back, rotate it around the Y axis in 3D and hide each face’s back side.

css
.card { perspective: 800px; }

.card__inner {
  position: relative;
  transition: transform 0.6s;
  transform-style: preserve-3d;
}

.card:hover .card__inner { transform: rotateY(180deg); }

.card__front,
.card__back {
  backface-visibility: hidden;
}

.card__back {
  position: absolute;
  inset: 0;
  transform: rotateY(180deg);
}

FAQ

How do I mirror an image in CSS?
transform: scaleX(-1). It flips the image horizontally, like a mirror.

Does flipping change the layout?
No. Transforms are visual only: the image keeps its size and position, and nothing around it moves.

Can I flip an image in HTML without CSS?
There’s no HTML attribute for it. Use an inline style (style="transform: scaleX(-1)") or flip the image file itself.

Why does my flipped image look blurry?
It shouldn’t: a -1 scale doesn’t resample the image. If it’s blurry, check for an extra scale or a fractional translate on the same element.

Did you like this CSS code snippet? Share it with your friends:
Want to learn more? Ask your AI: