新年惊喜!CSS 为 “Happy New Year 2025” 披上 3D 旋转华服

256 阅读2分钟

这段代码是一个 HTML 页面,它包含了 CSS 样式,用于创建一个 3D 旋转动画效果的文字展示。文字内容为“Happy New Year 2025”,通过 CSS 动画实现 3D 旋转效果。

演示效果

HTML&CSS

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;600;700;800;900&display=swap"
        rel="stylesheet">
    <title>公众号关注:前端Hardy</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Poppins', -apple-system, BlinkMacSystemFont, sans-serif;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: #222;
            overflow: hidden;
        }

        .container {
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .container .box {
            transform-style: preserve-3d;
            animation: animate 25s linear infinite;
        }

        @keyframes animate {
            0% {
                transform: perspective(1000px) rotateX(0deg) rotate(25deg);
            }

            100% {
                transform: perspective(1000px) rotateX(360deg) rotate(25deg);
            }
        }

        .container .box span {
            position: absolute;
            color: #fff;
            font-size: 3.5em;
            white-space: nowrap;
            text-transform: uppercase;
            font-weight: 900;
            padding: 0 10px;
            background: linear-gradient(90deg, transparent, rgba(0, 0, 0, 0.5), transparent);
            line-height: 0.76em;
            transform-style: preserve-3d;
            text-shadow: 0 5px 15px rgba(0, 0, 0, 0.25);
            transform: translate(-50%, -50%) rotateX(calc(var(--i)* 22.5deg)) translateZ(106px);
        }

        .container .box span i:nth-child(1) {
            font-style: initial;
            color: #ff246f;
        }

        .container .box span i:nth-child(2) {
            font-style: initial;
            color: #12b5ff;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="box">
            <span style="--i:1;"><i>Happy</i> New <i>Year</i> 2025</span>
            <span style="--i:2;"><i>Happy</i> New <i>Year</i> 2025</span>
            <span style="--i:3;"><i>Happy</i> New <i>Year</i> 2025</span>
            <span style="--i:4;"><i>Happy</i> New <i>Year</i> 2025</span>
            <span style="--i:5;"><i>Happy</i> New <i>Year</i> 2025</span>
            <span style="--i:6;"><i>Happy</i> New <i>Year</i> 2025</span>
            <span style="--i:7;"><i>Happy</i> New <i>Year</i> 2025</span>
            <span style="--i:8;"><i>Happy</i> New <i>Year</i> 2025</span>
            <span style="--i:9;"><i>Happy</i> New <i>Year</i> 2025</span>
            <span style="--i:10;"><i>Happy</i> New <i>Year</i> 2025</span>
            <span style="--i:11;"><i>Happy</i> New <i>Year</i> 2025</span>
            <span style="--i:12;"><i>Happy</i> New <i>Year</i> 2025</span>
            <span style="--i:13;"><i>Happy</i> New <i>Year</i> 2025</span>
            <span style="--i:14;"><i>Happy</i> New <i>Year</i> 2025</span>
            <span style="--i:15;"><i>Happy</i> New <i>Year</i> 2025</span>
            <span style="--i:16;"><i>Happy</i> New <i>Year</i> 2025</span>
        </div>
    </div>
</body>

</html>

HTML 结构

  • container: 创建一个类名为 container 的 div 元素,用于包含动画内容。
  • box: 创建一个类名为 box 的 div 元素,用于包含旋转的文字。
  • span 元素,每个元素包含 Happy New Year 2025 文字,通过 CSS 变量--i 控制旋转角度。

CSS 样式

  • *: 重置所有元素的边距、填充,设置 box-sizing 为 border-box,并统一字体为“Poppins”。
  • body: 设置页面的显示方式、对齐方式、最小高度和背景色。
  • .container: 设置容器的显示方式和对齐方式。
  • .container .box: 设置动画盒子的样式,包括 3D 效果和无限循环的旋转动画。
  • @keyframes animate: 定义动画,使文字围绕 X 轴旋转 360 度。
  • .container .box span: 设置文字的样式,包括位置、颜色、字体大小、背景渐变、阴影和 3D 效果。
  • .container .box span i:nth-child(1), .container .box span i:nth-child(2): 设置文字中特定部分的颜色。