CSS3 实现火焰-小火苗效果

创建 CSS3 火焰效果可以通过组合 CSS 动画、伪元素 和 渐变 来实现。以下是一个简单的实现步骤,展示如何制作动态火焰效果

1. HTML 结构

我们只需要一个简单的 div 容器:

html 复制代码
<div class="fire"></div>

2. CSS 实现

基础样式

使用 position: absolute 和渐变背景色来设置火焰的颜色层次。

css 复制代码
body {
  margin: 0;
  background: black; /* 背景颜色模拟夜晚 */
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.fire {
  position: relative;
  width: 50px;
  height: 80px;
  border-radius: 50%; /* 圆润的形状 */
  background: radial-gradient(circle, #ff4500, #ff8c00, transparent);
  animation: flicker 0.2s infinite ease-in-out;
  box-shadow: 0 0 30px rgba(255, 69, 0, 0.6);
}

动画火焰抖动

通过 @keyframes 来模拟火焰跳动的效果:

css 复制代码
@keyframes flicker {
  0%, 100% {
    transform: scale(1) translateY(0);
    opacity: 1;
  }
  50% {
    transform: scale(1.2) translateY(-10px); /* 火焰抖动并缩放 */
    opacity: 0.8;
  }
}

添加火焰层次

使用伪元素(::before 和 ::after)来叠加多层火焰:

css 复制代码
.fire::before,
.fire::after {
  content: '';
  position: absolute;
  border-radius: 50%;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background: radial-gradient(circle, #ffa500, #ff4500, transparent);
  animation: flicker 0.3s infinite ease-in-out;
}

.fire::before {
  width: 70%;
  height: 70%;
  top: 15%;
  left: 15%;
  background: radial-gradient(circle, #ff6347, #ff4500, transparent);
  animation-duration: 0.25s;
}

3. 添加更复杂的动画效果

通过叠加火焰的多层渐变,进一步增强火焰效果:

css 复制代码
.fire {
  background: radial-gradient(circle, #ff4500, #ff8c00, transparent);
  box-shadow: 0 0 30px rgba(255, 69, 0, 0.6);
  position: relative;
  animation: flicker 0.3s infinite ease-in-out;
}

.fire::before {
  content: '';
  position: absolute;
  top: -30px;
  left: -20px;
  width: 70px;
  height: 100px;
  border-radius: 50%;
  background: radial-gradient(circle, #ffa500, #ff4500, transparent);
  opacity: 0.7;
  animation: flicker 0.4s infinite ease-in-out;
}

.fire::after {
  content: '';
  position: absolute;
  top: -40px;
  left: -25px;
  width: 100px;
  height: 140px;
  border-radius: 50%;
  background: radial-gradient(circle, #ffff00, #ffa500, transparent);
  opacity: 0.5;
  animation: flicker 0.5s infinite ease-in-out;
}

完整代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS Fire Effect</title>
  <style>
    body {
      margin: 0;
      background: black;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }

    .fire {
      position: relative;
      width: 50px;
      height: 80px;
      border-radius: 50%;
      background: radial-gradient(circle, #ff4500, #ff8c00, transparent);
      animation: flicker 0.2s infinite ease-in-out;
      box-shadow: 0 0 30px rgba(255, 69, 0, 0.6);
    }

    @keyframes flicker {
      0%, 100% {
        transform: scale(1) translateY(0);
        opacity: 1;
      }
      50% {
        transform: scale(1.2) translateY(-10px);
        opacity: 0.8;
      }
    }

    .fire::before,
    .fire::after {
      content: '';
      position: absolute;
      border-radius: 50%;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      background: radial-gradient(circle, #ffa500, #ff4500, transparent);
      animation: flicker 0.3s infinite ease-in-out;
    }

    .fire::before {
      width: 70%;
      height: 70%;
      top: 15%;
      left: 15%;
      background: radial-gradient(circle, #ff6347, #ff4500, transparent);
      animation-duration: 0.25s;
    }
  </style>
</head>
<body>
  <div class="fire"></div>
</body>
</html>

效果预览

运行上述代码后,您会看到一个动态火焰效果,火焰会随着时间微微跳动。

可扩展性

颜色调整:更改 radial-gradient 中的颜色,可以生成不同风格的火焰(如蓝色火焰)。

大小调整:修改 .fire 的 width 和 height,调整火焰的大小。

添加粒子效果:结合 CSS 粒子动画(如火花)进一步增强视觉效果。

如果需要更多高级效果,可以结合 SVG 或 WebGL 创建更逼真的火焰动画。

相关推荐
阿帕琪尔几秒前
😎vite插件: 自动打包压缩图片和转webp(二)
前端·vite
思慕很大很大6 分钟前
浏览器基础知识-进程与线程
前端·浏览器
猩猩程序员11 分钟前
dial9:一个强悍的 Tokio 调试工具!!!
前端
gyx_这个杀手不太冷静30 分钟前
OpenCode 深度解析:架构设计、工具链集成与工程化实践
前端·架构·ai编程
m0_459252461 小时前
fastadmin动态渲染统计信息
开发语言·前端·javascript·php
该怎么办呢1 小时前
Source/Core/Matrix4.js
前端·javascript
小江的记录本1 小时前
【MyBatis-Plus】Spring Boot + MyBatis-Plus 进行各种数据库操作(附完整 CRUD 项目代码示例)
java·前端·数据库·spring boot·后端·sql·mybatis
于慨2 小时前
Capacitor
前端
IT凝冬2 小时前
liunx 的 centos7 安装ngin
前端
赵锦川2 小时前
大屏比例缩放
前端·javascript·html