CSS 中的内联 SVG [英] Inline SVG in CSS

查看:29
本文介绍了CSS 中的内联 SVG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 CSS 中使用内联 SVG 定义?

Is it possible to use an inline SVG definition in CSS?

我的意思是:

.my-class {
  background-image: <svg>...</svg>;
}

推荐答案

是的,这是可能的.试试这个:

Yes, it is possible. Try this:

body { background-image: 
        url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'><linearGradient id='gradient'><stop offset='10%' stop-color='%23F00'/><stop offset='90%' stop-color='%23fcc'/> </linearGradient><rect fill='url(%23gradient)' x='0' y='0' width='100%' height='100%'/></svg>");
      }

  • http://jsfiddle.net/6WAtQ/
  • (请注意,SVG 内容需要进行 url-escaped 才能工作,例如 # 被替换为 %23.)

    (Note that the SVG content needs to be url-escaped for this to work, e.g. # gets replaced with %23.)

    这适用于 IE 9(支持 SVG).数据 URL 也适用于旧版本的 IE(有限制),但它们本身不支持 SVG.

    This works in IE 9 (which supports SVG). Data-URLs work in older versions of IE too (with limitations), but they don’t natively support SVG.

    这篇关于CSS 中的内联 SVG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆