我想使用字体字符(例如来自 font-awesome)作为输入元素的背景图像。
<style>
#id {
background-image: content('\f2d3'); /* content instead of url() */
background-position: right center;
}
</style>
<input id="test" />
Run Code Online (Sandbox Code Playgroud)
我的猜测是我要么将角色保存为图像,要么使用伪元素:after与内容和足够的定位来实现这一点。只是想看看是否有更好的解决方案。是否可以访问 SVG 中的字符并使用内联 SVG 作为内容?
我用 SVG 做了一个部分解决方案(参见https://fiddle.jshell.net/92h52e65/4/),但仍然存在使用正确字体的问题。
<style>
#input1 {
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="30px" height="20px"><text x="5" y="15" font-family="FontAwesome">x</text></svg>');
background-position: right center;
background-repeat: no-repeat no-repeat;
}
</style>
<input id="input1" placeholder="insert some text here" size="30" required />
Run Code Online (Sandbox Code Playgroud)
为了在 IE 和 FF 中工作,我必须使用 base64 编码而不是 utf8。我把 utf8 留在这里是为了让它更具可读性。