给定一个透明的PNG显示一个简单的形状在白色,它是有可能以某种方式改变这通过CSS的颜色?某种叠加还是什么?
当前回答
如果你只需要一个图标,就不需要整个字体集,而且我觉得它作为一个单独的元素更“干净”。因此,出于这个目的,在HTML5中可以直接在文档流中放置SVG。然后你可以在你的.CSS样式表中定义一个类,并使用fill属性访问它的背景色:
工作小提琴: http://jsfiddle.net/qmsj0ez1/
请注意,在示例中,我使用:hover来说明行为;如果您只是想更改“正常”状态的颜色,则应该删除伪类。
其他回答
我在谷歌上找到了这个,我发现最适合我的工作…
HTML
<div class="img"></div>
CSS
.img {
background-color: red;
width: 60px;
height: 60px;
-webkit-mask-image: url('http://i.stack.imgur.com/gZvK4.png');
}
http://jsfiddle.net/a63b0exm/
在大多数浏览器中,你可以使用过滤器:
在<img>元素和其他元素的背景图像上 并在CSS中静态设置它们,或者使用JavaScript动态设置它们
请看下面的演示。
< img >元素
你可以把这个技巧应用到<img>元素上:
#original, #changed { 宽度:45%; 填充:2.5%; 浮:左; } #{改变 -webkit-filter:色调旋转(180度); 滤镜:色调旋转(180度); } <img id="original" src="http://i.stack.imgur.com/rfar2.jpg" /> <img id="changed" src="http://i.stack.imgur.com/rfar2.jpg" />
背景图片
你可以把这个技巧应用到背景图像上:
#original, #changed { 背景:url (http://i.stack.imgur.com/kaKzj.jpg); background-size:封面; 宽度:30%; 利润率:0 10% 0 10%; padding-bottom: 28%; 浮:左; } #{改变 -webkit-filter:色调旋转(180度); 滤镜:色调旋转(180度); } < div id = "原始" > < / div > < div id = "改变" > < / div >
JavaScript
你可以使用JavaScript在运行时设置一个过滤器:
var element = document.getElementById("changed"); Var过滤器= '色调-旋转(120度)饱和(2.4)'; 元素。Style ['-webkit-filter'] = filter; 元素。Style ['filter'] = filter; #original, #changed { 利润率:0 10%; 宽度:30%; 浮:左; 背景:url (http://i.stack.imgur.com/856IQ.png); background-size:封面; padding-bottom: 25%; } < div id = "原始" > < / div > < div id = "改变" > < / div >
是的:)
Surfin' Safari - Blog Archive » CSS Masks WebKit now supports alpha masks in CSS. Masks allow you to overlay the content of a box with a pattern that can be used to knock out portions of that box in the final display. In other words, you can clip to complex shapes based off the alpha of an image. [...] We have introduced new properties to provide Web designers with a lot of control over these masks and how they are applied. The new properties are analogous to the background and border-image properties that already exist. -webkit-mask (background) -webkit-mask-attachment (background-attachment) -webkit-mask-clip (background-clip) -webkit-mask-origin (background-origin) -webkit-mask-image (background-image) -webkit-mask-repeat (background-repeat) -webkit-mask-composite (background-composite) -webkit-mask-box-image (border-image)
我需要一个特定的颜色,所以滤镜不适合我。
相反,我创建了一个div,利用CSS多个背景图像和线性梯度函数(它自己创建图像)。如果你使用叠加混合模式,你的实际图像将与生成的“渐变”图像混合,包含你想要的颜色(这里,#BADA55)
.colored-image { background-image: linear-gradient(向右,#BADA55, #BADA55), url("https://i.imgur.com/lYXT8R6.png"); background-blend-mode:覆盖; background-size:包含; 宽度:200 px; 身高:200 px; } < div class = "的" > < / div >
如果图像像矢量图像一样简单,可以转换为SVG并在那里进行CSS更改。 另一种选择是将图像制作成PNG,并将特定的颜色更改为透明(如裁剪),并使用CSS更改背景颜色。