clowncar icon indicating copy to clipboard operation
clowncar copied to clipboard

Using an svg with css backgrounds....as a css background

Open ccbayer opened this issue 12 years ago • 0 comments

Something like this works in IE, but not FF / Chrome, at least in my experimenting.

It's an element whose background is an SVG, which in turn also has the responsive backgrounds which load based on mediaquery.

Setting different background colors in the SVG media queries works, so i know that the SVG is loading.

Is this the same security issues as using inline images?

html:

    <style>
    .svg{
        background-image:url('path/to/svgfile.svg');
        background-size:cover;
        height: 100px;
        width: 100%;
        }
    </style>
    <div class="svg"></div>

svg:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 800" preserveAspectRatio="xMidYMid meet">

  <title>The Clown Car Technique</title>
  <style>
    svg {
    background-size: 100% 100%;
    background-repeat: no-repeat;
    }

    @media screen and (max-width: 400px) {
      svg {
        background-color: blue;
        background-image: url("../images/small.jpg");
      }
    } 

    @media screen and (min-width: 401px) and (max-width: 800px) {
      svg {
        background-color: red;
        background-image: url("../images/medium.jpg");
      }
    }

    @media screen and (min-width: 801px){
      svg {
        background-color: black;
        background-image: url("../images/large.jpg");
      }
    }
  </style>
</svg>

Chrome: chrome

IE 10: ie

ccbayer avatar Aug 01 '13 21:08 ccbayer