#Inline SVG handled via PHP with png background image fallback
In looking at all the methods for implementing svg with fallback I just wanted to make note of this method. I'm not sure if i will ultimately use it but i wanted to make note of it so i can revisit.
Their are multiple ways of adding your svg files. For the purposes of my testing i was interested in directly inlining the svg. I was interested in a way of adding the svg that reduced requests and allowed for direct manipulation of the svg file through css. Plus i could have php do all the work using file_get_contents()
.
The inline <svg>
image is wraped in a <span>
element with a class that we can use to target for the .png background-image
fallback. This allso adds some additional support for older browsers that wont render the <svg>
element at all. I'm using a little bit of JS from Todd Motto to ditect if SVG is supported and return a class of .svg
or .no-svg
to the html element. When .no-svg
is present the background image is applied and the png takes the place of the svg file.
That svg support script needs to be added before any of the css files in the head.
But frankly if your not bothered with supporting anything bellow EI9 then i might just go with mutliple background images instead. I loose the ability to manuipulate the svg via css but i'm not sure if thats a deal breaker.