Front-end development is messy in today's fragmented world. At Artsy, our goal is to do what it takes to provide an incredible experience for all of our users (IE8+, iOS and the usual suspects). Deploying bleeding edge tech, like CSS 3D transforms, is an exercise in compromising principals for practicality -- and managing these "compromises" in well-documented code.
We looked to Modernizr's feature detection approach to provide us with a reliable way to detect CSS3 3D transform support across browsers. They have some well- documented struggles around the issue. After flipping most of the tables in the office ┻━┻ ︵ヽ (`Д´)ﾉ︵ ┻━┻ , we settled on user agent sniffing as the most robust method for detecting CSS3 3D transform support. But why did none of the available methods work for us?
CSS3 3D transforms involve interaction between the browser and the graphics card. The browser may be able to parse the 3D declarations but may not be able to properly instruct the graphics card in how to render your page. There are many possible outcomes ranging from the page rendering with lines across it (Safari 4) to the page rendering beautifully then crashing the browser seconds later (Safari on iOS4). Any 'feature detection' approach would unacceptably flag these as 'supports CSS3 3D transforms'. This is one case where 'feature detection' fails and user agent sniffing (and lots of testing) wins hands down.
Most feature detection assumes a "supports" or "does not support" binary. This is not the case with CSS3 3D transforms -- there is a "gradient of support". Additionally, enabling 3D transforms causes the page to be re-rendered in an entirely different rendering engine which then causes other problems (more on this in a later post).
CSS3 3D transform support can be separated into 4 levels:
- Reliably supports 3D transforms across most machines. For example: Safari 6.
- Can parse and apply 3D transform declarations but ignores the 3D parts. For example: Chrome on a Retina MacBook Pro.
- Can parse and apply 3D transform declarations but renders in unacceptable ways. For example: Safari 4 and Safari 4/5 on Windows show lines across the page.
- Cannot apply 3D transform declarations in any way. For example: IE or Firefox < v10.
Here are a few popular ways of detecting CSS3 3D transform support and why they don't work for us:
Meny / Hakim's method
1 2 3 4 5 6 7
This works best and is straightforward code. The only issue is that it throws a positive for iOS4 causing the browser to crash and a positive for Safari on Windows and Safari 4 OSX which both display a grid over the page when using the 3D renderer.
This only works reliably Safari in our testing.
1 2 3 4 5
This creates a div, transforms it, and then checks if it's position has changed as expected. It only works in reliably in Safari. It sometimes works in Chrome but throws a false positive in the case of Chrome on Retina MacBook Pro as the element does move -- just not in 3D space.
User Agent method
We want to maintain wide support of new tech while ensuring all users have a great experience. Modernizr and the feature detection group have their heart in the right place and do a great job most of the time. That said, user agent sniffing is the only way to handle the complex support scenarios inherent in bleeding edge CSS3 tech such as 3D transforms.
Here is our method/hack for identifying browsers that support CSS3 3D transforms well:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
If you would like to take issue with or improve this code please check it out on Github.