Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Modernizr to detect whether if SVG CSS backgrounds are supported?

Tags:

css

modernizr

As the question says how do I use Modernizr to detect whether if SVG CSS backgrounds are supported?

.svg #example{} is not the correct way because different browsers have varied support for svg files.

For example Modernizr reports SVG to be supported in firefox 3.5 however SVG files and CSS as background images ARE NOT supported.

like image 922
Malcr001 Avatar asked Dec 19 '12 17:12

Malcr001


2 Answers

How about a pure CSS solution? I can confirm this works with IE8.

E {
background-image:  url('image.png');
background-image:  none, url('image.svg'), url('image.png');
background-size: 100% 100%;
}

http://www.broken-links.com/2010/06/14/using-svg-in-backgrounds-with-png-fallback/

Or try this other method:

E {
background: transparent url(fallback-image.png) center center no-repeat;
background-image: linear-gradient(transparent, transparent), url(vector-image.svg);
}

http://pauginer.tumblr.com/post/36614680636/invisible-gradient-technique

like image 84
cimmanon Avatar answered Nov 16 '22 03:11

cimmanon


One option is to use the Inline SVG option in modernizer. I can confirm this works in FF 3.6.14. You'd choose "Inline SVG" as part of your modernizer build, and can manage it in CSS like:

.logo {
    background: url(mahimage.svg);
    ...
}

.no-inlinesvg .logo {
    background: url(mahimage.png);
    ...
}

or in javascript like:

if (Modernizr.inlinesvg) {
    ...
}
else {
    ....
}
like image 2
MikeSmithDev Avatar answered Nov 16 '22 03:11

MikeSmithDev