【发布时间】:2019-01-31 17:08:27
【问题描述】:
我在 ::before 元素中有 SVG 背景图像:https://codepen.io/sshiling/pen/PVWgdW
我想在悬停、焦点、活动时更改它的颜色。我尝试使用这种方法:
:hover::路径前 {
填充:#000;
}
但它不起作用
我可以复制我的内联 SVG 并手动更改它的颜色,但也许有一种方法可以使用“路径填充”或类似的东西来更改它。
HTML
<div class="field">
<div class="field__search"></div>
</div>
CSS
.field {
width: 100px;
height: 100px;
display: flex;
justify-content: center;
align-items: center;
background-color: #849d8f;
}
.field__search {
width: 35px;
height: 35px;
background-color: rgba(255, 255, 255, 0.2);
border-radius: 66px;
position: relative;
}
.field__search::before {
content: "";
position: absolute;
top: 8px;
left: 8px;
width: 22px;
height: 22px;
background-image: url("data:image/svg+xml;utf8, <svg xmlns='http://www.w3.org/2000/svg' width='16.687' height='16.688' viewBox='0 0 16.687 16.688'><defs><style>.cls-1 {fill: #fff;fill-rule: evenodd;}</style></defs><path id='Лупа' class='cls-1' d='M931.155,40.747H930.4l-0.264-.261a6.212,6.212,0,1,0-.675.675l0.263,0.262v0.755l4.773,4.76,1.423-1.422Zm-5.731,0a4.291,4.291,0,1,1,4.3-4.291A4.294,4.294,0,0,1,925.424,40.747Z' transform='translate(-919.219 -30.25)'/></svg>");
background-repeat: no-repeat;
background-position: 0 0;
opacity: 1;
}
.field__search:hover::before path,
.field__search:focus::before path,
.field__search:active::before path {
fill: #000;
}
【问题讨论】: