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

相关推荐
weixin_382395232 小时前
为小工厂量身打造:本地部署的物料管理系统带缺料计算
前端·制造
__zRainy__2 小时前
解决pnpm v10+不自动构建
前端·pnpm·工程化
猫猫不是喵喵.3 小时前
Vue3 Props 属性
前端·javascript·vue.js
醉城夜风~4 小时前
CSS元素显示模式(display)
前端·css
AI大模型-小华5 小时前
Codex 三方充值快速入门指南
java·前端·数据库·chatgpt·ai编程·codex·chatgpt pro
做前端的娜娜子7 小时前
同一链接实现 PC Web 与移动 H5 自适应
前端·掘金·金石计划
小帅不太帅7 小时前
架构没变、规模没变,DeepSeek V4 Flash 正式版凭什么暴涨 47 分?
前端·aigc·deepseek
jarvisuni8 小时前
DeepSeekFlash前端依旧拉垮,而且变慢了很多!
前端·javascript·算法
卷福同学9 小时前
AI编程出海第二步:验证关键词能否做站
前端·人工智能·后端