Skip to content
LOCZH/安大略 · 加拿大待机OK/--:--:--EST
M4M4RK_YUportfolio
  • 项目
    项目Overview
    • 作品精选案例与项目记录
    • 游戏可玩原型与游戏开发日志
  • 影像
    影像Overview
    • 档案影像合集与视觉实验
    • 商店印刷品、海报和限量物件
  • 日志
    日志Overview
    • 博客长篇开发日志与现场笔记
    • 笔记短观察、链接与代码片段
  • 资源
    资源Overview
    • 工具38 款浏览器内开发工具
    • 链接每日使用的开发与设计书签
  • 关于
  • 联系
EN

同步 · dev.to / @markyu

Quick Guide To 3D Transformations in CSS3 😎

Introduction Creating dynamic 3D scenes in web development can elevate your design, making...

发布日期
May 20 '24
·
阅读时长
3 min read
·
点赞
4
webdevcss3dfrontendchallenge
在 dev.to 查看

Introduction

Creating dynamic 3D scenes in web development can elevate your design, making it more engaging and interactive. In this guide, we'll explore the fundamentals of 3D transformations using CSS3. You'll learn about coordinate axes, perspective views, rotations, and scaling, and how to effectively apply these techniques to your web projects.

Coordinate Axes

Image description

Understanding 3D Scenes

In 2D scenes, we work with the x-axis (horizontal) and y-axis (vertical). In 3D scenes, we introduce a third axis: the z-axis, which is perpendicular to the screen.

  • Z-axis: Positive values move the element closer to the viewer, while negative values move it further away.

In CSS3, 3D transformations include several key functions:

  1. 3D Translation: translateZ() and translate3d(x, y, z)
  2. 3D Rotation: rotateX(), rotateY(), rotateZ(), and rotate3d()
  3. 3D Scaling: scaleZ() and scale3d()
  4. 3D Perspective View: perspective(n) function or perspective property

CSS3 3D Transform Functions

3D Translation

CSS3 provides the translateZ() and translate3d(x, y, z) functions for 3D translation. These functions allow you to move elements along the z-axis, giving the illusion of depth.

.element {
  transform: translate3d(10px, 20px, 30px);
}

In this example, the element is moved 10 pixels along the x-axis, 20 pixels along the y-axis, and 30 pixels along the z-axis.

3D Rotation

For 3D rotations, CSS3 offers rotateX(), rotateY(), rotateZ(), and rotate3d(). These functions enable you to rotate elements around the x, y, or z axes, respectively.

.element {
  transform: rotateX(30deg);
}

This rotates the element 30 degrees around the x-axis. You can combine rotations around multiple axes for complex effects.

Perspective View

Defining Perspective

Perspective view, also known as depth of field, gives a sense of depth to 3D elements. Without perspective, elements will appear uniformly sized regardless of their distance from the viewer.

Image description

To create a sense of depth:

  1. Set the perspective distance.
  2. Apply transformations along the z-axis.
.container {
  perspective: 1200px;
}

.element {
  transform: perspective(1200px) translateZ(200px);
}

Setting the perspective distance adjusts how the viewer perceives depth. The closer the perspective value (e.g., 900px), the more pronounced the depth effect.

Perspective-Origin

Image description

The perspective-origin property defines the viewpoint of the observer. By default, the z-axis is positioned at the center of the parent element.

.container {
  perspective-origin: 50% 50%;
}

You can adjust this property to change the observer's angle:

.container {
  perspective-origin: left top;
}

3D Rotation

The rotateX(), rotateY(), and rotateZ() methods rotate an element around the respective axis.

.element {
  transform: rotateX(30deg);
}

For complex rotations, use rotate3d():

.element {
  transform: rotate3d(1, 1, 1, 30deg);
}

This rotates the element 30 degrees around a vector (1, 1, 1).

Transform Style

The transform-style property determines whether child elements maintain their 3D position.

.container {
  transform-style: preserve-3d;
}

This ensures that nested elements retain their 3D transformations.

Transform Origin

The transform-origin property sets the point of origin for transformations.

.element {
  transform-origin: 50% 50% 50px;
}

This sets the transformation origin to the center of the element at 50px along the z-axis.

3D Scaling

The scaleZ() and scale3d() functions scale an element in 3D space. Note that scaleZ() requires additional transformations to be visible.

.element {
  transform: perspective(100px) scaleZ(2) translateZ(1px);
}

This scales the element along the z-axis, making it appear larger or smaller.

Practical Example

To create a 3D effect, combine multiple transformations:

.box {
  transform-style: preserve-3d;
  perspective: 800px;
}

.center {
  transform: scaleZ(10) rotateX(45deg);
}

Or:

.box {
  transform-style: preserve-3d;
}

.center {
  transform: perspective(800px) scaleZ(10) rotateX(45deg);
}

These examples demonstrate how to combine perspective, scaling, and rotation to create complex 3D effects.

Conclusion

Image description

Mastering CSS3 3D transformations allows you to create visually appealing and interactive web designs. By understanding and applying these techniques, you can add depth and dimension to your projects, enhancing the user experience.

相关阅读

react

3 Ways To Create Engaging React Loading Screens with Hooks

Creating engaging loading screens for your React applications can greatly enhance the user...

html

Create a Heart Shaped Animation with CSS3

Introduction Creating a personalized love confession page can be a fun and heartfelt way...

webdev

Implementing 3D Graphics in React

3D is an exciting area in computer science, and it could range from creating 3D shapes, vectors,...

原文发布

本文首发于 dev.to,评论与点赞保留在原站。

在 dev.to 继续阅读
上一篇Navigating the Clouds: A Comprehensive Guide to Modern Cloud InfrastructuresIntroduction: ​ As a full-stack developer, understanding cloud architecture is crucial...
返回档案
下一篇MySQL Performance Monitoring and Query AnalysisMySQL Performance Monitoring and Query Analysis In this guide, we will explore various...
返回档案
频道开放·随时打个招呼 · 2026
--:--:--EST
联系

看到什么有意思的?和我聊聊。

这是一个作品集,不是服务 · 但每一条留言我都会看 — 如果哪里让你有所触动,或者只想打个招呼,欢迎写信过来。

开启对话

订阅

偶尔收到一封简讯

来自 m4rkyu.com 的笔记与日志——简短、标注日期、没有杂音。随时可退订。

作品

线上发布、游戏作品与视觉档案。

  • 项目
  • 游戏
  • 档案
  • 日志

资源

每日好用的工具与个人收藏的链接库。

  • 搜索
  • 最新
  • 工具
  • 链接
  • 笔记
  • 主题
  • RSS
  • JSON Feed
  • 商店

工作室

背景、联系方式以及合作渠道。

  • 关于
  • 联系
  • 更新日志
  • 技术说明
  • 简历筹备中

社交

在常去的平台上找到我。

  • Facebook敬请期待
  • Instagram敬请期待
  • YouTube敬请期待
  • 领英敬请期待
M4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYU
始于 2024
ZhenXiao Mark YuZhenXiao Mark Yu
© 2026 ZhenXiao Mark Yu·加拿大 安大略
  • 邮件
  • GitHub
  • dev.to
  • 领英 (敬请期待)
  • 推特 / X (敬请期待)
  • Instagram (敬请期待)
由 Next.js 16 · React 19 · Tailwind 4 构建

由 Next.js 16 · React 19 · Tailwind 4 构建