【发布时间】:2020-06-10 21:17:27
【问题描述】:
如何使用 currentColor 填充 svg?我想像这样在 svg 背景中使用当前元素颜色:
.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>');
}
}
}
有什么想法吗?
【问题讨论】:
-
你可以使用javascript来设置背景图片并使用css vars而不是currentColor