Flip / mirror an image horizontally + vertically with CSS
Achieve a horizontal flip of an image by applying CSS transform: scaleX(-1);
. For a vertical flip, use transform: scaleY(-1);
. To perform both flips simutaneously and create a full mirror effect, combine the two: transform: scale(-1, -1);
.
Then, add the class to your image:
Compatibility across different browsers
Confirm your flip effects function across all browsers. When dealing with legacy browsers such as Internet Explorer, include the -ms-transform
property or utilize the filter
property as an alternative approach. Below are the snippets:
Important to note: The filter
property, though effective, isn't recognized as standard CSS.
TailwindCSS counterparts
Implementing flip effects is made simpler for those using TailwindCSS. Simply include the relevant utility classes:
Comprehending rotation and flips
Flipping of images can also be executed through rotation. A transform: rotate(180deg);
rotates the image 180 degrees, resulting visually in a horizontal and vertical flip. However, this method doesn't technically flip the image along its axes, so be sure to choose it judiciously.
Techniques for advanced flipping
Access control and complexity in your flipping by resorting to matrix transformations in CSS when you want to flip an element which already has transformations applied:
The matrix transformation is essentially a combination of multiple transformations that doesn't override previous transform
properties, as it would if you used individual rotate
, scale
, skew
, etc., which is crucial for building interactive UIs.
Troubles with combining flips
There may be cases when combining horizontal and vertical flips may lead to unexpected results. This is usually due to an incorrect transform stacking order or misunderstanding of how CSS applies multiple transformations. Conduct an analysis of your existing CSS to locate and correct these issues.
Flipping background images
Flipping background images takes a slightly different approach versus standard img
tags. The related background properties need to be set appropriately:
This way, the entire element with the background image is flipped, not just the contained image.
Was this article helpful?