我们在网站上有一个很大的应用程序,我们有一些链接,比如说蓝色的就像这个网站上的蓝色链接。现在我想做一些其他的链接,但是用浅一点的颜色。显然,我可以简单地通过在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
}

有没有办法将颜色减少一个百分比?


当前回答

Try:

a {
  color: hsl(240, 100%, 50%);
}

a:hover {
  color: hsl(240, 100%, 70%);
}

其他回答

我正在使用原始CSS3和SVG添加一个答案,不需要LESS或SASS。

基本上,如果问题是为了全局悬停效果而将颜色调亮10%、25%、50%或更暗,你可以像这样创建一个SVG数据调用

:root{
--lighten-bg: url('data:image/svg+xml;utf8,<svg version="1.1" id="cssLighten" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="50px" height="50px" viewBox="0 0 50 50" enable-background="new 0 0 50 50" xml:space="preserve"><rect opacity="0.2" fill="white" width="50" height="50"/></svg>') !important;
--darken-bg: url('data:image/svg+xml;utf8,<svg version="1.1" id="cssDarken" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="50px" height="50px" viewBox="0 0 50 50" enable-background="new 0 0 50 50" xml:space="preserve"><rect opacity="0.2" fill="black" width="50" height="50"/></svg>') !important;

}

.myButton{
    color: white;
    background-color:blue;
}
.myButton:hover{
    background-image: var(--lighten-bg);
}

出于某种原因,我不知道,SVG不允许我在“填充”属性中输入十六进制值,但“白色”和“黑色”满足了我的需求。

发人深思: 如果你不介意使用图片,可以使用50%透明的PNG作为背景图片。如果您想要更花哨,可以调用一个PHP脚本作为背景图像,并将HEX和不透明度值传递给它,然后让它输出上面的SVG代码。

请看我对Ctford回复的评论。

我认为使颜色变亮的简单方法是将每个RGB组件加到0xff并除以2。如果这不能给出你想要的确切结果,用0xff减去当前值乘以某个常数,然后加回当前值。例如,如果你想向白色方向移动1/3,取(0xff - current)/3+current。

你得先试一下,看看结果如何。我担心,根据这个简单的公式,一个大到足以使深色颜色很好地褪色的因素可能会使浅色颜色完全变白,而一个小到足以使浅色颜色只变亮一点的因素可能会使深色颜色不够亮。

尽管如此,我还是认为,与固定步数相比,向白色移动一小部分距离更有希望。

下面的代码可以让颜色变暗——只需要改变悬停的50%

.button {
  display: inline-block;
  background-color: green;
}

.button:hover {
  background-image: linear-gradient(rgb(0 0 0/50%) 0 0);
}

如果你只需要改变背景颜色,这是一个很好的方法,是万无一失的-在背景图像上使用线性梯度方法!

看看下面的例子:

document .getElementById('colorpicker') .addEventListener('change', function(event) { document .documentElement .style.setProperty('--color', event.target.value); }); span { display: inline-block; border-radius: 20px; height: 40px; width: 40px; vertical-align: middle; } .red { background-color: red; } .red-darker { background: linear-gradient( to top, rgba(0, 0, 0, 0.25), rgba(0, 0, 0, 0.25) ) red; } :root { --color: lime; } .dynamic-color { background-color: var(--color); } .dynamic-color-darker { background: linear-gradient( to top, rgba(0, 0, 0, 0.25), rgba(0, 0, 0, 0.25) ) var(--color); } <table> <tr> <td><strong>Static Color</strong></td> <td><span class="red"></span></td> <td><span class="red-darker"></span></td> </tr> <tr> <td><strong>Dynamic Color</strong></td> <td><span class="dynamic-color"></span></td> <td><span class="dynamic-color-darker"></span></td> </tr> </table> <br/> Change the dynamic color: <input id="colorpicker" value="#00ff00" type="color"/>

学分:https://css-tricks.com/css-custom-properties-theming/

不直接,不。但是你可以使用一个网站,比如colorschemedesigner.com,它会给你基本颜色,然后给你不同范围的基本颜色的十六进制和rgb代码。

一旦我找到了我的网站配色方案,我就把颜色的十六进制代码放在样式表顶部的评论部分中并命名它们。

其他一些配色方案生成器包括:

http://www.colorschemer.com/online.html http://colorschemegenerator.com/ http://www.cssjuice.com/25-popular-color-scheme-and-palette-generators/