img src SVG 用 CSS 改变样式

2022-01-30 00:00:00 image svg css

html

<img src="logo.svg" alt="Logo" class="logo-img">

css

.logo-img path {
  fill: #000;
}

上面的 svg 加载并且是原生的 fill: #fff 但是当我使用上面的 css 尝试将其更改为黑色时它不会改变,这是我的第一次玩 SVG,我不知道为什么它不起作用.

The above svg loads and is natively fill: #fff but when I use the above css to try change it to black it doesn't change, this is my first time playing with SVG and I am not sure why it's not working.

推荐答案

您可以将 SVG 设置为蒙版.这样设置背景颜色将充当您的填充颜色.

You could set your SVG as a mask. That way setting a background-color would act as your fill color.

HTML

<div class="logo"></div>

CSS

.logo {
  background-color: red;
  -webkit-mask: url(logo.svg) no-repeat center;
  mask: url(logo.svg) no-repeat center;
}

JSFiddle: https://jsfiddle.net/KuhlTime/2j8exgcb/
MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/mask

请检查您的浏览器是否支持此功能:https://caniuse.com/#search=mask

Please check whether your browser supports this feature: https://caniuse.com/#search=mask

相关文章