276

How to flip any background image using CSS? Is it possible?

currenty I'm using this arrow image in a background-image of li in css

enter image description here

On :visited I need to flip this arrow horizontally. I can do this to make another image of arrow BUT I'm just curious to know is it possible to flip the image in CSS for :visited

Jitendra Vyas
  • 148,487
  • 229
  • 573
  • 852

5 Answers5

277

You can flip it horizontally with CSS...

a:visited {
    -moz-transform: scaleX(-1);
    -o-transform: scaleX(-1);
    -webkit-transform: scaleX(-1);
    transform: scaleX(-1);
    filter: FlipH;
    -ms-filter: "FlipH";
}

jsFiddle.

If you want to flip vertically instead...

a:visited {
    -moz-transform: scaleY(-1);
    -o-transform: scaleY(-1);
    -webkit-transform: scaleY(-1);
    transform: scaleY(-1);
    filter: FlipV;
    -ms-filter: "FlipV";
}

Source.

alex
  • 479,566
  • 201
  • 878
  • 984
  • @Jitendra You wouldn't do it that way, you'd put the icon in the span and do `a:visited span { ... }`. – alex Apr 24 '11 at 06:49
  • @alex - Please edit your jsfiddle example. I don't want to keep blank ` ` though. – Jitendra Vyas Apr 24 '11 at 06:50
  • @Jitendra If you don't want to make the compromise, you won't get it working in today's browsers. – alex Apr 24 '11 at 06:51
  • @alex- I'm only considering web-kit browsers. is it possible without using blank `span`? – Jitendra Vyas Apr 24 '11 at 06:53
  • @alex - I found a way without compromise. Thanks to you to give me a clue. – Jitendra Vyas Apr 24 '11 at 07:17
  • Also yay for [XY flipping](https://developer.mozilla.org/en-US/docs/Web/CSS/transform#scale); `transform: scale(-1, -1);`, which is equal to `transform: scale(-1);`. – Joel Purra Nov 29 '13 at 14:46
  • @Claudio you didn't made any improvements to the answer in your edit. I don't know why some one approved this edit. – Ramaraj T May 14 '14 at 09:02
  • 1
    You can also apply `transform: scaleX(-1);` on all children elements to undo the mirroring for anything besides the background >:D – Klesun Nov 02 '21 at 12:11