CSS Polyfills

Jason Johnston

@lojjic, @css3pie

http://lojjic.github.com/css-summit-polyfills

Sencha CSS3 PIE

Whoziwhutzit?

Also known as: shim, shiv, patch, hack

Term “polyfill” coined by Remy Sharp:

a piece of code (or plugin) that provides the technology that you, the developer, expect the browser to provide natively

Polyfill

FILL: fills in missing standard features or APIs when not natively available

POLY: can use multiple fallbacks depending on the browser and environment

Why are they needed?

“OldIE”

...but also newer browsers, to keep up with the pace of new features.

Wait a minute...

Don't most libraries smooth out browser incompatibilities?

Abstraction

You use standard code, not new APIs

Goal is to be invisible

Ultimate goal is to become obsolete

Reasons To Use A Polyfill

Modernizr

http://modernizr.com

Modernizr

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

Modernizr

YepNope.js

Conditional loading of polyfill scripts

Modernizr.load([{
    test: Modernizr.flexbox,
    nope: '/polyfills/flexie/flexie.js'
}, ... ]);

Most CSS polyfills have their own way of doing this

Modernizr

Also from the Modernizr community:

The All-In-One Entirely-Not-Alphabetical No-Bullshit Guide to HTML5 Fallbacks

http://bit.ly/polyfills

Things Polyfills Do

Ways CSS Polyfills Work

1. Client-side CSS Parsing/Rewriting

Ways CSS Polyfills Work

2. Server-side CSS Parsing/Rewriting

Ways CSS Polyfills Work

3. DOM Querying

Ways CSS Polyfills Work

4. IE Behaviors (.htc)

Ways CSS Polyfills Work

4. IE Behaviors (.htc)

.my-selector {
    behavior: url(polyfill.htc);
}
<PUBLIC:COMPONENT lightWeight="true">
<PUBLIC:ATTACH EVENT="oncontentready" FOR="element" ONEVENT="init()" />
<PUBLIC:ATTACH EVENT="ondocumentready" FOR="element" ONEVENT="init()" />
<PUBLIC:ATTACH EVENT="ondetach" FOR="element" ONEVENT="cleanup()" />

<script type="text/javascript">
//JS code
</script>

</PUBLIC:COMPONENT>

Let's explore a few...

html5shim / html5shiv

http://code.google.com/p/html5shiv/

Selectivizr

Selectivizr

http://selectivizr.com/

Selectivizr Support Chart
-prefix-free

-prefix-free

http://leaverou.github.com/prefixfree/

Respond.js

https://github.com/scottjehl/Respond

Flexie

http://flexiejs.com/

Flexie Site
Flexie Playground

CSS Sandpaper

https://github.com/zoltan-dulac/cssSandpaper

CSS3 PIE

CSS3 PIE

http://css3pie.com/

CSS3 PIE Site

The big question

Q: Should I use a polyfill?

A: It depends.

Considerations

Graceful Degradation

Decisions, decisions...

1. “Is graceful degradation acceptable, or can I convince the necessary parties to allow it?”

YES? Great, do it!

NO? ...

Decisions, decisions...

2. “Would making my design work in all browsers require sending nonstandard or hacky markup, extra images, or dumbed-down code to modern browsers?”

NO? Then we're all good.

YES? Then consider using a polyfill...

Decisions, decisions...

3. “Would the polyfill create unacceptable performance or compatibility issues?”

NO? Yay! Polyfill FTW! :)

YES? Sorry, you're on your own. :(

Assessing Performance

Polyfill creators don't typically publish performance stats; it's up to you to measure.

Common Factors

Assessing Performance

Polyfills that rewrite CSS

Assessing Performance

Polyfills that query the DOM

Assessing Performance

Polyfills that render

Compatibility Concerns

Test, test, test!

In Conclusion,

Thank you! Questions?

Jason Johnston

@lojjic, @css3pie