Generate random color with pure CSS (no javascript)?

后端 未结 6 1912
终归单人心
终归单人心 2021-02-06 23:57

Is it possible to generate random color with pure CSS and without using javascript? It\'s probably impossible but I\'m still curious.

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-07 00:16

    Not exactly random but with CSS:

    Step 1 - Select queried backgrounds, order them in sequence and adjust the frequency

        @keyframes bgrandom {
        0% { background: linear-gradient(90deg, rgba(255,255,255,0.98) 50%, rgba(255,255,255,0.96) 0%); }
        50% { background: linear-gradient(90deg, rgba(255,255,255,0.98) 50%, rgba(255,255,255,0.96) 0%); }
    
        55% { background: linear-gradient(90deg,  rgba(255,255,255,0.96) 50%, rgba(255,255,255,0.98) 0%); }
        80% { background: linear-gradient(90deg, rgba(255,255,255,0.96)  50%, rgba(255,255,255,0.98) 0%); }
    
        85% { background: linear-gradient(90deg, rgba(255,255,255,0.96) 50%, rgba(255,255,255,0.94) 0%); }
        100% { background: linear-gradient(90deg, rgba(255,255,255,0.96) 50%, rgba(255,255,255,0.94) 0%); }
        }
    

    Step 2 - Launch the animation(length animationName repeatMode)

        #element{  animation: 1.2s bgrandom infinite; }
    

    Easy to use, customize and works great. Similar technique can be used for sliders, spinners, etc.

提交回复
热议问题