html - 根据百分比渐变确定颜色
问题描述
我正在寻找一种基于渐变百分比确定纯色的方法。基本上像下图
我已经使用 CSS 来获取渐变,但我无法弄清楚如何从该渐变中选择纯色。这是渐变的 CSS:
background-image: linear-gradient(to bottom, #00b050 20%, #4ac148 40%, #74d13e, #9de031, #c6ee21, #dbe508, #eedb00, #ffd000, #ffad00, #ff8700, #ff5b00, #ff0000);
最坏的情况是,我可以在该渐变上分配 0-100 的 RGB 值并使用它。但是,我不禁认为必须有更好的编程方式来解决这个问题。
任何帮助都会很有用。
解决方案
一个想法是使渐变非常大,然后将其调整background-position
为具有一种颜色或至少近似它:
.box {
background-image: linear-gradient(to right, #00b050 20%, #4ac148 40%, #74d13e, #9de031, #c6ee21, #dbe508, #eedb00, #ffd000, #ffad00, #ff8700, #ff5b00, #ff0000);
background-size:10000% 100%;
border:10px solid;
width:50px;
height:100px;
display:inline-block;
}
<div class="box" style="background-position:20% 0"></div>
<div class="box" style="background-position:40% 0"></div>
<div class="box" style="background-position:50% 0"></div>
<div class="box" style="background-position:60% 0"></div>
<div class="box" style="background-position:70% 0"></div>
<div class="box" style="background-position:80% 0"></div>
<div class="box" style="background-position:100% 0"></div>
推荐阅读
- continuous-integration - Gitlab-ci子管道同名
- elasticsearch - ES多次必须查询返回错误结果
- node.js - 如何使用 cookie-session 和 passport.js 在注册时启动会话?
- python - 尝试将唯一的 ASCII 文件导入熊猫数据帧时出现解析错误
- php - 10K 次相同 URL 的 PHP CURL 请求
- c# - 文件上传过快
- javascript - PostgreSQL (pgAdmin) 和跨站点 cookie 警告
- java - 将数据库调用放在自定义 ConstraintValidator 的“isvalid()”方法中会导致 stackoverflow 错误,因为它被重复调用
- awk - 无法使用 awk 从文件中提取列范围
- kotlin - 在 kotlin 单元测试中创建 MKMapView 时崩溃