Image Flip with jQuery CSS

In this tutorial, we are going to see how to flip an image using jQuery and CSS. In a previous tutorial, we have seen various jQuery animation to rotate image.

In this example, we are flipping images in a horizontal direction using a 3D transform. We are using the CSS transform property to flip images in X-axis. This CSS property will be set via jQuery.

View Demo

Image Flip HTML and CSS

This code shows HTML markup for the flippable image.

jquery-image-flip

<div id="flip-box">
	<img src="fixed_bg.jpg">
</div>
<div id="flip-link" class="demo-submit">
	Flip the Image
</div>

and the styles are,

<style>
	#flip-box {
		width: 200px;
		height: 150px;
		transition: 0.9s;
		transform-style: preserve-3d;
		position: relative;
	}
	.demo-submit{
		padding: 10px 20px;
		background: #555;
		border: 0;
		color: #FFF;
		display:inline-block;
		margin-top:20px;
	}
</style>

jQuery Image Flip

This jQuery script shows how to change the image transform property on the click event. By changing transform property the image will flip back and forth. The code is,

<script type="text/javascript">
	$("#flip-link").on("click",function(){
		if($("#flip-box").css("transform") == 'none') {
			$("#flip-box").css("transform","rotateY(180deg)");
			$("#flip-box img").attr("src","fixed_bg1.jpg");
		}
		else {
			$("#flip-box").css("transform","");
			$("#flip-box img").attr("src","fixed_bg.jpg");
		}
	})
</script>

View DemoDownload

Photo of Vincy, PHP developer
Written by Vincy Last updated: July 12, 2022
I'm a PHP developer with 20+ years of experience and a Master's degree in Computer Science. I build and improve production PHP systems for eCommerce, payments, webhooks, and integrations, including legacy upgrades (PHP 5/7 to PHP 8.x).

Leave a Reply

Your email address will not be published. Required fields are marked *

Explore topics
Need PHP help?