Css 如何使用currentColor更改SCS中的SVG颜色
用户如何使用currentColor填充svg?我希望在svg背景中使用当前元素颜色,如下所示:Css 如何使用currentColor更改SCS中的SVG颜色,css,svg,sass,Css,Svg,Sass,用户如何使用currentColor填充svg?我希望在svg背景中使用当前元素颜色,如下所示: .icon { color:bleu; &::after { content: ''; display: inline-block; background-size: contain; background-repeat: no-repeat; background-position: center center; background-im
.icon {
color:bleu;
&::after {
content: '';
display: inline-block;
background-size: contain;
background-repeat: no-repeat;
background-position: center center;
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19 15" fill="currentColor"> <polygon points="9 14 1 14 1 1 9 1 9 4.5 10 4.5 10 0 0 0 0 15 10 15 10 10.5 9 10.5 9 14"></polygon> <polygon points="14.708 3.146 14.011 3.854 17.113 7 4.5 7 4.5 8 17.113 8 14.011 11.146 14.708 11.854 19 7.5 14.708 3.146"></polygon> </svg>');
width: 19px;
height: 15px;
margin-left: 14px;
position: absolute; // hide underline on hover
top: 2px;
}
&:focus {
&::after {
position: inherit; // show border on focus and save the top position
}
}
&:hover {
color:red;
&::after {
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19 15" fill="currentColor"> <polygon points="9 14 1 14 1 1 9 1 9 4.5 10 4.5 10 0 0 0 0 15 10 15 10 10.5 9 10.5 9 14"></polygon> <polygon points="14.708 3.146 14.011 3.854 17.113 7 4.5 7 4.5 8 17.113 8 14.011 11.146 14.708 11.854 19 7.5 14.708 3.146"></polygon> </svg>');
}
}
}
有什么想法吗?使用像这样添加背景图像的url技术为svg创建了一个新的生存环境,有效地使currentColor成为默认的浏览器颜色。所以你不能这样做
但是,您可以执行以下操作:
将SVG填充颜色设置为蓝色填充=蓝色
在悬停中,使用“过滤器:色调旋转”为背景着色
在你的情况下,那就是
.icon {
&::after {
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19 15" fill="blue"> <polygon points="..."></polygon> </svg>');
}
&:hover {
color: red;
&::after {
filter: hue-rotate(120deg);
}
}
}
这将对图标和常规文本应用过滤器。使用url技术添加这样的背景图像,为svg创建一个新的活动上下文,有效地使currentColor成为默认的浏览器颜色。所以你不能这样做
但是,您可以执行以下操作:
将SVG填充颜色设置为蓝色填充=蓝色
在悬停中,使用“过滤器:色调旋转”为背景着色
在你的情况下,那就是
.icon {
&::after {
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19 15" fill="blue"> <polygon points="..."></polygon> </svg>');
}
&:hover {
color: red;
&::after {
filter: hue-rotate(120deg);
}
}
}
这将对图标和常规文本应用过滤器。您可以使用javascript设置背景图像并使用css变量而不是currentColor您可以使用javascript设置背景图像并使用css变量而不是currentColor