html - 为什么 CSS mask-image 不能使用 no-repeat?

标签 html css image image-masking

我有一个 SVG,我想在某些事件中将它的颜色更改为红色,但是你不能用 SVG 作为背景图像来做到这一点,所以你必须使用 CSS image-mask。我正在使用 PHP 将我的 CSS 回显到 div 的样式属性上:

$jfid = "background-color:red;
        -webkit-mask-image:url(../like_icons/" . $iconfgg . ".svg);
         mask-image:url(../like_icons/" . $iconfgg . ".svg)"; 

喜欢

.buttonlikee {
    background: transparent;
    outline: none;
    border: none;
    margin-left: 10px;
    transition: 0.8s all ease
}
.ts{
  width: 34px;
  height: 32px;
  background-color:red;
  -webkit-mask-image:url(https://svgshare.com/i/CB7.svg);
  mask-image:url(https://svgshare.com/i/CB7.svg) 
}
<button class="buttonlikee">
  <div class="ts"></div>
</button>

这按预期工作但返回相同 SVG 的重复图像。所以解决方案是像这样在最后添加no-repeat:

$jfid = "background-color:red;
         -webkit-mask-image:url(../like_icons/" . $iconfgg . ".svg) no-repeat;
         mask-image:url(../like_icons/" . $iconfgg . ".svg) no-repeat"; 

这反过来给了我一个充满红色的 div,你看不到像

.buttonlikee {
    background: transparent;
    outline: none;
    border: none;
    margin-left: 10px;
    transition: 0.8s all ease
}
.ts{
  width: 34px;
  height: 32px;
  background-color:red;
  -webkit-mask-image:url(https://svgshare.com/i/CB7.svg) no-repeat;
  mask-image:url(https://svgshare.com/i/CB7.svg) no-repeat
}
<button class="buttonlikee">
  <div class="ts"></div>
</button>

这是一个错误吗?可能的解决方案是什么?

最佳答案

no-repeat 不是 mask-image 属性的有效命令,如 the documentation 中所示.相反,您应该使用 mask-repeat property像这样:

.buttonlikee {
    background: transparent;
    outline: none;
    border: none;
    margin-left: 10px;
    transition: 0.8s all ease
}
.ts {
  width: 34px;
  height: 32px;
  background-color:red;
  -webkit-mask-image: url(https://svgshare.com/i/CB7.svg);
  mask-image: url(https://svgshare.com/i/CB7.svg);
  -webkit-mask-repeat: no-repeat;
  mask-repeat: no-repeat;
}
<button class="buttonlikee">
  <div class="ts"></div>
</button>

否则你可以使用 mask property简写:

.buttonlikee {
    background: transparent;
    outline: none;
    border: none;
    margin-left: 10px;
    transition: 0.8s all ease
}
.ts {
  width: 34px;
  height: 32px;
  background-color:red;
  -webkit-mask: url(https://svgshare.com/i/CB7.svg) no-repeat;
  mask: url(https://svgshare.com/i/CB7.svg) no-repeat;
}
<button class="buttonlikee">
  <div class="ts"></div>
</button>

关于html - 为什么 CSS mask-image 不能使用 no-repeat?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55498806/

相关文章:

javascript - 如何加载 leanmodal div 内容 onclick

javascript - 传递正确的参数

javascript - Dropzone.js - 在多个文件上传时仅多次上传 1 个文件

html - IE不支持base标签吗?

javascript - 使用 jQuery slider 通过串行发送数据

javascript - 按下回车键不会在 Mozilla 中打开下拉菜单

javascript - 如何根据 viewbox 或 svg 标签的高度宽度缩放绘图

c++ - C/C++ 中的图像缩放和旋转

javascript - 动态javascript图像变化

c++ - qt creator 无法加载图标