HTML
<img src="logo.svg" alt="Logo" class="logo-img">
层叠样式表(CSS)
.logo-img path {
fill: #000;
}
以上SVG加载并且本地的 fill:#fff
,但是当我使用上面的 CSS
尝试将其更改为黑色时,它没有改变,这是我第一次操作SVG,不确定为什么它不起作用。
HTML
<img src="logo.svg" alt="Logo" class="logo-img">
层叠样式表(CSS)
.logo-img path {
fill: #000;
}
以上SVG加载并且本地的 fill:#fff
,但是当我使用上面的 CSS
尝试将其更改为黑色时,它没有改变,这是我第一次操作SVG,不确定为什么它不起作用。
<div class="logo"></div>
.logo {
background-color: red;
-webkit-mask: url(logo.svg) no-repeat center;
mask: url(logo.svg) no-repeat center;
}
div
内添加了 <img src="logo.svg" style="opacity:0" />
以获得正确的 div
尺寸。 - Shanimal尝试纯CSS:
.logo-img {
/* to black */
filter: invert(1);
/* or to blue */
filter: invert(0.5) sepia(1) saturate(5) hue-rotate(175deg);
}
查看本文详细信息:https://blog.union.io/code/2017/08/10/img-svg-fill/
logo.svg
。
2. 查找fill: #fff
并将其替换为fill: #000
。logo.svg
时,它可能会看起来像这样:<svg fill="#000000" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M0 0h24v24H0z" fill="none"/>
<path d="M1 21h22L12 2 1 21zm12-3h-2v-2h2v2zm0-4h-2v-4h2v4z" fill="#fff"/>
</svg>
...只需更改填充颜色并保存即可。
path
或circle
项中,例如<path fill="#777777" d="m129.774,74.409c-5.523,...
。 - SaeXfill="currentColor"
,然后在父元素上使用color
。参考链接:https://css-tricks.com/cascading-svg-fill-color/ - serge<svg>
<use href="logo.svg" style="--color_fill: #000;"></use>
</svg>
在您的SVG中,将任何出现 style="fill: #000"
的地方替换为 style="fill: var(--color_fill)"
。
xlink:href
已经被 href
取代,所以这个应该仍然可以工作。 - Benjamin Intalhref
似乎对我不起作用。 - Ben Winding编辑您的SVG文件,将 fill="currentColor"
添加到svg标记中,并确保从文件中删除任何其他填充属性。
例如:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 139.435269383854" id="img" fill="currentColor">...
</svg>
需要注意的是,currentColor
是一个关键字(而不是在使用中的固定颜色)。
之后,您可以通过设置元素或其父元素的 color
属性来使用 CSS 修改颜色。
示例:
.div-with-svg-inside {
color: red;
}
我忘了说,你必须以这种方式插入SVG:
<svg>
<use xlink:href='/assets/file.svg#img' href="/assets/file.svg#img"></use>
</svg>
如果图像来自某个变量,则
<svg>
<use [attr.xlink:href]="somevariable + '#img'" [attr.href]="somevariable + '#img'"></use>
</svg>
Note that `#img` is the id of the `svg` tag inside svg file. Also note `xlink:href` has been deprecated instead you should use `href` or use can use both to support older browser versions.
Another way of doing it: [https://css-tricks.com/cascading-svg-fill-color/][1]
[1]: https://css-tricks.com/cascading-svg-fill-color/
xlink:href
是一个已经被弃用的功能,不应该被使用。https://developer.mozilla.org/zh-CN/docs/Web/SVG/Attribute/xlink:href - kano<svg viewBox="0 0 200 200"> <use fill="blue" href="https://cdn.jsdelivr.net/gh/instanano/apparatus/test111.svg#test111"> </use></svg>
- Dheeraj Kumar建议选择您的颜色,并转到此代码片段https://codepen.io/sosuke/pen/Pjoqqp,它可以将 HEX 转换为 CSS 滤镜,例如:#64D7D6
等同于:
filter: invert(88%) sepia(21%) saturate(935%) hue-rotate(123deg) brightness(85%) contrast(97%);
最终片段
.filterit{
width:270px;
filter: invert(88%) sepia(21%) saturate(935%) hue-rotate(123deg) brightness(85%) contrast(97%);
}
<img src="https://www.flaticon.com/svg/static/icons/svg/1389/1389029.svg"
class="filterit
/>
您需要首先将SVG注入到HTML DOM中。
有一个名为SVGInject的开源库可以为您完成此操作。它使用onload
属性触发注入。
以下是使用SVGInject的最简示例:
<html>
<head>
<script src="svg-inject.min.js"></script>
</head>
<body>
<img src="image.svg" onload="SVGInject(this)" />
</body>
</html>
图片加载完成后,onload="SVGInject(this)"
会触发注入过程,<img>
元素将被替换为src
属性提供的SVG文件内容。
它解决了几个与SVG注入相关的问题:
SVG可以在注入完成之前隐藏。如果在加载期间已经应用了样式,否则会导致短暂的“未成样式的内容闪烁”,这一点很重要。
<img>
元素会自动注入。如果您动态添加SVG,则无需担心再次调用注入函数。
每个ID中都添加一个随机字符串,避免了在文档中多次注入相同的ID,如果同一个SVG文件被注入多次则尤为重要。
SVGInject是纯JavaScript编写的,适用于支持SVG的所有浏览器。
免责声明:我是SVGInject的联合作者
使用滤镜转换为任何颜色。
我最近发现了这个解决方案,希望有人能够使用它。 由于该解决方案使用了滤镜,因此可以与任何类型的图像一起使用,不仅限于 svg。
如果您有一个单色图像,只想更改其颜色,您可以使用一些滤镜来实现。当然,它也适用于多色图像,但您无法针对特定颜色进行操作,只能操作整个图像。
这些滤镜来自于在如何使用 CSS 滤镜将黑色转换为任何给定颜色中提出的脚本。如果要将白色更改为任何颜色,则可以调整每个滤镜中的反转值。
.startAsBlack{
display: inline-block;
width: 50px;
height: 50px;
background: black;
}
.black-green{
filter: invert(43%) sepia(96%) saturate(1237%) hue-rotate(88deg) brightness(128%) contrast(119%);
}
.black-red{
filter: invert(37%) sepia(93%) saturate(7471%) hue-rotate(356deg) brightness(91%) contrast(135%);
}
.black-blue{
filter: invert(12%) sepia(83%) saturate(5841%) hue-rotate(244deg) brightness(87%) contrast(153%);
}
.black-purple{
filter: invert(18%) sepia(98%) saturate(2657%) hue-rotate(289deg) brightness(121%) contrast(140%);
}
Black to any color: <br/>
<div class="startAsBlack black-green"></div>
<div class="startAsBlack black-red"></div>
<div class="startAsBlack black-blue"></div>
<div class="startAsBlack black-purple"></div>
filter
很有效!如果你有 HEX 颜色 并想从中生成滤镜颜色,请使用 Codepen 的 CSS 滤镜生成器。生成的滤镜颜色适用于最初为黑色的颜色。如果你有一个最初为白色的颜色,你可以增加生成的滤镜颜色的亮度来达到目标颜色。 - Berkin Sansal本答案基于答案https://stackoverflow.com/a/24933495/3890888,但使用了该脚本的纯JavaScript版本。
您需要将SVG转换为内联SVG。您可以通过向图像添加一个类svg
来使用此脚本:
/*
* Replace all SVG images with inline SVG
*/
document.querySelectorAll('img.svg').forEach(function(img){
var imgID = img.id;
var imgClass = img.className;
var imgURL = img.src;
fetch(imgURL).then(function(response) {
return response.text();
}).then(function(text){
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(text, "text/xml");
// Get the SVG tag, ignore the rest
var svg = xmlDoc.getElementsByTagName('svg')[0];
// Add replaced image's ID to the new SVG
if(typeof imgID !== 'undefined') {
svg.setAttribute('id', imgID);
}
// Add replaced image's classes to the new SVG
if(typeof imgClass !== 'undefined') {
svg.setAttribute('class', imgClass+' replaced-svg');
}
// Remove any invalid XML tags as per http://validator.w3.org
svg.removeAttribute('xmlns:a');
// Check if the viewport is set, if the viewport is not set the SVG wont't scale.
if(!svg.getAttribute('viewBox') && svg.getAttribute('height') && svg.getAttribute('width')) {
svg.setAttribute('viewBox', '0 0 ' + svg.getAttribute('height') + ' ' + svg.getAttribute('width'))
}
// Replace image with new SVG
img.parentNode.replaceChild(svg, img);
});
});
那么,现在如果你执行以下操作:
.logo-img path {
fill: #000;
}
.logo-img path {
background-color: #000;
}
JSFiddle: http://jsfiddle.net/erxu0dzz/1/
parser.parseFromString(text,“image/svg+xml”);
- ChristoKiwifetch
导致的(https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)。jQuery版本可以正常工作。 - mariaines