我有一个按钮设置如下:
<button class="buttonclass"><i class="iconclass plus-icon"></i></button>
Run Code Online (Sandbox Code Playgroud)
我的css类看起来像这样:
.buttonclass {
width: 30px;
height: 30px;
text-align: center;
position: relative;
border-radius: 20px;
background-color: #1DBE60
}
.iconclass {
width: 20px;
height: 20px;
margin: 7.5px;
}
.buttonclass .iconclass {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.plus-icon {
content: url(http://uxrepo.com/static/icon-sets/mfg-labs/svg/plus.svg);
}
Run Code Online (Sandbox Code Playgroud)
如果它是SVG,如何用css更改'plus-icon'的颜色?我已经尝试将填充类添加到svg,颜色类,背景类等.
这是一个插件:http://plnkr.co/edit/6fLYQlpFmDdf7aWenBtp?p = preview
如果您愿意添加一个额外的类(用于加号图标的颜色),那么这里是一个使用 CSScurrentColor变量的纯 CSS 解决方案:
.buttonclass {
width: 30px;
height: 30px;
text-align: center;
position: relative;
border-radius: 20px;
background-color: #1DBE60
}
.iconclass {
width: 20px;
height: 20px;
margin: 7.5px;
}
.buttonclass .iconclass {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.plus-icon {
background-image:url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><rect x="0" y="0" width="8" height="8" fill="rgb(29,190,96)" /><rect x="0" y="12" width="8" height="8" fill="rgb(29,190,96)" /><rect x="12" y="0" width="8" height="8" fill="rgb(29,190,96)" /><rect x="12" y="12" width="8" height="8" fill="rgb(29,190,96)" /></svg>');
background-color: currentColor;
border: 1px solid rgb(29,190,96);
border-radius: 15px;
}
.white {
color: rgb(255,255,255);
}
.yellow {
color: rgb(255,255,0);
}
.green {
color: rgb(0,255,0);
}Run Code Online (Sandbox Code Playgroud)
<button class="buttonclass"><i class="iconclass plus-icon white"></i></button>
<button class="buttonclass"><i class="iconclass plus-icon yellow"></i></button>
<button class="buttonclass"><i class="iconclass plus-icon green"></i></button>Run Code Online (Sandbox Code Playgroud)
您必须将 svg 内联,但考虑使用该<use>元素,这样您就可以通过引用它来多次使用图标:
http://tympanus.net/codrops/2015/07/16/styling-svg-use-content-css/