gpt4 book ai didi

html - 灰度 (100%) 容器内的彩色图像

转载 作者:行者123 更新时间:2023-11-28 04:48:53 26 4
gpt4 key购买 nike

我有一个带有背景图像的 div,我正在使用过滤器以黑白方式显示它 (filter: grayscale(100%);)。我现在正试图在该 div 中放置一个颜色图标。尝试将图标设置为 grayscale(0%),但这似乎不起作用。

这是一个例子:

#a1, #a2 {
background-image: url(http://ndrichardson.com/blog/wp-content/uploads/2012/04/colorpsychology.jpg);
background-repeat: no-repeat;
background-size: 100% 100%;
}
#a1 {
height: 250px;
width: 250px;
-webkit-filter: grayscale(100%);
filter: grayscale(100%);
position: relative;
}
#a2 {
height: 50px;
width: 50px;
-webkit-filter: grayscale(0%);
filter: grayscale(0%);
position: absolute;
top: 0px;
right: 0px;
}
<div id="a1"><!-- << this should be black and white -->
<div id="a2"><!-- << this should be color -->
</div>
</div>

有没有办法在不创建额外的 div 来保存背景图像的情况下做到这一点?真正的代码要复杂得多,这就是为什么我想尽可能避免使用额外的 div。

最佳答案

当然可以,你应该使用 ::before::after 伪元素:

#a1 {
height: 250px;
width: 250px;
position: relative;
}
#a1:after, #a1:before {
content: '';
background-image: url(http://ndrichardson.com/blog/wp-content/uploads/2012/04/colorpsychology.jpg);
background-repeat: no-repeat;
background-size: 100% 100%;
position: absolute;
}
#a1:before {
-webkit-filter: grayscale(100%);
filter: grayscale(100%);
width: 100%;
height: 100%;
}
#a1:after {
height: 50px;
width: 50px;
top: 0px;
right: 0px;
}
<div id="a1"></div>

DEMO on JSFiddle

关于html - 灰度 (100%) 容器内的彩色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38164663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com