Skip to content

Tailwind CSS - Transform Origin

Modern Guide to Tailwind CSS: Transform Origin

Section titled “Modern Guide to Tailwind CSS: Transform Origin”

This guide covers how to use Tailwind’s transform-origin utilities. These classes set the origin point for an element’s transformations, like rotations, scaling, and skews, allowing for more creative and controlled animations.

By default, transformations like rotate and scale originate from the center of an element. The transform-origin property lets you change this pivot point. Think of it like placing a pin in a piece of paper before you rotate it—the paper spins around the pin.

Tailwind provides classes for all the standard origin points:

ClassCSS Properties
origin-centertransform-origin: center;
origin-toptransform-origin: top;
origin-top-righttransform-origin: top right;
origin-righttransform-origin: right;
origin-bottom-righttransform-origin: bottom right;
origin-bottomtransform-origin: bottom;
origin-bottom-lefttransform-origin: bottom left;
origin-lefttransform-origin: left;
origin-top-lefttransform-origin: top left;

The following example demonstrates how different origin-* classes affect a rotate and scale transform. Hover over each card to see the transformation in action. A transition utility is added to animate the effect smoothly.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tailwind Transform Origin Example</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-slate-900 text-white p-8">
<h1 class="text-3xl font-bold text-center mb-10">Interactive Transform Origin Demo</h1>
<p class="text-center text-slate-400 mb-12">Hover over the elements to see them transform from different origin points.</p>
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-16">
<!-- Origin Top-Left -->
<div class="relative h-28 w-28 mx-auto">
<div class="absolute inset-0 bg-violet-500 rounded-lg transition-transform duration-300 origin-top-left hover:rotate-12 hover:scale-125"></div>
<span class="absolute inset-0 flex items-center justify-center font-mono">origin-top-left</span>
</div>
<!-- Origin Top -->
<div class="relative h-28 w-28 mx-auto">
<div class="absolute inset-0 bg-violet-500 rounded-lg transition-transform duration-300 origin-top hover:rotate-12 hover:scale-125"></div>
<span class="absolute inset-0 flex items-center justify-center font-mono">origin-top</span>
</div>
<!-- Origin Top-Right -->
<div class="relative h-28 w-28 mx-auto">
<div class="absolute inset-0 bg-violet-500 rounded-lg transition-transform duration-300 origin-top-right hover:rotate-12 hover:scale-125"></div>
<span class="absolute inset-0 flex items-center justify-center font-mono">origin-top-right</span>
</div>
<!-- Origin Left -->
<div class="relative h-28 w-28 mx-auto">
<div class="absolute inset-0 bg-pink-500 rounded-lg transition-transform duration-300 origin-left hover:rotate-12 hover:scale-125"></div>
<span class="absolute inset-0 flex items-center justify-center font-mono">origin-left</span>
</div>
<!-- Origin Center (Default) -->
<div class="relative h-28 w-28 mx-auto">
<div class="absolute inset-0 bg-pink-500 rounded-lg transition-transform duration-300 origin-center hover:rotate-12 hover:scale-125"></div>
<span class="absolute inset-0 flex items-center justify-center font-mono">origin-center</span>
</div>
<!-- Origin Right -->
<div class="relative h-28 w-28 mx-auto">
<div class="absolute inset-0 bg-pink-500 rounded-lg transition-transform duration-300 origin-right hover:rotate-12 hover:scale-125"></div>
<span class="absolute inset-0 flex items-center justify-center font-mono">origin-right</span>
</div>
</div>
</body>
</html>
  • UI Animations: Use origin-top-right or origin-top-left to make dropdown menus appear to expand from their trigger button.
  • Icon Effects: An icon might scale from the origin-bottom to create a ‘popping up’ effect.
  • Combining Transforms: transform-origin must be used with a transform utility like scale-*, rotate-*, or skew-* to have any visible effect. Always add a transition class to animate these changes smoothly.
  • Arbitrary Values: With Tailwind’s JIT engine, you can specify custom origins if needed, e.g., origin-[25%_75%]. This is an advanced feature for precise control.
  • Missing Transform Utility: Applying an origin-* class alone does nothing. It only sets the stage for another transformation.
  • No transform Base Class: In older versions of Tailwind, you needed a transform class. This is now enabled by default when you use any transform utility, so this is less of an issue, but good to be aware of.