How to Use the CSS rotateY() Function to Rotate Elements in 3D

By

Introduction

The CSS rotateY() function is a powerful tool for adding depth and motion to your web designs. It rotates an element around its vertical Y-axis, creating a horizontal flip effect—imagine a door swinging open or a card flipping over. This function is part of the transform property and works best when combined with 3D perspective settings. In this guide, you'll learn step by step how to apply rotateY() effectively, from basic syntax to smooth animations.

How to Use the CSS rotateY() Function to Rotate Elements in 3D
Source: css-tricks.com

What You Need

Step-by-Step Guide

Step 1: Understand How rotateY() Works

The Y-axis runs vertically through the center of an element. When you rotate around this axis, the element turns horizontally—left edge moves away or toward you depending on the angle. Picture a pin stuck through the top and bottom center of a piece of paper; the paper can only spin left or right. This understanding is crucial for predicting the visual outcome.

Step 2: Set Up the Parent with Perspective

For rotateY() to create a true 3D effect, you must add the perspective property to the parent container. Perspective defines the distance from the viewer's eyes to the element. Without it, the rotation will look flat and shrunken.

.parent {
  perspective: 1000px;
}

Lower values (e.g., 400px) make the element appear closer and exaggerate the 3D effect. Higher values (e.g., 2000px) push it farther away, reducing the visible depth. A good starting point is 1000px.

Step 3: Apply rotateY() to the Child Element

On the element you want to rotate, use the transform property with the rotateY() function:

.card {
  transform: rotateY(45deg);
}

This rotates the card 45 degrees to the right. Positive angles rotate the right edge away from you; negative angles rotate the left edge away.

Step 4: Choose an Angle Value

The function accepts a single <angle> argument, which can be expressed in four units:

Examples:

How to Use the CSS rotateY() Function to Rotate Elements in 3D
Source: css-tricks.com
/* Degrees */
rotateY(45deg);
rotateY(-180deg);

/* Turns */
rotateY(0.25turn);
rotateY(1turn);

/* Radians */
rotateY(3.14rad); /* ≈ 180° */

Remember: positive = right edge away, negative = left edge away.

Step 5: Add a Smooth Transition

To animate the rotation, add the transition property, often on a hover state:

.card {
  transition: transform 0.3s ease;
}

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

This creates a 3D flip effect. You can also trigger the rotation with JavaScript for more complex interactions.

Step 6: Test and Refine the Perspective Value

Experiment with different perspective values on the parent to control the intensity of the 3D effect. A lower value (e.g., 500px) makes the rotation more dramatic; a higher value (e.g., 2000px) makes it more subtle. Also consider the transform-style: preserve-3d if you have nested 3D transforms.

Tips for Best Results

Now you're ready to add stunning 3D rotations to your projects. Start with a simple card flip and experiment from there!

Related Articles

Recommended

Discover More

ROCm 7.2.3 vs ROCm 7.0.0: Performance Gains on the Radeon AI PRO R9700WebAssembly JSPI API Bridges Synchronous-Asynchronous Gap: Breaking News on Web Development MilestoneAnthropic Reverses Ban on OpenClaw: Claude Subscribers Get Agent SDK Credits – But With Strict LimitsMastering the Shift: How to Migrate Your Flutter Project from CocoaPods to Swift Package ManagerCISA Warns of Active Exploitation of 'Copy Fail' Linux Flaw Leading to Full System Compromise