探索 HTML 和 CSS 实现的蜡烛火焰

效果演示

这段代码是一个模拟蜡烛火焰的HTML和CSS代码。它创建了一个具有动态效果的蜡烛火焰动画,包括火焰的摆动、伸缩和光晕的闪烁。

HTML

html 复制代码
<div class="holder">
    <div class="candle">
        <div class="blinking-glow"></div>
        <div class="thread"></div>
        <div class="glow"></div>
        <div class="flame"></div>
    </div>
</div>
  • holder:这是蜡烛的容器,用于包含蜡烛的所有元素。
  • candle:这是蜡烛的主体部分,包含了蜡烛的各个部分,如闪烁的光晕、烛芯、光晕和火焰。
  • blinking-glow:模拟蜡烛火焰周围的闪烁光晕。
  • thread:模拟蜡烛的烛芯。
  • glow:模拟蜡烛火焰底部的光晕。
  • flame:模拟蜡烛的火焰。

CSS

css 复制代码
.holder {
  margin: 12rem auto 0;
  width: 150px;
  height: 400px;
  position: relative;
}

.holder *, .holder *:before, .holder *:after {
  position: absolute;
  content: "";
}

.candle {
  bottom: 0;
  width: 150px;
  height: 300px;
  border-radius: 150px / 40px;
  -webkit-box-shadow: inset 20px -30px 50px 0 rgba(0, 0, 0, 0.4), inset -20px 0 50px 0 rgba(0, 0, 0, 0.4);
  box-shadow: inset 20px -30px 50px 0 rgba(0, 0, 0, 0.4), inset -20px 0 50px 0 rgba(0, 0, 0, 0.4);
  background: #190f02;
  background: -webkit-gradient(linear, left top, left bottom, from(#e48825), color-stop(#e78e0e), color-stop(#833c03), color-stop(50%, #4c1a03), to(#1c0900));
  background: linear-gradient(#e48825, #e78e0e, #833c03, #4c1a03 50%, #1c0900);
}

.candle:before {
  width: 100%;
  height: 40px;
  border-radius: 50%;
  border: 2px solid #d47401;
  background: #b86409;
  background: radial-gradient(#ffef80, #b86409 60%);
  background: radial-gradient(#eaa121, #8e4901 45%, #b86409 80%);
}

.candle:after {
  width: 34px;
  height: 10px;
  left: 50%;
  -webkit-transform: translateX(-50%);
  -ms-transform: translateX(-50%);
  transform: translateX(-50%);
  border-radius: 50%;
  top: 14px;
  -webkit-box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.5);
  box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.5);
  background: radial-gradient(rgba(0, 0, 0, 0.6), transparent 45%);
}

.thread {
  width: 6px;
  height: 36px;
  top: -17px;
  left: 50%;
  z-index: 1;
  border-radius: 40% 40% 0 0;
  -webkit-transform: translateX(-50%);
  -ms-transform: translateX(-50%);
  transform: translateX(-50%);
  background: #121212;
  background: -webkit-gradient(linear, left top, left bottom, from(#d6994a), color-stop(#4b232c), color-stop(#121212), color-stop(black), color-stop(90%, #e8bb31));
  background: linear-gradient(#d6994a, #4b232c, #121212, black, #e8bb31 90%);
}

.flame {
  width: 24px;
  height: 120px;
  left: 50%;
  -webkit-transform-origin: 50% 100%;
  -ms-transform-origin: 50% 100%;
  transform-origin: 50% 100%;
  -webkit-transform: translateX(-50%);
  -ms-transform: translateX(-50%);
  transform: translateX(-50%);
  bottom: 100%;
  border-radius: 50% 50% 20% 20%;
  background: rgba(255, 255, 255, 1);
  background: -webkit-gradient(linear, left top, left bottom, color-stop(80%, white), to(transparent));
  background: linear-gradient(white 80%, transparent);
  -webkit-animation: moveFlame 6s linear infinite, enlargeFlame 5s linear infinite;
  animation: moveFlame 6s linear infinite, enlargeFlame 5s linear infinite;
}

.flame:before {
  width: 100%;
  height: 100%;
  border-radius: 50% 50% 20% 20%;
  -webkit-box-shadow: 0 0 15px 0 rgba(247, 93, 0, .4), 0 -6px 4px 0 rgba(247, 128, 0, .7);
  box-shadow: 0 0 15px 0 rgba(247, 93, 0, .4), 0 -6px 4px 0 rgba(247, 128, 0, .7);
}

@-webkit-keyframes moveFlame {
  0%, 100% {
    -webkit-transform: translateX(-50%) rotate(-2deg);
    transform: translateX(-50%) rotate(-2deg);
  }

  50% {
    -webkit-transform: translateX(-50%) rotate(2deg);
    transform: translateX(-50%) rotate(2deg);
  }
}

@keyframes moveFlame {
  0%, 100% {
    -webkit-transform: translateX(-50%) rotate(-2deg);
    transform: translateX(-50%) rotate(-2deg);
  }

  50% {
    -webkit-transform: translateX(-50%) rotate(2deg);
    transform: translateX(-50%) rotate(2deg);
  }
}

@-webkit-keyframes enlargeFlame {
  0%, 100% {
    height: 120px;
  }

  50% {
    height: 140px;
  }
}

@keyframes enlargeFlame {
  0%, 100% {
    height: 120px;
  }

  50% {
    height: 140px;
  }
}

.glow {
  width: 26px;
  height: 60px;
  border-radius: 50% 50% 35% 35%;
  left: 50%;
  top: -48px;
  -webkit-transform: translateX(-50%);
  -ms-transform: translateX(-50%);
  transform: translateX(-50%);
  background: rgba(0, 133, 255, .7);
  -webkit-box-shadow: 0 -40px 30px 0 #dc8a0c, 0 40px 50px 0 #dc8a0c, inset 3px 0 2px 0 rgba(0, 133, 255, .6), inset -3px 0 2px 0 rgba(0, 133, 255, .6);
  box-shadow: 0 -40px 30px 0 #dc8a0c, 0 40px 50px 0 #dc8a0c, inset 3px 0 2px 0 rgba(0, 133, 255, .6), inset -3px 0 2px 0 rgba(0, 133, 255, .6);
}

.glow:before {
  width: 70%;
  height: 60%;
  left: 50%;
  -webkit-transform: translateX(-50%);
  -ms-transform: translateX(-50%);
  transform: translateX(-50%);
  bottom: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.35);
}

.blinking-glow {
  width: 100px;
  height: 180px;
  left: 50%;
  top: -55%;
  -webkit-transform: translateX(-50%);
  -ms-transform: translateX(-50%);
  transform: translateX(-50%);
  border-radius: 50%;
  background: #ff6000;
  -webkit-filter: blur(50px);
  -moz-filter: blur(60px);
  -o-filter: blur(60px);
  -ms-filter: blur(60px);
  filter: blur(60px);
  -webkit-animation: blinkIt .1s infinite;
  animation: blinkIt .1s infinite;
}

@-webkit-keyframes blinkIt {
  50% {
    opacity: .8;
  }
}

@keyframes blinkIt {
  50% {
    opacity: .8;
  }
}
  • .holder:定义蜡烛容器的样式。它是一个相对定位的元素,宽150px,高400px,并且垂直居中。
  • .candle:定义蜡烛主体的样式。它是一个绝对定位的元素,宽150px,高300px,底部对齐,并且有圆角和渐变背景,模拟蜡烛的质感。
  • .candle:before 和 .candle:after:这些伪元素用于添加蜡烛顶部的装饰,如烛芯的顶部装饰和底部的光晕。
  • .thread:定义烛芯的样式。它是一个绝对定位的元素,位于蜡烛顶部,有圆角和渐变背景,模拟烛芯的质感。
  • .flame:定义火焰的样式。它是一个绝对定位的元素,位于蜡烛底部,有圆角和渐变背景,模拟火焰的形状和颜色。它还包含了两个动画,moveFlame 和 enlargeFlame,用于模拟火焰的动态效果。
  • .glow:定义蜡烛火焰底部的光晕样式。它是一个绝对定位的元素,位于蜡烛顶部,有圆角和渐变背景,模拟光晕的效果。
  • .blinking-glow:定义蜡烛火焰周围的闪烁光晕样式。它是一个绝对定位的元素,位于蜡烛顶部,有圆角和模糊背景,模拟闪烁的光晕效果。它还包含了一个动画blinkIt,用于模拟光晕的闪烁效果。
  • @keyframes moveFlame:定义火焰移动的动画,使火焰在垂直方向上轻微摆动。
  • @keyframes enlargeFlame:定义火焰放大的动画,使火焰在垂直方向上轻微伸缩。
  • @keyframes blinkIt:定义光晕闪烁的动画,使光晕的透明度在一定范围内变化,模拟闪烁效果。
相关推荐
西索斯coding3 分钟前
Claude Code 工具调用返回空怎么办?claude-opus-5.5 的 input_schema 校验踩坑记录与修复方案
服务器·前端·javascript·ai
疯狂敲代码的老刘4 分钟前
MarvellousSuspender浏览器扩展:1.4K星开源标签页冻结神器,Chrome吃内存终结者的干净复活版
前端·chrome
码上上班5 分钟前
shell 脚本知识点总结
前端·chrome
程序员Sunday7 分钟前
Flex 和 Grid 有什么区别?一维布局、二维布局怎么选?
前端·css·面试
Jlzn88889 分钟前
华南PACK线自动化率怎么统计才靠谱
前端·javascript·自动化
拖孩29 分钟前
AI 时代,个人开发者到底能不能靠一个小程序赚到钱
前端·后端·微信小程序
Hilaku30 分钟前
ERP选型时不问MCP,等于默认三年后再付一次集成费!
前端·javascript·程序员
程序人生88831 分钟前
几百张固定位置扫描件如何离线批量清?DocConverter Web 批量去水印链路
前端
deli00732 分钟前
一个汉字三种编码:UTF-8、GBK、UTF-16 逐字节对比实测
前端
庖丁解牛41 分钟前
第二课:把代码保存成网页 + 认识变量 (2)
前端