我们在网站上有一个很大的应用程序,我们有一些链接,比如说蓝色的就像这个网站上的蓝色链接。现在我想做一些其他的链接,但是用浅一点的颜色。显然,我可以简单地通过在CSS文件中添加十六进制代码来做,但我们的网站让用户决定他们想要的自定义配置文件/网站(如Twitter)的颜色。
所以,我的问题是:我们能减少百分比的颜色吗?
让我们说下面的代码是CSS:
a {
color: blue;
}
a.lighter {
color: -50%; // obviously not correct way, but just an idea
}
OR
a.lighter {
color: blue -50%; // again not correct, but another example of setting color and then reducing it
}
有没有办法将颜色减少一个百分比?
您可以使用RGBa(“a”是alpha透明度),但它还没有得到广泛支持。不过,它将是,所以你现在可以使用它,并添加一个备用方案:
a:link {
color: rgb(0,0,255);
}
a:link.lighter {
color: rgb(128,128,255); /* This gets applied only in browsers that don't apply the rgba line */
}
a:link.lighter { /* This comes after the previous line, so has priority in supporting browsers */
color: rgba(0,0,255,0.5); /* last value is transparency */
}
自2020年1月以来,所有现代浏览器都支持100%的过滤器。甚至UC浏览器的Android(而不是Chrome,在80美元的手机上)也支持它。
a {
/* a nice, modern blue for links */
color: #118bee;
}
a:active {
/* Darken on click by 15% (down to 85%) */
filter: brightness(0.85);
}
此外,你可以用CSS变量动态控制这一点,自2017年10月以来,大多数浏览器(不包括QQ)都支持CSS变量:
:root {
--color: #118bee;
--hover-brightness: 1.2;
}
a {
color: var(--color);
}
a:active {
/* Darken on click */
filter: brightness(var(--hover-brightness));
}
不是我的项目,但有一个很好的例子,可以看到现代CSS是多么伟大,看看:MVP.css
原来的答案
如果你使用的堆栈允许你使用Sass或Less,你可以使用点亮函数:
$linkcolour: #0000FF;
a {
color: $linkcolour;
}
a.lighter {
color: lighten($linkcolour, 50%);
}
还有一种暗色调,效果相同,但方向相反。
这是一个老问题,但大多数答案都需要使用预处理器或JavaScript来操作,否则它们不仅会影响元素的颜色,还会影响其中包含的元素的颜色。我将添加一种复杂的css方式来做同样的事情。也许在未来,随着更高级的CSS功能,这将更容易做到。
这个想法是基于使用:
RGB颜色,所以你有单独的值红,绿,蓝;
CSS变量,用来存储颜色值和暗度;而且
calc函数,以应用更改。
默认情况下,暗度为1(对于100%,常规颜色),如果您乘以0到1之间的数字,您将使颜色变深。例如,如果您将每个值乘以0.85,您将使颜色暗15%(100% - 15% = 85% = 0.85)。
下面是一个例子,我创建了三个类:.dark, .dark和.darkest,这将使原始颜色分别暗25%,50%和75%:
html {
--clarity: 1;
}
div {
display: inline-block;
height: 100px;
width: 100px;
line-height: 100px;
color: white;
text-align: center;
font-family: arial, sans-serif;
font-size: 20px;
background: rgba(
calc(var(--r) * var(--clarity)),
calc(var(--g) * var(--clarity)),
calc(var(--b) * var(--clarity))
);
}
.dark { --clarity: 0.75; }
.darker { --clarity: 0.50; }
.darkest { --clarity: 0.25; }
.red {
--r: 255;
--g: 0;
--b: 0;
}
.purple {
--r: 205;
--g: 30;
--b: 205;
}
<div class="red">0%</div>
<div class="red dark">25%</div>
<div class="red darker">50%</div>
<div class="red darkest">75%</div>
<br/><br/>
<div class="purple">0%</div>
<div class="purple dark">25%</div>
<div class="purple darker">50%</div>
<div class="purple darkest">75%</div>
我知道已经很晚了,但是,你可以使用一个包装器来改变你的按钮和rgba颜色函数的不透明度级别,正如在其他答案中所说,但没有明确的例子。
这是一支笔:
https://codepen.io/aronkof/pen/WzGmjR
#wrapper {
width: 50vw;
height: 50vh;
background-color: #AAA;
margin: 20px auto;
border-radius: 5px;
display: grid;
place-items: center;
}
.btn-wrap {
background-color: #000;
display: inline-block;
}
button {
transition: all 0.6s linear;
background-color: rgba(0, 255, 0, 1);
border: none;
outline: none;
color: #fff;
padding: 50px;
font-weight: 700;
font-size: 2em;
}
button:hover {
background-color: rgba(0, 255, 0, .5);
}
<div id="wrapper">
<div class="btn-wrap">
<button class="btn">HEY!</buutton>
</div>
</div>