如何在css中将svg数据图像作为div背景?如果在html <img src="data:image/svg+xml;base64,[data]">
中它可以正常工作但作为css背景它不会显示。我在代码笔上创建了一个样本,你可以看到。所以我正在调查这个问题,发现其他一些svg数据图像工作正常。
.frame__wrapper {
background: transparent url('data:image/svg+xml;base64,[data]') center top no-repeat;
background-size: 100% auto;
width: 310px;
height: 272px;
margin: 0 auto;
padding: 26px 10px;
}
codepen working sample with svg image 我不能在这里粘贴编码的svg。
使用来自此tuttorial svg data uris的数据uri我设法设置了css backgkround。所以base64还没有显示出来。
答案 0 :(得分:2)
我的步骤如何在css中使用svg数据图像作为背景:
1)我将图像编码为base64
2)我将这个编码图像嵌入到svg标记中
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="your width" height="your height" viewBox="0 0 your width your height">
<image xlink:href="data:image/jpeg;base64,[data]" x="0" y="0" height="100%" width="100%"/>
</svg>
3)然后我在这里uri编码所有这些东西SVG encoder
4)最后,我将结果放入我的背景属性data:image/svg+xml;charset=utf-8,[data]
在我的情况下,我需要一个过滤器应用于背景图像,但如果你不需要它,它是好的:)它也不能在IE中工作。希望这些信息有所帮助。
div {
width: 100px;
height: 100px;
background: url(data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20xmlns%3Axlink%3D%22http%3A//www.w3.org/1999/xlink%22%20width%3D%22100%22%20height%3D%22100%22%20viewBox%3D%220%200%20100%20100%22%3E%0A%20%20%3Cimage%20xlink%3Ahref%3D%22data%3Aimage/jpeg%3Bbase64%2C/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCABk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