SVG Background Image with PNG Fallback using Modernizr

In this example Modernizr is used to detect SVG support. CSS is then used to display the image of a bike (PNG or SVG) as the background for a DIV. SVG is a scalable vector graphic, it will always be sharp no matter how it’s re-sized. PNG is a raster graphic, it can become pixelated if re-sized.

bike

Visit the Modernizr download page and select SVG and Add CSS Classes options. Generate the code and download the file.

modernizr-svg-arrow

By adding this Modernizr script to the page we get either the svg or no-svg class name added to the html tag. Here’s how it looks in the console:

modernizr-body-js-svg

We can utilize these classes with CSS selectors to fallback to the PNG image if SVG is not supported.

<!DOCTYPE html>
<html>
<head>
	<title>SVG Background Image with PNG Fallback using Modernizr</title>
	<style type="text/css">
		#bike {
			width: 792px;
			height: 612px;
		}
		/* Use the PNG if SVG is not supported */
		.no-svg #bike {
			background-image: url(bike.png);
		}
		/* Use the SVG if supported */
		.svg #bike {
			background-image: url(bike.svg);
		}
		/* Hide the SVG description if using PNG */
		.no-svg #svg-description {
			display: none;
		}
		/* Hide the PNG description if using SVG */
		.svg #png-description {
			display: none;
		}
	</style>
	<script type="text/javascript" src="modernizr.custom.65829.js"></script>
</head>
<body>
<div id="bike"></div>
<p id="svg-description">This bike is a scalable vector graphic.</p>
<p id="png-description">This bike is a raster graphic.</p>
</body>
</html>

View a demo here. Download the source files.

Modernizr SVG Test

To determine how Modernizr is testing for SVG support we can look at the non-minified version of the code. The sections involved in SVG testing are:

ns = {'svg': 'http://www.w3.org/2000/svg'}
...
tests['svg'] = function() {
    return !!document.createElementNS && !!document.createElementNS(ns.svg, 'svg').createSVGRect;
};

The createElementNS method creates an element node given a namespace URI and a qualified name.

Let’s look closer at the comparison operator:

!!document.createElementNS

In older browsers document.createElementNS may be undefined because this method doesn’t exist. In JavaScript undefined is a falsey value. Therefore the not(!) of undefined is true. The not of true is false. So if a browser doesn’t support createElementNS this statement returns false.

In newer browsers createElementNS exists. Therefore document.createElementNS will return the function. A function isn’t falsey, therefore the statement returns true.

!!document.createElementNS(ns.svg, ‘svg’).createSVGRect

Here a SVGSVGElement element called svg is created in the ‘http://www.w3.org/2000/svg’ namespace. Then the createsSVGRect method is called. If the method exists the statement returns true. If the method is undefined the statement returns false.

Combining the two operands means that Modernizr verifies that a SVGSVGElement can be created and that it has a method called createsSVGRect.

Links:
- Bicycle Vector Image
- Namespaces Crash Course
- Namespaces in HTML5

This entry was posted in Uncategorized. Bookmark the permalink.