use
HTML5
&
CSS3
today  with
Modernizr

Eric Phetteplace
Emerging Technologies Librarian
Chesapeake College

@phette23
phette.net
github.com/phette23

The Problem

We want to use modern features today
but they're not supported in every browser

accidentally opened Internet Explorer, horror face

Approaches

  • IE Conditional Comments
  • User Agent Sniffing
  • Live & Let Die

I'm a web developer;
I'll blame IE!

Your
Argument
is Invalid

dozens of tablets computers

User Agent Sniffing

sniffing dog
Android logo
if ( navigator.userAgent.match(/Android 2\.[0-3]/) ) {
	oopsNoInlineSVG();
	oopsNoContentEditable();
	// no need to run oopsNoCssGradients();
}

Did I Not
Just Show
You This Slide?

dozens of latops, tablets, & phones laid out

Live & Let Die

a.k.a. "ignoring older browsers"

you are using an unsupported version of Firefox

Enter Modernizr

One of the web's most popular JavaScript libraries

Modernizr usage trends from Built With

With Modernizr

.no-inlinesvg .svg-wrapper {
  background-image: url(fallback.png);
}

Without Modernizr

if ( navigator.userAgent.match( hugeListOfUAsThatDontSupportSVG ) ) {
  $( 'div > .svg-wrapper' )
  .css( { "background-image":
  "url(fallback.png)" } );
}

Using Modernizr

index.html

<html class="no-js">
	<head>
		<title>Awesome Website</title>
		<script src="modernizr.test.js"></script>
	</head>
...

Pseudo-Modernizr

var docElement = document.documentElement;

docElement.className = docElement.className.replace(/(^|\s)no-js(\s|$)/, '$1$2') + ' js';

var div = document.createElement('div');
div.innerHTML = '<svg/>';

if (div.firstChild.namespaceURI == "http://www.w3.org/2000/svg") {
	docElement.className += ' inlinesvg';
	} else {
	docElement.className += ' no-inlinesvg';
}

Afterwards...

index.html

<html class=" js inlinesvg">
	<head>
		<title>Awesome Website</title>
		<script src="modernizr.svg.js"></script>
	</head>

style.css

.inlinesvg svg { display: block; }

.no-inlinesvg .svg-wrapper, .no-js .svg-wrapper {
	background: url(fallback.png) no-repeat;
}

Let's see an example.

There is no single Modernizr.js

<html lang="en" xml:lang="en" class=" js flexbox canvas canvastext webgl no-touch geolocation postmessage websqldatabase indexeddb hashchange history draganddrop websockets rgba hsla multiplebgs backgroundsize borderimage borderradius boxshadow textshadow opacity cssanimations csscolumns cssgradients cssreflections csstransforms csstransforms3d csstransitions fontface generatedcontent video audio localstorage sessionstorage webworkers applicationcache svg inlinesvg smil svgclippaths cufon-active cufon-ready">

Choices, Choices...

all the checkboxes on the Modernizr build screen

Polyfills

Modernizr.load()

Modernizr.load( {
	test: Modernizr.geolocation,
	nope: "geolocation.js"
} );

advanced Modernizr.load()

Modernizr.load([{
    test : Modernizr.fontface && Modernizr.canvas && Modernizr.cssgradients,
    nope : [ 'presentational-polyfill.js' , 'presentational.css' ] }
    , { test : Modernizr.websockets && window.JSON,
    nope : 'functional-polyfills.js',
    both : [ 'app.js', 'extra.js' ],
    complete : function () { myApp.init(); }, }
    , 'post-analytics.js'
]);

Let's see a more complex example.

Modernizr is not a Superhero

.no-inlinesvg.no-border-image.no-url-data-uri { dont: dothis; }

"The Undetectables"

poster from The Expendables but with quesiton marks over each face

"The Undetectables"

  • Datepicker, Colorpicker
  • Font Rendering
  • position: fixed; on Mobile
  • The Full List

Useful Sites

Thanks!


¿Questions?


Eric Phetteplace
@phette23
phette.net