from green to red color depend on percentage

前端 未结 14 2060
北恋
北恋 2020-11-30 20:00

I have a poll system and I want answers for this poll to be colored. For example: If it\'s 10% it would be red, if 40% it would be yellow and if 80% it would be green, so I

14条回答
  •  情书的邮戳
    2020-11-30 20:34

    I know this is originally an old javascript question, but I got here searching for CSS only solution, so maybe it will help others equally: It's actually quite simple:

    Use the percentage as a HSL color value! Red to Green spans H from 0 to 128.(so you can pump the percentage up by 1.2 if you want). Example:

    background-color:hsl(perc,100%,50%);
    

    Where perc is just the number, without the % sign.

提交回复
热议问题