欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 探索 HTML 和 CSS 实现的蜡烛火焰

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

2025/4/20 13:14:02 来源:https://blog.csdn.net/weixin_52814911/article/details/143798892  浏览:    关键词:探索 HTML 和 CSS 实现的蜡烛火焰

效果演示

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

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

.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:定义光晕闪烁的动画,使光晕的透明度在一定范围内变化,模拟闪烁效果。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词