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 创建更逼真的火焰动画。

相关推荐
Larcher1 小时前
JS 变量提升:代码没动,为什么执行顺序就变了?
前端·javascript·前端框架
yingyima1 小时前
MySQL 事件调度器速查:核心语法与实战代码
前端
要写代码1 小时前
2026-Css忘掉一切、归零再启
css
GISer_Jing1 小时前
Claude Code多Agent架构深度剖析
前端·人工智能·架构·自动化
comphub1 小时前
comp-hub:让你的 Vue 业务组件真正"活"起来
前端
AI砖家1 小时前
Claude Code 跳过确认完全指南:让 AI 自己完成开发任务
前端·人工智能·python·ai编程·代码规范
KaMeidebaby1 小时前
卡梅德生物技术快报|Pull Down 实验在 lncRNA - 蛋白互作机制研究中的应用实例解析
大数据·前端·架构·spark·新浪微博
锋行天下1 小时前
让nginx网关扛下所有攻击
前端·后端·nginx
广州华水科技2 小时前
单北斗GNSS是什么?主要有哪些形变监测应用?
前端
边界条件╝2 小时前
微前端进阶(二)
前端