75.纯 CSS 创作一支摇曳着烛光的蜡烛

原文地址:https://segmentfault.com/a/1190000015580809

学习后效果地址:https://scrimba.com/c/c8PQ3PTB

感想:CSS 真强大!

HTML code:

<!-- candle: 蜡烛; glow: 光晕; flames:火焰; thread:灯芯 -->
<div class="candle">
    <span class="glow"></span>
    <span class="flames"></span>
    <span class="thread"></span>
</div>

CSS code:

html, body {
    margin: 0;
    padding: 0;
}
/* 设置body的子元素水平垂直居中 */
body{
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: black;
}
/* 画出蜡烛的轮廓 */
.candle{
    position: relative;
    top: 10em;
    display: flex;
    justify-content: center;
    /* 现在浏览器默认12px */
    font-size: 12px;
    width: 15em;
    height: 30em;
    /* 显示border看清candle的大小 
    border: 1px solid white; */
    border-radius: 10em / 4em;
    box-shadow: 
        inset 2em -3em 5em rgba(0, 0, 0, 0.4),
        inset -2em 0 5em rgba(0, 0, 0, 0.4);
    background: linear-gradient(
        orange,
        darkorange,
        sienna,
        saddlebrown 50%,
        rgba(0, 0, 0, 0.6)
    );
}
/* 用伪元素画出蜡烛的顶面 */
.candle::before{
    position: absolute;
    /* 设置为此元素设置的width、height包括此元素的border、padding、content */
    box-sizing: border-box;
    content: '';
    width: 100%;
    height: 5em;
    border: 0.2em solid darkorange;
    border-radius: 50%;
    background: radial-gradient(
        #444,
        orange,
        saddlebrown,
        sienna,
        darkorange
    );
    filter: opacity(0.7);
}
/* 画出灯芯 */
.thread{
    position: absolute;
    top: -1.8em;
    width: 0.6em;
    height: 3.6em;
    /* border: 1px solid white; */
    border-radius: 40% 40% 0 0;
    background: linear-gradient(
        #111,
        black,
        orange 90%
    );
}
/* 画出蜡烛轮廓 */
.flames{
    position: absolute;
    width: 2.4em;
   /* border: 1px solid white;*/
}
/* 利用flames的::before伪元素画出內焰 */
.flames::before{
    position: absolute;
    top: -4.8em;
    content:'';
    box-sizing: border-box;
    width: 100%;
    height: 6em;
    border: 0.2em solid transparent;
    border-radius: 50% 50% 35% 35%;
    filter: opacity(0.7);
}
/* 利用flames的::after伪元素画出外焰 */
.flames::after{
    position: absolute;
    top: -12em;
    content:'';
    width: 100%;
    height: 12em;
    border-radius: 50% 50% 20% 20%;
    box-shadow: 0 -0.6em 0.4em darkorange;
    background: linear-gradient(white 80%, transparent);
    animation: 
        enlarge 5s linear infinite,
        move 6s linear infinite;
}
@keyframes move {
    0%, 100% {
        transform: rotate(-2deg);
    }
    50% {
        transform: rotate(2deg);
    }
}
@keyframes enlarge {
    0%, 100% {
        height: 12em;
        top: -12em;
    }
    50% {
        height: 14em;
        top: -13em;
    }
}
/* 画出光晕 */
.glow{
    position: absolute;
    top: -16.5em;
    width: 10em;
    height: 18em;
    border-radius: 50%;
    background-color: orangered;
    filter: blur(6em);
    animation: blink 100ms infinite;
}
/* 为光晕增加闪烁效果 */
@keyframes blink{
    to{
        filter: blur(6em) opacity(0.8);
    }
}

 

上一篇:75道程序员面试逻辑题


下一篇:Numpy 基础