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

SVG text background image

body {
    background-image:url("data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" version='1.1' height="50px" width="120px"><text x='0' y='15' fill="red" font-size="20">I love SVG!</text></svg>");
}
<p>I hate SVG!</p><p>I hate SVG!</p><p>I hate SVG!</p><p>I hate SVG!</p>
<p>I hate SVG!</p><p>I hate SVG!</p><p>I hate SVG!</p><p>I hate SVG!</p>

Here is an indented version of the CSS so you can understand better. Note that this does not work, you need to use the single liner SVG from the snippet above instead:

body {
  background-image:url("data:image/svg+xml;utf8,
  <svg xmlns="http://www.w3.org/2000/svg" version='1.1'
       height="50px" width="120px">
    <text x='0' y='15' fill="red" font-size="20">I love SVG!</text>
  </svg>");
}

Not sure how portable this is (works on Firefox 31 and Chrome 36), and it is technically an image… but the source is inline and plain text, and it scales infinitely.

@senectus found that it works better on IE if you base64 encode it: https://stackoverflow.com/a/25593531/895245

Leave a Comment