Is there a way to use use text as the background with CSS?

前端 未结 8 711
暖寄归人
暖寄归人 2020-11-28 03:48

I would like to use dynamic text as background of certain elements in my tag. Because of this, I can use images (dynamic text). How do I do it with just CSS or JavaScript?

8条回答
  •  被撕碎了的回忆
    2020-11-28 04:20

    Ciro's solution about an SVG Data URI background containing the text is very clever.

    However, it won't work in IE if you just add the plain SVG source to the data URI.

    In order to get around this and make it work in IE9 and up, encode the SVG to base64. This is a great tool.

    So this:

    background:url('data:image/svg+xml;utf8,I love SVG!');
    

    Becomes this:

    background:url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjx0ZXh0IHg9IjUlIiB5PSI1JSIgZm9udC1zaXplPSIzMCIgZmlsbD0icmVkIj5JIGxvdmUgU1ZHITwvdGV4dD48L3N2Zz4=');
    

    Tested and it works in IE9-10-11, WebKit (Chrome 37, Opera 23) and Gecko (Firefox 31).

    http://jsfiddle.net/qapp5dLn/

提交回复
热议问题