{"id":199370,"date":"2015-05-04T11:10:08","date_gmt":"2015-05-04T18:10:08","guid":{"rendered":"http:\/\/css-tricks.com\/?p=199370"},"modified":"2020-12-28T15:30:30","modified_gmt":"2020-12-28T23:30:30","slug":"a-complete-guide-to-svg-fallbacks","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/a-complete-guide-to-svg-fallbacks\/","title":{"rendered":"A Complete Guide to SVG Fallbacks"},"content":{"rendered":"\n
Here at CSS-Tricks, there’s a lot of information telling you how<\/a> wonderful<\/a> SVG<\/a> is<\/a>. And as much as we want to convince you that SVG is for Everybody<\/a>, SVG isn’t as widely used as we would like. In fact, some people still (literally) don’t get SVG.<\/p>\n\n\n\n\n\n\n\n Maybe they are stuck on an office computer that hasn’t been updated since the IT guy quit six years ago, maybe they’re using a second-hand refurbished phone they can’t afford to upgrade, or maybe they just don’t understand or don’t care about updating to a new browser. Whatever the reason, approximately 5% of users browsing the web are doing so with web browsers that can’t display SVG \u2014 more in the USA (according to caniuse data<\/a>).<\/p>\n\n\n\n Some tech-oriented web sites (like this one) can afford to say that they only support modern web browsers. But for most sites, you can’t ignore 1 in 20 potential customers. If you want to give the 95% of users on modern browsers all the benefits of SVG, but still provide a functional experience for the others, you need a fallback plan.<\/p>\n\n\n\n The guide is written by Amelia Bellamy-Royds<\/a> and me, Chris Coyier. Amelia and I presented at the same conference together. We both covered SVG, yet neither of us SVG fallbacks comprehensively. It’s such a huge topic, after all. While I’ve covered SVG fallbacks<\/a> before, it’s been a few years and we figured we could do that subject better justice now. Here we go!<\/p>\n\n\n\n Before getting into the technical options for implementing fallbacks, it helps to stop and think of what kind<\/strong> of fallback you need:<\/p>\n\n\n\n On the interactive front, your options are limited. You could convert the SVG to Flash<\/a> and then rewrite all your interaction code in ActionScript. Or you could use Rapha\u00ebl<\/a> to both draw and manipulate the graphic. It provides a single JavaScript interface to both SVG and the VML vector graphics supported in Internet Explorer 6-8. Which means that Rapha\u00ebl doesn’t help with old mobile browsers, but it does cut down the number of unsupported users significantly.<\/p>\n\n\n\n The rest of this post covers the ways to create text and image fallbacks for your SVG. Your options will depend almost entirely on how you’re including that SVG in the web page in the first place: as an embedded object, as inline SVG code, as an image in the HTML, or as an image in the CSS.<\/p>\n\n\n<\/details>\n\n\n SVG can be used like this:<\/p>\n\n\n\n Here are some options for browsers that don’t support that.<\/p>\n\n\n\n Even though this test is supposed to test<\/a> for the \n
What KIND of fallback do you need?<\/h3>\n <\/summary>\n \n\n
\n\n\n\n\n
Fallback for SVG as
<img><\/code><\/h3>\n <\/summary>\n \n\n
<img src=\"image.svg\" alt=\"\"><\/code><\/pre>\n\n\n\n
\n How to Test for Support <\/summary>\n \n\n
<image><\/code> element within SVG, thanks to some testing<\/a> we\u2019ve proven it works for SVG used as the source for
<img><\/code> elements as well. It\u2019s this easy<\/strong>:<\/p>\n\n\n\n
function svgasimg() {\n return document.implementation.hasFeature(\"http:\/\/www.w3.org\/TR\/SVG11\/feature#Image\", \"1.1\");\n}<\/code><\/pre>\n\n\n<\/details>\n\n\n