【问题标题】:Add a hovering effect on an element with masked or backround SVG Icon在带有蒙版或背景 SVG 图标的元素上添加悬停效果
【发布时间】:2022-06-13 09:47:51
【问题描述】:

如何在用作 SVG 图标的元素上添加悬停效果,该图标也可以更改图标本身的颜色。

这就是我现在所拥有的,两个选项,使用蒙版图像或将图标显示为背景。

在 CSS 中使用background 的问题,悬停时显示正确,但我无法更改图标颜色。

.icon {
    display: inline-block;
    background: url('https://cdn-icons-png.flaticon.com/512/151/151773.png') no-repeat center; /* imagine the file here as an SVG file */
    background-size: 100px 100px !important;
    height: 100px;
    width: 100px;
}

.icon_interactive:hover {
    cursor: pointer;
    background-color: white;
    border-radius: 50%;
    padding: 25px;
}
<div style="padding: 100px; background-color: gray; text-align: center;">
  <span class="icon icon_interactive"></span>
</div>

在 CSS 中使用mask-image 的问题,悬停时显示不正确,但我可以更改图标颜色。

.icon {
    -webkit-mask: url('https://cdn-icons-png.flaticon.com/512/151/151773.png');
    -webkit-mask-size: cover !important;
    mask-image: url('https://cdn-icons-png.flaticon.com/512/151/151773.png');
    mask-size: cover !important;
    display: inline-block;
    background-color: black;
    height: 100px;
    width: 100px;
}

.icon_interactive:hover {
    cursor: pointer;
    background-color: white;
    border-radius: 50%;
    padding: 25px;
}
<div style="padding: 100px; background-color: gray; text-align: center;">
  <span class="icon icon_interactive"></span>
</div>

我想要创建的是悬停时的交互式图标,您可以更改图标本身的颜色。

编辑: 总结一下我的问题,我有一个作为图标的蒙版元素,当悬停该元素时,我需要让它具有可控的颜色、大小和背景圆圈。

【问题讨论】:

    标签: html css user-interface svg


    【解决方案1】:

    考虑使用伪元素来结合这两种技巧:

    .icon {
      display: inline-block;
      height: 100px;
      width: 100px;
      border-radius: 50%;
      padding: 25px;
    }
    
    .icon:before {
      content: "";
      display: block;
      height:100%;
      background: #000;
      -webkit-mask: url('https://cdn-icons-png.flaticon.com/512/151/151773.png') center/100px 100px no-repeat;
    }
    
    .icon_interactive:hover {
      cursor: pointer;
      background-color: white;
    }
    .icon_interactive:hover::before {
      background:red;
    }
    
    body {
      background:gray;
    }
    &lt;span class="icon icon_interactive"&gt;&lt;/span&gt;

    【讨论】:

    • 在将它应用到我的代码中后,它突然崩溃了,因为我也在使用类来确定尺寸,而且我也在使用类来应用颜色。这不行
    猜你喜欢
    • 2014-11-10
    • 2013-10-15
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 2015-01-12
    相关资源
    最近更新 更多