【问题标题】:Why is CSS mask-image not working with no-repeat?为什么 CSS mask-image 不能与 no-repeat 一起使用?
【发布时间】:2019-08-25 04:45:18
【问题描述】:

我有一个 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>

这是一个错误吗?有什么解决办法?

【问题讨论】:

    标签: html css image image-masking


    【解决方案1】:

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

    .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 attribute 速记:

    .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>

    【讨论】:

      猜你喜欢
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 2020-11-15
      • 2011-12-22
      相关资源
      最近更新 更多