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

相关推荐
小小小小宇几秒前
Vue `import` 为什么可以异步加载
前端
WMYeah5 分钟前
【无标题】
前端·rust·抽奖程序·跨平台抽奖程序
Unbelievabletobe6 分钟前
免费外汇api的响应时间在不同时段下的波动分析
大数据·开发语言·前端·python
大哥,带带弟弟15 分钟前
Grafana 前端嵌入与 JWT 鉴权实战
前端·grafana
小小小小宇16 分钟前
前端 V8 引擎垃圾回收机制与内存问题排查
前端
前端老石人27 分钟前
CSS 值定义语法
前端·css
sheeta199838 分钟前
Vue 前端基础笔记
前端·vue.js·笔记
小小小小宇38 分钟前
GitLab + GitLab Runner + Qiankun 微前端 + Nginx + Node 中间件 前端开发机从零搭建 CI/CD 全流程
前端
前端那点事42 分钟前
别再写垃圾组件!Vue3 如何设计「真正可复用」的高质量通用组件
前端·vue.js
卷帘依旧1 小时前
JavaScript 中的 Symbol
前端·javascript